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: array required, but String found

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

    Default error: array required, but String found

    This assignment requires a program that asks a customer for their burger order and outputs the order and price. I can't figure out how to fix the errors that I'm getting here. Please help. I'm new to Java and not very good at it.

    import java.util.Scanner;
     
    public class Hamburger
    {
     
    Scanner sc=new Scanner(System.in);
    //write constructor for single on white w/ no cheese and no toppings for $1.99
     
    String bun=" ";
    int patties=1;
    boolean addCheese=false;
    String toppings=" ";
    double price=1.99;
     
     
     
    public boolean getAddCheese()
    {
    return addCheese;
    }
     
    public void setAddCheese(int newAddCheese)
    {
    System.out.println("Welcome to Dead Cow on a Bun. Would you like cheese on your dead cow?");
    String cheese = sc.next();
       boolean addCheese = true;
        if(cheese.equalsIgnoreCase("Yes"))
        {
             addCheese = true;
        }
        else if(cheese.equalsIgnoreCase("No"))
        {
            addCheese = false;
        }
    }
     
    public int getSuperSizeIt()
    {
    return patties;
    }
     
    public void setSuperSizeIt(int newPatties)
    {
    patties=newPatties;
    System.out.println("Do you want to supersize your dead cow?");
    String superSizeIt=sc.next();
    if(superSizeIt.equalsIgnoreCase("Yes") || superSizeIt.equalsIgnoreCase("y"))
    {
    price=price+1.0;
    patties=patties+1;
    }
    }
     
    public String getBun()
    {
    return bun;
    }
     
    public void setBun(String newBun)
    {
     
    System.out.println("What kind of bun would you like?");
    bun=newBun;
    }
     
    public int getToppings()
    {
    return toppingChoices;
    }
     
    public void setToppings(String newtoppingChoices)
    {
    String[] toppings = {"Bacon", "Grilled Mushrooms", "Lettuce", "Onion Jam", "Magic Sauce"};
    String[] toppingChoices = new String[5];
    for(int index=0; index< toppings.length; index++)
        {
            System.out.println("Please answer Y or N. Would you like " +toppings[index]);
            toppingChoices[index]=sc.nextLine();
        }
    }
     
     
    //burgerDetails - prints order
    public void burgerDetails(boolean addCheese, int patties, String bun, String toppingChoices)
    {
    System.out.println("Cheese: " +addCheese);
    System.out.println("Patties: " +patties);
    System.out.println("Bun: " +bun);
    System.out.print("Toppings: " +toppingChoices[index]);
    }
     
    }

    Here are the errors I'm getting:
    Hamburger.java:73: error: cannot find symbol
    return toppingChoices;
           ^
      symbol:   variable toppingChoices
      location: class Hamburger
    Hamburger.java:94: error: cannot find symbol
    System.out.print("Toppings: " +toppingChoices[index]);
                                                  ^
      symbol:   variable index
      location: class Hamburger
    Hamburger.java:94: error: array required, but String found
    System.out.print("Toppings: " +toppingChoices[index]);

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

    Default Re: error: array required, but String found

    The cannot find symbol error is because the the compiler can not find a definition for the variable named in the error message that is in scope where it was coded.

    array required, but String found
    Using array notation: variable[anIndex] requires that the variable be an array. The compiler found a String variable instead of an array.

    Note: The posted code has lost most of its indentations making it harder to read and understand. Please post properly indented code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    75
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: error: array required, but String found

    1.
    public int getToppings()
    {
    return toppingChoices;
    }
    This has two problems: There is no 'toppingChoices' variable in scope for this function, and based on how you've used the other 'toppingChoices' variables I believe the int return value on this function is wrong. It should return a String array.

    At the top when you do this:
    String bun=" ";
    int patties=1;
    boolean addCheese=false;
    String toppings=" ";
    double price=1.99;
    It makes all those variables "visible" to all the functions in the class. You need to do the same thing with 'toppingChoices'. Your setToppings() function is creating its own 'toppingChoices' array, but that should be moved to become a class-level variable.

    2.
    System.out.print("Toppings: " +toppingChoices[index]);
    This has two problems as well: Again there is no 'toppingChoices' variable in scope for this function (but that'll be fixed when you move 'toppingChoices' to the class level). There's also no 'index' variable in scope for this function. Since you need to print all the chosen toppings, you could loop over the array and print them individually (at which point you'll have an index), or simply print out 'toppingChoices' (without an array index) to let the Array class format it to a single String.

    Hope that helps!
    Last edited by BinaryDigit09; April 20th, 2018 at 09:36 AM. Reason: Forum acting foolish

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

    Default Re: error: array required, but String found

    Thank you, that solved the problems with my class! Now if only I could get my driver to work

Similar Threads

  1. Replies: 2
    Last Post: March 23rd, 2014, 08:44 AM
  2. [ASK]Array required, but java.lang.Object Found
    By vikar in forum Object Oriented Programming
    Replies: 5
    Last Post: July 9th, 2013, 06:56 AM
  3. Replies: 6
    Last Post: March 1st, 2013, 01:06 PM
  4. string identifier symbol not found
    By MeteoricDragon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 26th, 2012, 04:52 PM
  5. "possible loss of precision, found double, required int" HELP
    By kkatchh in forum Loops & Control Statements
    Replies: 3
    Last Post: November 6th, 2011, 09:50 AM