Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7158017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T12:59:27+00:00 2026-05-28T12:59:27+00:00

I recently jumped back into programming and decided to learn Java as my first

  • 0

I recently jumped back into programming and decided to learn Java as my first major language. I got pretty far without any major issues, until I started delving into file saving. I pretty much went with the first method I came across; input and output streaming. The problem I’m encountering is in the method “analyzeIncidents.” If no user data exists then the method runs properly and I get the expected output. If user data DOES exist, then the first if…then statement in “analyzeIncidents” will run, but it will never find the conditional statement to be true. Once I delete the UserData.sav file, however, everything works again. I’ve spent hours checking the output at all stages of the program, but so far can find no reason why the if…then statement fails when using loaded data but succeeds otherwise. I’ve also tried saving then loading the data right before it gets tossed to “analyzeIncidents” and it works on the first run through, but fails on any attempt afterwards. Hopefully you guys can point out what I’m sure is an obvious mistake on my part.

MAIN:

import java.io.*;
import java.util.*;


public class nutralyzeApp {


    static Scanner userInput = new Scanner(System.in);

    public static void main(String[] args) {

        int elements = 0;
        int selectedElement = 0;
        int[][] incidentAnalysis = new int[2][50];
        String newElement = "999";
        String[][] data2012 = new String[366][51];


        //Checks to see if previous save file exists. If not, creates new save file.
        File f = new File("C:/Users/Liam/Dropbox/Workspace/Nutralyze/UserData.sav");
        if(f.exists()) {
            System.out.println("Previous work session loaded.");
            data2012 = loadData();
        } else {
            data2012[0][0] = "granola";
            data2012[0][1] = "gluten";
            data2012[0][2] = "sugar";
            data2012[0][3] = "beef";
            data2012[1][0] = "dairy";
            data2012[8][0] = "cheese";
            data2012[59][0] = "pizza";
            data2012[59][1] = "chips";
            data2012[206][0] = "beer";
            data2012[206][1] = "cheese";

            data2012[8][50] = "incident";
            data2012[59][50] = "incident";
            data2012[85][50] = "incident";
            data2012[190][50] = "incident";
            data2012[206][50] = "incident";

            saveData(data2012);
        }

        System.out.println("What day is it? ");
        int currentDay = userInput.nextInt();
        elements = numberOfElements(currentDay,data2012);

        System.out.println("Elements consumed that day: " + elements);

        System.out.println("What element would you like to add?");
        newElement = userInput.next();

        while (!newElement.equals("49")) {
            data2012 = addElement(currentDay,elements,newElement,data2012);
            elements = numberOfElements(currentDay,data2012);
            System.out.println("What element would you like to add?");
            newElement = userInput.next();
        }

        System.out.print("Which element would you like to see? ");
        selectedElement = userInput.nextInt();

        while (selectedElement != 49) {
            System.out.println("The element you selected is: " + data2012[currentDay][selectedElement]);
            System.out.println("You typed: " + selectedElement);
            System.out.print("Which element would you like to see? ");
            selectedElement = userInput.nextInt();
        }

        data2012 = gatherElements(data2012);
        incidentAnalysis = analyzeIncidents(data2012);  

        for (int x=0;x<10;x++) {
            System.out.print(data2012[365][x] + " --> " + incidentAnalysis[0][x] + "/" + incidentAnalysis[1][x]+ "\n");
        }

        //Resets data and saves to file.
        for (int x=0;x<50;x++) {
            data2012[365][x] = null;
        }   

        saveData(data2012); 
    }

METHODS:

    private static int[][] analyzeIncidents(String[][] userData) {

        boolean found = false;
        int elements1 = 0;
        int elements2 = 0;
        int incidentCounter = 0;
        int currentIncident = 0;
        int[] incidentDates = new int[365];
        int[][] incidentReport = new int[2][50];

        for (int x=0;x<365;x++) {
            if (userData[x][50]=="incident") {
                incidentDates[incidentCounter]=x;
                incidentCounter++;
                System.out.println(userData[x][50]);
            } 
        }
System.out.println(incidentCounter + "          " + userData[8][50]);
        elements1 = numberOfElements(365,userData);
        for (int x=0;x<elements1;x++) {

            for (int y=0;y<incidentCounter;y++) {

                currentIncident = incidentDates[y];
                elements2 = numberOfElements(currentIncident,userData);

                for (int z=0;z<elements2;z++) {
                    if ((userData[365][x]==userData[currentIncident][z]) && (found==false)) {
                        incidentReport[0][x]++;
                        incidentReport[1][x]++;
                    } else if ((userData[365][x]==userData[currentIncident][z]) && (found==true)) {
                        incidentReport[1][x]++;
                    }
                }


            }
        }

        return incidentReport;
    }

