Search code examples
javajvminitializationvoiddeclare

ERROR CANT FIND SYMBOL (FOR LOOP)


Whenever i run this program, it says that it can't find the symbol for SHOTS (last line) , so I think the problem is because i am declaring the SHOTS inside the FOR LOOP, but when I wanted to print it out, I cant..

import java.util.Scanner;

public class CoffeeBot {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println ("Hello, what's your name?");
        String name = keyboard.nextLine();
        System.out.println("Would you like to order some coffee, " + name + "? (y/n)");
        String goon=keyboard.next();
        char answer=goon.charAt(0);
        if ((answer!= 'y') && (answer!='n'))
            System.out.println("no valid");
        else if (answer== 'n')
            System.out.println("OK BYE");
        else {
            System.out.println("Great, Let's get started.");
            System.out.println("Order selection");
            System.out.println("----------------");
            System.out.println("There are 90 coffee cups in stock and each costs $2.00");
            System.out.println("There are 100 coffee shots in stock and each costs $1.00");
            System.out.println("How many cups of coffee would you like?");
            int CupsOfCoffee = keyboard.nextInt();
            if (CupsOfCoffee ==0)
                System.out.println("No cups, no coffee, Goodbye");
            else if (CupsOfCoffee < 0)
                System.out.println("Doesn't compute, system terminating");
            else if (CupsOfCoffee >90)
                 System.out.println("Not enogh stock,come back later");
            else {
                int countd;
                for (countd = 1; countd<= CupsOfCoffee; countd++) {
                    System.out.println("How many coffee shots in cup "+ countd);
                     int shots = keyboard.nextInt();
                }
                System.out.println("Order Suammery\n----------------");
                for (countd = 1; countd<= CupsOfCoffee; countd++)
                    System.out.println("cup " + countd + "has" + shots+  "and will cost" ) ;
            }
        }
    }
}

Then I tried to do some changes , by declaring the SHOTS outside the FOR LOOP, and i came up with that, by getting my output (SHOTS) the last number I enter: for example when I they ask how many shots, I say 2,3,4,.. all the cups (1,2 and 3) got 4 shots, which i want to get 2 shots for cup1, 3 shots for cup 2, and 4 shots for cup 3.. thats an example

 import java.util.Scanner;
 public class CoffeeBot
{
 public static void main(String[] args)
 {

Scanner keyboard = new Scanner(System.in);
System.out.println ("Hello, what's your name?");
String name = keyboard.nextLine();
System.out.println("Would you like to order some coffee, " + name + "? (y/n)");
 String goon=keyboard.next();
 char answer=goon.charAt(0);  
 if ((answer!= 'y') && (answer!='n'))
 System.out.println("no valid");
 else if (answer== 'n')
 System.out.println("OK BYE");
else{
System.out.println("Great, Let's get started.");
 System.out.println("Order selection");
 System.out.println("----------------");
  System.out.println("There are 90 coffee cups in stock and each costs $2.00");
  System.out.println("There are 100 coffee shots in stock and each costs $1.00");
   System.out.println("How many cups of coffee would you like?");
   int CupsOfCoffee = keyboard.nextInt();
   if (CupsOfCoffee ==0)
   System.out.println("No cups, no coffee, Goodbye");
  else if (CupsOfCoffee < 0)
  System.out.println("Doesn't compute, system terminating");
  else if (CupsOfCoffee >90)
     System.out.println("Not enogh stock,come back later");
    else {


   int countd;
   int shots=0;
     for (countd = 1; countd<= CupsOfCoffee; countd++)
   {
      System.out.println("How many coffee shots in cup "+ countd);

     shots = keyboard.nextInt();
    }
   System.out.println("Order Suammery\n----------------");
    for (countd = 1; countd<= CupsOfCoffee; countd++)

   System.out.println("cup " + countd + "has" + shots+  "and will cost" ) ;

   }
   }
   }
   }

I think I should store the SHOTS values in an int[] array.


Solution

  • I can see a couple of problems. Firstly, in the println statement on the last line, you're referring to shot rather than shots.

    Secondly, declaring shots outside the for loop is fine, but you're then redefining it inside the loop, hiding the other variable.

    To use an array, try something like:

    ...
    int[] shots = new int[CupsOfCoffee];
    for (countd = 0; countd < CupsOfCoffee; countd++)
    {
        System.out.println("How many coffee shots in cup " + (countd + 1));
    
        shots[countd] = keyboard.nextInt();
    }
    System.out.println("Order Suammery\n----------------");
    for (countd = 0; countd < CupsOfCoffee; countd++)
    
    System.out.println("cup " + (countd + 1) + " has " + shots[countd] +  " shots and will cost" ) ;
    

    Note that array indexes begin at zero, so I've updated countd to reflect that.

    As a general style point, variables should generally begin with a lower case letter, so you should use cupsOfCoffee ideally.