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 8828591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:38:31+00:00 2026-06-14T07:38:31+00:00

The array stores all the information, I feel like this program is really close

  • 0

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

  • 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-06-14T07:38:32+00:00Added an answer on June 14, 2026 at 7:38 am

    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.

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

Sidebar

Related Questions

I have a snippet of code that creates an array clusterUniqueMarkers that stores all
While working with ActiveRecord I have a table which stores a serialized array of
I have an array stored in a variable temp which looks like this: temp.inspect
I feel like I am missing something very simple here, but this is the
This might seem like a duplicate question, but I can't find any information on
I am having list of char array by that I want to store all
I've got an array called $bigArrayWithLinks that stores a string with a url in
I have a PHP class that stores a complex multidimensional array, and rather than
I have an array: int[][] lawn = new int[980][1280]; wich stores the values of
I'm trying to write a code which stores strings in an array. I'm trying

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.