QUOTE
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:687)
at If_Else2.Grade(If_Else2.java:74)
at If_Else2.main(If_Else2.java:9)
Process completed.
at java.lang.String.charAt(String.java:687)
at If_Else2.Grade(If_Else2.java:74)
at If_Else2.main(If_Else2.java:9)
Process completed.
Here is the source code (created in JCreator LE).
CODE
//This is my first attempt at an if/else program.
import java.util.Scanner;
public class If_Else2
{
public static void main( String args[])
{
Grade();
}
public static void Grade()
{
System.out.println("*********Grade Calculation*********");
System.out.println();
Scanner input = new Scanner( System.in );
int score;
char grade;
char cont;
boolean done = false;
boolean ans = false;
while (done == false)
{
System.out.print("Enter your test score: ");
score = input.nextInt();
//Main Calculations
if (score >= 90)
{
grade = 'A';
}
else if (score >= 80)
{
grade = 'B';
}
else if (score >= 75)
{
grade = 'C';
}
else if (score >= 70)
{
grade = 'D';
}
else
{
grade = 'F';
}
//Output
System.out.println();
if (score > 9000)
{
System.out.println("YOUR SCORE IS OVER NIIIINE THOUSSSAAAAAANNNDDDDD!!!!!!!!!");
}
else if (score > 100)
{
System.out.println("You either have a good teacher, or you're a suck-up,\nbecause you are not supposed to score that high.");
}
else if (score >= 0)
{
System.out.println("You earned a(n) " +grade +" on your test.");
}
else
{
System.out.println("You must have a bad teacher, because you cannot score that low\nno matter how dumb you are.");
}
while (ans == false)
{
System.out.print("\nDo you want to enter another score? Enter \"y\" for \"yes\" or \"n\" for \"no\": ");
//cont = input.nextChar();
cont = input.nextLine().charAt(0);
//cont = charAt(0);
if (cont == 'y' || cont == 'Y')
{
done = false;
ans = true;
}
else if (cont == 'n' || cont == 'N')
{
done = true;
ans = true;
}
else
{
System.out.println("Please Enter \"y\" for \"yes\" or \"n\" for \"no\".");
}
}
System.out.println();
}
}
}
import java.util.Scanner;
public class If_Else2
{
public static void main( String args[])
{
Grade();
}
public static void Grade()
{
System.out.println("*********Grade Calculation*********");
System.out.println();
Scanner input = new Scanner( System.in );
int score;
char grade;
char cont;
boolean done = false;
boolean ans = false;
while (done == false)
{
System.out.print("Enter your test score: ");
score = input.nextInt();
//Main Calculations
if (score >= 90)
{
grade = 'A';
}
else if (score >= 80)
{
grade = 'B';
}
else if (score >= 75)
{
grade = 'C';
}
else if (score >= 70)
{
grade = 'D';
}
else
{
grade = 'F';
}
//Output
System.out.println();
if (score > 9000)
{
System.out.println("YOUR SCORE IS OVER NIIIINE THOUSSSAAAAAANNNDDDDD!!!!!!!!!");
}
else if (score > 100)
{
System.out.println("You either have a good teacher, or you're a suck-up,\nbecause you are not supposed to score that high.");
}
else if (score >= 0)
{
System.out.println("You earned a(n) " +grade +" on your test.");
}
else
{
System.out.println("You must have a bad teacher, because you cannot score that low\nno matter how dumb you are.");
}
while (ans == false)
{
System.out.print("\nDo you want to enter another score? Enter \"y\" for \"yes\" or \"n\" for \"no\": ");
//cont = input.nextChar();
cont = input.nextLine().charAt(0);
//cont = charAt(0);
if (cont == 'y' || cont == 'Y')
{
done = false;
ans = true;
}
else if (cont == 'n' || cont == 'N')
{
done = true;
ans = true;
}
else
{
System.out.println("Please Enter \"y\" for \"yes\" or \"n\" for \"no\".");
}
}
System.out.println();
}
}
}
Here's the section I'm having trouble with:
CODE
while (ans == false)
{
System.out.print("\nDo you want to enter another score? Enter \"y\" for \"yes\" or \"n\" for \"no\": ");
//cont = input.nextChar();
cont = input.nextLine().charAt(0);
//cont = charAt(0);
if (cont == 'y' || cont == 'Y')
{
done = false;
ans = true;
}
else if (cont == 'n' || cont == 'N')
{
done = true;
ans = true;
}
else
{
System.out.println("Please Enter \"y\" for \"yes\" or \"n\" for \"no\".");
}
}
{
System.out.print("\nDo you want to enter another score? Enter \"y\" for \"yes\" or \"n\" for \"no\": ");
//cont = input.nextChar();
cont = input.nextLine().charAt(0);
//cont = charAt(0);
if (cont == 'y' || cont == 'Y')
{
done = false;
ans = true;
}
else if (cont == 'n' || cont == 'N')
{
done = true;
ans = true;
}
else
{
System.out.println("Please Enter \"y\" for \"yes\" or \"n\" for \"no\".");
}
}
As you can see, I've commented out the "cont" twice as a result of trial and error.