# Thread: Need help with a "Continue? (y/n)"

1. Member
Join Date
Apr 2013
Posts
3
Rep Power
0

## Need help with a "Continue? (y/n)"

Hey guys,

First off, thank you for coming here to help me! Secondly, I'm terrible at coming up with titles, so I hope that got your attention! I'm going to keep this short n simple to avoid confusion...here is my entire code:

-------------------------------------------------------------------------------------------

import java.util.Scanner;
import java.text.NumberFormat;

public class TestScoreApp
{

public static void main(String[] args)
{
int scoreTotal = 0;
int scoreCount = 0;
double averageScore = 0;

Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get the input from the user
int testScore = getIntWithinRange (sc, "Enter score: ", 0, 100);
scoreCount += 1;
scoreTotal += testScore;

averageScore = (double) scoreTotal / (double) scoreCount;

// see if the user wants to enter more test scores and discard any other value
System.out.print ("Continue? (y/n): ");
choice = sc.next ();

}

NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(1);
String message = "\n" +
"Score count: " + scoreCount + "\n"
+ "Score total: " + scoreTotal + "\n"
+ "Average score: " + number.format(averageScore) + "\n";
System.out.println(message);
}

public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)

{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}

public static int getInt(Scanner sc, String choice)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(choice);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
}
-------------------------------------------------------------------------------------------

Now, I've been asked to write code that discards any extra entries at the prompt that asks if you want to enter another score (I put it in bold). The book tutorial I'm using says I can do this easily with an if statement but in another section it says you can use a while loop. Basically, if the user enters in something other than "y" or "n", such as "y7" which is incorrect then the system needs to discard that input and display a message saying it was invalid and then asks again if the user wants to continue.

I'm hoping one of you smart folks out there have the ability to read over the code and guide me in the right direction as I've been trying to code in "if statements" for a few hours now to no avail! lol I know it's probably something extremely basic but I can't comprehend it!

2. Member
Join Date
Apr 2013
Posts
3
Rep Power
0

## Re: Need help with a "Continue? (y/n)"

Any ideas? :D

3. Member
Join Date
Apr 2013
Posts
41
Rep Power
0

## Re: Need help with a "Continue? (y/n)"

this is a simple way that is very efficient. it is called a "do-while" loop.
for instance:
Java Code:
do{
System.out.print(BLAH);
x++
}
while( x < 5 );
i know this is different code but you could easily make the while like != y or something like that.
whiles are best used with booleans because the escape is as easy a "" = false; to cut out. hope that helps.

4. Member
Join Date
Apr 2013
Posts
3
Rep Power
0

## Re: Need help with a "Continue? (y/n)"

Originally Posted by leeroijenkiins
this is a simple way that is very efficient. it is called a "do-while" loop.
for instance:
Java Code:
do{
System.out.print(BLAH);
x++
}
while( x < 5 );
i know this is different code but you could easily make the while like != y or something like that.
whiles are best used with booleans because the escape is as easy a "" = false; to cut out. hope that helps.
Ill give it a shot.. Thank you very much for your time! :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•