1

The array stores all the information, I feel like this program is really close to working. I know its not tidy, I'll clean it right after! Problem is at the bottom.

 public class FoodFacts
{
     private static BufferedReader textIn;
     private static BufferedReader foodFacts;
             static int numberOfLines = 0;
               static  int NUM_COL = 7;
            static int NUM_ROW = 961;
             static String [][] foodArray = new String[NUM_ROW][NUM_COL];
      public static String  fact;
    // Make a random number to pull a line
    static Random r = new Random();




    public static void main(String[] args)
        {
            try 
            {   

                textIn = new BufferedReader(new InputStreamReader(System.in));
                foodFacts= new BufferedReader(new FileReader("foodfacts.csv"));
                Scanner factFile = new Scanner(foodFacts);
                List<String> facts = new ArrayList<String>();

                //  System.out.println("Printing out your array!");
                while ( factFile.hasNextLine()){
                 fact = factFile.nextLine();
                 StringTokenizer st2 = new StringTokenizer(fact, ",")    ;

               while (st2.hasMoreElements()){
                  for ( int j = 0; j < NUM_COL ; j++) {
                    foodArray [numberOfLines][j]= st2.nextToken();  
                    //System.out.println("Foodarray at " + "  " + numberOfLines + " is " +foodArray[numberOfLines][j]);
                   }
                    }  
                     numberOfLines++;
                  }

System.out.println("Please type in the food you wish to know about.");
                String request; //user input 
                request = textIn.readLine();
                System.out.println ("You requested" + request);

Problem starts here!

for ( int i = 0; i < NUM_ROW ; i++)
                    {
if ( foodArray[i][0] == request)
                            for ( int j = 0 ; j < NUM_COL ; j++ )
                        System.out.println ( foodArray[i][j] ); //never prints anything
                        }  

                  }
                    catch (IOException e)
                    {
                    System.out.println ("Error, problem reading text file!");
                    e.printStackTrace();
                    }

             }
  }

I'm trying to test it in the terminal where the foodArray[6][0] should match input All-Bran Cereal

Fred V
  • 187
  • 1
  • 11

1 Answers1

2

In your last for loop, you are comparing your string using == operator in your if construct, which would give you incorrect result, because == compares the string reference, which would be different, as both the references point to different string objects.

Use equals method to compare string contents: -

if (foodArray[i][0].equals(request))

You should always use equals method with any object if you want to compare their content.

Check out this post: - How do I Compare strings in Java for more details.

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525