Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Error Accessing Array Index

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error Accessing Array Index

    I'm writing code to have the user input entrees served on certain days. The error is coming in when I ask the user to search for a certain entrée so I can output the day it's served.

    The error I'm getting is java:39: cannot find symbol for this line:
    System.out.println(food+" is served on " +day[index]);

    symbol: variable index

    I accessed the day index earlier in the code without errors, so I'm not sure what's wrong.

    import java.util.Scanner;
     
    public class Assignment3
    {
       public static void main(String[] args)
      {
       //create scanner object
       Scanner sc = new Scanner(System.in);
       String food;
     
       //create day array
       String[] day = {"Monday", "Tuesday","Wednesday", "Thursday", "Friday"};
     
       //create entree array
       String[] entree = new String[5];
     
       //create price array
       double[] price = new double[5];
     
       //prompt user for input for entree
       for(int index=0; index < day.length; index++)
       {
       System.out.println("What entree is being served on " +day[index]);
       entree[index]=sc.next();
       }
       //prompt user for price
       for(int index=0; index < entree.length; index++)
       {
       System.out.println("What is the price for "+entree[index]);
       price[index]=sc.nextDouble();
       }
       System.out.println("Enter an entree to search for:");
       food=sc.next();
       System.out.println(food+" is served on " +day[index]);
     
    	//Find the highest priced entree
    	double max = price[0];
    	for(int index=1; index < price.length; index++)
    	{
    	//examine the current element in the array
    	//determine if it is larger than the current max
    	if(price[index]>max)
    	  {
    	   //if so, replace the current max
    	   max = price[index];
    	  }
    	}
    	System.out.println();
    	System.out.println("The most expensive entree is " +max);
       }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,273 Times in 2,244 Posts

    Default Re: Error Accessing Array Index

    cannot find symbol for this line:
    System.out.println(food+" is served on " +day[index]);
    symbol: variable index
    Looks like the variable: index is defined only inside of the for loop.

    Why do you want to use the loop control variable (index) outside of the loop? What slot in the day array do you want to access?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error Accessing Array Index

    Quote Originally Posted by Norm View Post
    Why do you want to use the loop control variable (index) outside of the loop? What slot in the day array do you want to access?
    That's the problem, the slot will be determined by user input when they search for an entrée.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,273 Times in 2,244 Posts

    Default Re: Error Accessing Array Index

    the slot will be determined by user input
    Can you save that value in a variable that is in scope outside of the loop? Define a variable to hold the value outside the loop, set its value inside of the loop and then its value will be available after the loop finishes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Accessing String Array
    By krulle in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 9th, 2014, 06:55 PM
  2. How to find the array index of the lowest value in the array?
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2013, 05:57 AM
  3. help with index in array
    By lanya1 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 6th, 2013, 08:58 PM
  4. Accessing array of method
    By ncode in forum Java Theory & Questions
    Replies: 3
    Last Post: October 18th, 2012, 08:11 AM
  5. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM