Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help Forums Windows Startup Programs Database Virus, Spyware, and Malware Removal Guides Computer Tutorials Uninstall Database File Database Computer Glossary Computer Resources
 

Welcome Guest ( Log In | Click here to Register a free account now! )



Register a free account to unlock additional features at BleepingComputer.com
Welcome to Bleeping Computer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.
Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

 
Reply to this topicStart new topic
> I need a little help (Java)
MadDawg
post May 23 2009, 11:17 PM
Post #1


Forum Regular
***

Group: Members
Posts: 212
Joined: 9-July 07
From: Houston, TX
Member No.: 142,377



I'm having a little trouble with my java application. I want it to repeat when the user enters "y" or "Y" and exit when the user enters "n" or "N". The app compiles, but when it's supposed to repeat/exit, I get this output:

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.


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();
}
}
}


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\".");
}
}


As you can see, I've commented out the "cont" twice as a result of trial and error.


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


Go to the top of the page
 
+Quote Post
groovicus
post May 24 2009, 10:02 AM
Post #2


Hail Groovicus!
******

Group: Site Admin
Posts: 7,900
Joined: 5-June 04
From: Centerville, SD
Member No.: 689



The error is telling you that there is nothing being pointed to by cont. Are tou reading in a line from the keyboard, or are you simply reading a char?


--------------------
Never Argue With Stupid People



Microsoft Senior Student Partner
Go to the top of the page
 
+Quote Post
MadDawg
post May 24 2009, 10:34 AM
Post #3


Forum Regular
***

Group: Members
Posts: 212
Joined: 9-July 07
From: Houston, TX
Member No.: 142,377



I want it to read a char (Y/y or N/n).


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


Go to the top of the page
 
+Quote Post
groovicus
post May 24 2009, 12:39 PM
Post #4


Hail Groovicus!
******

Group: Site Admin
Posts: 7,900
Joined: 5-June 04
From: Centerville, SD
Member No.: 689



This code works fine:
CODE
import java.util.Scanner;

public class scanTest {

    public static void main(String[] args) {
        Scanner input = new Scanner( System.in );
        System.out.println(input.nextLine().charAt(0));
    }
}


You need to fire up the debugger and step through your code to try and figure out where the keyboard input is being consumed.


--------------------
Never Argue With Stupid People



Microsoft Senior Student Partner
Go to the top of the page
 
+Quote Post
MadDawg
post May 24 2009, 02:08 PM
Post #5


Forum Regular
***

Group: Members
Posts: 212
Joined: 9-July 07
From: Houston, TX
Member No.: 142,377



It won't allow any input. The error comes up as soon as it asks whether you want to continue or not.


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


Go to the top of the page
 
+Quote Post
MadDawg
post May 25 2009, 01:30 PM
Post #6


Forum Regular
***

Group: Members
Posts: 212
Joined: 9-July 07
From: Houston, TX
Member No.: 142,377



Alright, well thanks for your help groovicus. I'll just program this same app in C++, since it handles letter inputs better. (IMO)


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


Go to the top of the page
 
+Quote Post
jpshortstuff
post May 26 2009, 03:29 AM
Post #7


WhatTheTech Teacher
****

Group: HJT Team
Posts: 465
Joined: 15-June 07
From: UK
Member No.: 136,795



The problem is that after using nextInt(), you are left with input that starts with a newline.

For example, your input will be something like this:
INT\n
CHAR\n


First, you are calling nextInt(), which will leave you with this:
\n
CHAR\n


Now you are calling nextLine(). Since the next character on the input is a newline character, the function returns immediately giving you a string with zero characters in it. You need to flush the rest of that first line away (an extra call to nextLine() should do it).


--------------------
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 8th November 2009 - 07:51 AM


Advertise   |   About Us   |   Terms of Use   |   Privacy Policy   |   Contact Us   |   Site Map   |   Chat   |   Tutorials   |   Uninstall List
Discussion Forums   |   The Computer Glossary   |   Resources   |   RSS Feeds   |   Startups   |   The File Database   |   Virus Removal Guides

© 2003-2009 All Rights Reserved Bleeping Computer LLC.