I am having some trouble getting this to compile... It says, \"GetArrayValues.ja
ID: 3627507 • Letter: I
Question
I am having some trouble getting this to compile...
It says, "GetArrayValues.java:41: variable play might not have been initialized" for the line reading while(play == true);
here is my code..
import java.util.Scanner;
public class GetArrayValues
{
public static void main( String [] args )
{
boolean play; // True if user wants to play again.
String playAgain;
do
{
Scanner scan = new Scanner( System.in );
double total = 0.0;
int size; // The number of meals the user is entering
System.out.println("How many meals would you like to enter? ");
size = scan.nextInt();
int [] meals = new int[size];
for ( int i = 0; i < meals.length; i++ )
{
System.out.println( "Enter total number of calories for meal "
+ ( i + 1 ) + ": ");
meals[i] = scan.nextInt( );
total += meals[i];
}
System.out.println( "Your total caloric intake for the day: " +total);
System.out.println("Play again? ");
playAgain = scan.nextLine();
if(playAgain == "yes" || playAgain == "y")
{
play = true;
}
else if (playAgain == "no" || playAgain == "n")
{
play = false;
}
else
{
System.out.println("Oops, please answer yes or no ");
System.out.println("Play again? ");
}
}
while(play == true);
}
}
Explanation / Answer
You need to declare play on this line:
"boolean play; // True if user wants to play again."
You should change it to either true or false. Let's set it as false:
"boolean play = false; // True if user wants to play again."
Your program will compile after this. However, it still doesn't work! As soon a trial run of the program the user never gets the chance to enter whether or not he wants to run the program again; it uses whatever the initial true/false value of play is and starts the program over.
It's because you didn't consume the remaining newline character introduced by using the .nextInt() from the Scanner class.
So go to this line:
"meals[i] = scan.nextInt( );"
And change it to this:
"meals[i] = scan.nextInt( );
scan.nextLine();
"
This will consume the remaining newline character and allow you to continue data entry.
However, your program is still faulty! You used "==" when comparing the string character and you didn't ask the user how the input should be intered.
You should use the .Equals from the string class to do a case by case compare to the string instead of the soft compare from '=='.
So go to any line where you compare a string with '==', here's one of them:
"if(playAgain =="yes" || playAgain == "y")"
Becomes:
"if(playAgain.equals("yes") || playAgain.equals("y"))"
So here's your revised program:
import java.util.Scanner;
public class GetArrayValues
{
public static void main( String [] args )
{
boolean play = false; // True if user wants to play again.
String playAgain;
do
{
Scanner scan = new Scanner( System.in );
double total = 0.0;
int size; // The number of meals the user is entering
System.out.println("How many meals would you like to enter? ");
size = scan.nextInt();
int [] meals = new int[size];
for ( int i = 0; i < meals.length; i++ )
{
System.out.println( "Enter total number of calories for meal "
+ ( i + 1 ) + ": ");
meals[i] = scan.nextInt( );
scan.nextLine();
total += meals[i];
}
System.out.println( "Your total caloric intake for the day: " +total);
System.out.println("Play again?");
playAgain = scan.nextLine();
if(playAgain.equals("yes") || playAgain.equals("y"))
{
play = true;
}
else if (playAgain.equals("no") || playAgain.equals("n"))
{
play = false;
}
else
{
System.out.println("Oops, please answer yes or no ");
System.out.println("Play again? ");
}
}
while(play == true);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.