    private static String[][] gatherElements(String[][] userData) {

        int z = 0;
        int elements = 0;
        int uniqueElements = 0;
        boolean found = false;

        for (int x=0;x<50;x++) {
            userData[365][x] = null;
        }
        //userData[365][0] = userData[0][0];

        for (int x=0;x<365;x++) {
            elements = numberOfElements(x,userData);

            for (int y=0;y<elements;y++) {
                while (z<50) {
                    if (userData[365][z]!=userData[x][y]) {
                        z++;
                        found=false;
                    } else if (userData[365][z]==userData[x][y]) {
                        z=50;
                        found=true;
                        break;
                    }
                }
                z=0;

                if (found==false) {
                    userData[365][uniqueElements] = userData[x][y];
                    uniqueElements++;
                }
            }
        }
        return userData;
    }

    private static String[][] addElement(int day,int position,String newElement,String[][] userData) {

        userData[day][position] = newElement;
        return userData;
    }

    private static int numberOfElements(int day, String[][] userData) {

        int x = 0;

        while (userData[day][x] != null && x < 365) {
            x++;
        }
        return x;
    }

    private static void saveData(String[][] data2012) {

        try{
            // Open a file to write to, named UserData.sav
            FileOutputStream saveFile = new FileOutputStream("UserData.sav");
            // Create an ObjectOutputStream to put objects into save file
            ObjectOutputStream save = new ObjectOutputStream(saveFile);
            // Now we do the save
            save.writeObject(data2012);
            // Close the file
            save.close(); // This also closes saveFile  
        }
        catch(Exception exc){
        exc.printStackTrace(); // If there was an error, print the info
        }
    }

    private static String[][] loadData() {

        String[][] userData = new String[366][51];

        try{
           // Open file to read from, named UserData.sav
           FileInputStream saveFile = new FileInputStream("UserData.sav");

           // Create an ObjectInputStream to get objects from save file
           ObjectInputStream save = new ObjectInputStream(saveFile);

           // Now we do the restore
           // readObject() returns a generic Object, we cast those back
           // into their original class type
           // For primitive types, use the corresponding reference class

           userData = (String[][]) save.readObject();

           // Close the file
           save.close(); // This also closes saveFile
        }
        catch(Exception exc){
        exc.printStackTrace(); // If there was an error, print the info
        }
              return userData;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T12:59:29+00:00Added an answer on May 28, 2026 at 12:59 pm

    You’re comparing strings by their references, instead of for semantic equality. This:

    if (userData[x][50]=="incident")
    

    should be:

    if (userData[x][50].equals("incident"))
    

    Or if you want to continue even if userData[x][50] is null:

    if ("incident".equals(userData[x][50]))
    

    You do the same elsewhere, too:

    if (userData[365][z]!=userData[x][y]) {
    ...
    } else if (userData[365][z]==userData[x][y]) {
    }
    

    This should be:

    if (!userData[365][z].equals(userData[x][y])) {
    ...
    } else {
    }
    

    (You don’t need the second condition, as it’s the exact opposite of the first.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently jumped into the world of jQuery. I saw the methods find()
Recently, while reading a Socket Programming HOWTO the following section jumped out at me:
I jumped into learning django recently. I am rendering my template with citylist like,
I've recently jumped into the XNA pool, and am learning all the ins and
I've only recently jumped into the world of iphone development and objective-c, so I'm
Recently we got a new server at the office purely for testing purposes. It
I recently jumped on a project using Pylons. I'm not familiar with either Python
Recently we have released our product. Our team decided to preserve the source code
Premise I recently ran into a bug in a select statement in my code.
Recently, I ran into a problem with my application: the size of the JSON

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.