Hi People,

When creating this program, I am trying to add a while loop so that I can continue to pull in input from the user until they put a -1 for the input. This is the error I get.

java:46: cannot find symbol
symbol : variable input
location: class GradesFour
String[] studs = input.split("\\s+");

With the arrow pointing to input. Normally I would know how to answer this but the input variable is already initialized! Please assist. See below.

import java.util.*;

public class GradesFour
{
public static void main(String[] args)
{
int total = 0;
int numberOfAs = 0;
int numberOfBs = 0;
int numberOfCs = 0;
int numberOfDs = 0;
int numberOfFs = 0;
int studsCount = 0;
int maxA = -1;
int maxB = -1;
int maxC = -1;
int maxD = -1;
int maxF = -1;
int minA = 101;
int minB = 101;
int minC = 101;
int minD = 101;
int minF = 101;
double aScore = 0;
double bScore = 0;
double cScore = 0;
double dScore = 0;
double fScore = 0;

boolean finished = false;
Scanner keyboard = new Scanner(System.in);
while(!finished)
{
System.out.println("Please enter a list of exam scores (-1 to end input)");
String input = keyboard.next();
if(input.equals(-1));{
finished = true;
}
}
String[] studs = input.split("\\s+"); //Setting a delimiter to separate the string. 
System.out.println(" Statistics");
System.out.println("------------------------------");

int grade = 0;
//Loop designed to look at each part of an array and then classify it by letter grade.
for(int i = 0; (i < studs.length) && (studs[i] != "-1"); i++){
grade = Integer.parseInt(studs[i]);
studsCount++;
total += grade; //Adds the new grade to the running total.
//Determines letter Grade max and mins.
if((grade >= 90) && (grade>maxA)) maxA = grade;
if((grade >= 90) && (grade<minA)) minA = grade;
if((grade >= 80 && grade < 90) && (grade>maxB)) maxB = grade;
if((grade >= 80) && (grade<minB)) minB = grade;
if((grade >= 70 && grade < 80) && (grade>maxC)) maxC = grade;
if((grade >= 70) && (grade<minC)) minC = grade;
if((grade >= 60 && grade < 70) && (grade>maxD)) maxD = grade;
if((grade >= 60) && (grade<minD)) minD = grade;
if((grade < 60) && (grade>maxF)) maxF = grade;
if((grade < 60 && grade >= 0) && (grade<minF)) minF = grade;
//Calculates the number of each type of letter grade.
numberOfAs = (grade >= 90)?(numberOfAs + 1):numberOfAs;
numberOfBs = (grade >= 80 && grade < 90)?(numberOfBs + 1):numberOfBs;
numberOfCs = (grade >= 70 && grade < 80)?(numberOfCs + 1):numberOfCs;
numberOfDs = (grade >= 60 && grade < 70)?(numberOfDs + 1):numberOfDs;
numberOfFs = (grade < 60 && grade >= 0)?(numberOfFs + 1):numberOfFs;
//Calculates the total value of each letter grade category.
aScore = (grade >= 90)?(aScore += grade):aScore;
bScore = (grade >= 80 && grade < 90)?(bScore += grade):bScore;
cScore = (grade >= 70 && grade < 80)?(cScore += grade):cScore;
dScore = (grade >= 60 && grade < 70)?(dScore += grade):dScore;
fScore = (grade >= 0 && grade < 60)?(fScore += grade):fScore;
}//Start of Output
studsCount = studsCount - 1;
System.out.println();
System.out.println("Number of students " + studsCount);

System.out.println("------------------------------");

System.out.println("Number of A's " + numberOfAs);
System.out.println("Number of B's " + numberOfBs);
System.out.println("Number of C's " + numberOfCs);
System.out.println("Number of D's " + numberOfDs);
System.out.println("Number of F's " + numberOfFs);

System.out.println("------------------------------");
//Beware of integer math! Had to type to double; otherwise, would get 0.
System.out.println("Percentage A's " + (((double)numberOfAs / (double)studsCount) * 100) + "%");
System.out.println("Percentage B's " + (((double)numberOfBs / (double)studsCount) * 100) + "%");
System.out.println("Percentage C's " + (((double)numberOfCs / (double)studsCount) * 100) + "%");
System.out.println("Percentage D's " + (((double)numberOfDs / (double)studsCount) * 100) + "%");
System.out.println("Percentage F's " + (((double)numberOfFs / (double)studsCount) * 100) + "%");

System.out.println("------------------------------");
//Outputs maxes and mins based on the constraints of >-1 and <101.
if(maxA != -1)
System.out.print("Maximum A= " + maxA);
if(minA != 101)
System.out.println("\tMinimum A= " + minA);
if(maxB != -1) 
System.out.print("Maximum B= " + maxB);
if(minB != 101)
System.out.println("\tMinimum B= " + minB);
if(maxC != -1) 
System.out.print("Maximum C= " + maxC);
if(minC != 101)
System.out.println("\tMinimum C= " + minC); 
if(maxD != -1) 
System.out.print("Maximum D= " + maxD);
if(minD != 101)
System.out.println("\tMinimum D= " + minD); 
if(maxF != -1) 
System.out.print("Maximum F= " + maxF);
if (minF != 101)
System.out.println("\tMinimum F= " + minF);

System.out.println("------------------------------");
System.out.println("Average A score = " + (aScore / numberOfAs));
System.out.println("Average B score = " + (bScore / numberOfBs));
System.out.println("Average C score = " + (cScore / numberOfCs));
System.out.println("Average D score = " + (dScore / numberOfDs));
System.out.println("Average F score = " + (fScore / numberOfFs));

System.out.println("------------------------------");
System.out.println("Average score = " + (total / studsCount));
}
}

btw, the code was tabbed and formatted correctly but I couldn't get the tabbing to work here.

You declared "input" inside of while loop therefore you cannot use it outside at this point. If you need it declare it before while loop, initialize it inside while loop and then you can use it outside

String input = "";
	while(!finished)
	{
		System.out.println("Please enter a list of exam scores (-1 to end input)");
		input = keyboard.next();
		if(input.equals(-1));{
			finished = true;
		}
	}
	String[] studs = input.split("\\s+");
This article has been dead for over six months. Start a new discussion instead.