Reputation: 112
My objective is to have the user enter the name of a category, the grade of that category, and the weight of that category.
In theory, everything should work perfectly, except my first loop never ends.
This is the code I have so far:
package gradecalculator;
import java.util.Scanner;
public class GradeCalculator
{
public static void main(String[] args)
{
String categoryName = null;
double categoryGrade;
double categoryWorth;
double Grade = 0;
String[] nameArray = new String[10];
double[] gradeArray = new double[10];
double[] worthArray = new double[10];
double[] categoryArray = new double[10];
int c = 0;
int c1 = 0;
Scanner entry = new Scanner(System.in);
while(!"Quit".equals(categoryName))
{
categoryName = entry.next();
nameArray[c] = categoryName;
categoryGrade = entry.nextDouble();
gradeArray[c] = categoryGrade;
categoryWorth = entry.nextDouble();
worthArray[c] = categoryWorth;
categoryArray[c] = finalCategory(categoryGrade, categoryWorth);
c++;
}
while(c != 0)
{
System.out.printf("%S:\t%f%\tWorth: %f\n", nameArray[c1], gradeArray[c1], worthArray[c1]);
c--;
c1++;
}
while(c1 != 0)
{
Grade = Grade + categoryArray[c];
c1--;
c++;
}
}
public static double finalCategory(double Grade, double Worth)
{
return (Grade * (Worth / 100));
}
}
I believe that the problem has been narrowed down to:
while(!"Quit".equals(categoryName))
{
categoryName = entry.next();
nameArray[c] = categoryName;
categoryGrade = entry.nextDouble();
gradeArray[c] = categoryGrade;
categoryWorth = entry.nextDouble();
worthArray[c] = categoryWorth;
categoryArray[c] = finalCategory(categoryGrade, categoryWorth);
c++;
}
and more specifically:
while(!"Quit".equals(categoryName))
What I am trying to accomplish is that as soon as the user enters the categoryName
and it turns out to be the word "Quit", the while loop will be terminated, and the program will move on. Obviously this isn't happening, so I would greatly appreciate any help to solve the problem.
Upvotes: 1
Views: 276
Reputation: 1
You can check to see if the string the user entered was "quit" directly after they input their string. Just add the code below after categoryName = entry.next();
if(categoryName.equalsIgnoreCase("quit"))
break;
This ignores the case that the user enters as well if that's what you want.
Upvotes: 0
Reputation: 12558
The problem is that the while-loop body continues execution even when categoryName
is "Quit"
. You can assign entry.next()
to categoryName
and do a "Quit"
check all in the while-loop's condition:
while(!(categoryName = entry.next()).equals("Quit"))
{ ... }
This line would have to be removed (first line of while-loop body):
categoryName = entry.next();
There's also a problem in your format String:
"%S:\t%f%\tWorth: %f\n"
%\t
will try to find a conversion for the tab character. If you want to print %
literally, escape it with another %
:
"%S:\t%f%%\tWorth: %f\n"
Upvotes: 0
Reputation: 47269
You ask for categoryName
before you take input for the other doubles. Your program is waiting for you to enter the next doubles even after you enter "Quit"
.
Upvotes: 1