Exception in thread "main" java.lang.NumberFormatException:For input String

this is a code about arrylist. but when I debug it.it metion:Exception in thread "main" java.lang.NumberFormatException:For input String at java.lang.NumberFormatException.forInputString(numberFomatExceptionio java:48)

at java.lang.Integer.parseInt(integer.java:468)

at java.lang.Integer.parseInt(integer.java:497)

at Get.getInt(manerger.java:208)

at LinkList.insertFirst(manager.java:94)

at manager.main(manager.java;20)

this is my code:

import java.io.*;

import java.lang.*;

public class manager

{

public static void main(String args[]) throws IOException

{

LinkList list=new LinkList();

System.out.println("input S can scan the grade\ninput D can delete one entry\ninput U can update the entry\ninput A can add one entry\ninput E can end");

int cr=System.in.read();

switch(cr)

{

case 'A':

list.insertFirst();break;//this is 20 row

case 'S':

System.out.println("input the s");break;

case 'D':

System.out.println("input the d");break;

case 'U':

System.out.println("input the u");break;

}

}

}

class Link

{

public int number;

public String name=new String();

public int chs;

public int eng;

public int math;

public Link next;

public Link(int number,String name, int chs,int eng,int math)

{

this.number=number;

this.name=name;

this.chs=chs;

this.eng=eng;

this.math=math;

}

public Link()

{

this(0,"",0,0,0);

}

public void displayLink()

{

System.out.println(number + " "+name+ " "+chs+ " "+eng+ " "+math+ " ");

}

}

class LinkList

{

public Link first;

public LinkList()

{

first = null;

}

public boolean isEmpty()

{

return first==null;

}

public void displayList()

{

System.out.println("");

Link current=first;

while(current!=null)

{

current.displayLink();

current=current.next;

}

System.out.println("");

}

public Link insertFirst() throws IOException

{

Get getdata=new Get();

int number=getdata.getInt();//this is 94 row

String name=getdata.getString();

int chs=getdata.getInt();

int eng=getdata.getInt();

int math=getdata.getInt();

Link newLink = new Link(number,name,chs,eng,math);

first=newLink;

return first;

}

public Link find(int key)

{

Link current=first;

while(current.number!=key)

{

if(current.next==null)

return null;

else

current=current.next;

}

return current;

}

public Link update(int key) throws IOException

{

Link current=first;

while(current.number!=key)

{

if(current.next==null)

return null;

else

{

System.out.println("Input the first letter of the subject:");

int c=System.in.read();

Get get=new Get();

switch(c)

{

case 'c':

current.chs=get.getInt();break;

case 'e':

current.eng=get.getInt();break;

case 'm':

current.math=get.getInt();break;

}

}

}

return current;

}

public float average(char key)

{

Link current=first;

float total=0;

float average=0;

float counter=0;

if(current==null)

return 0;

while(current!=null)

{

switch(key)

{

case 'c':

total=current.chs+current.next.chs;break;

case 'e':

total=current.eng+current.next.eng;break;

case 'm':

total=current.math+current.next.math;break;

}

current=current.next.next;

counter++;

}

average=total/counter;

return average;

}

public Link delete(int key)

{

Link current=first;

Link previous=first;

while(current.number!=key)

{

if(current.next==null)

return null;

else

{

previous=current;

current=current.next;

}

}

if(current==first)

first=first.next;

else

previous.next=current.next;

return current;

}

}

class Get

{

public static String getString() throws IOException

{

System.out.println("Input your name:");

InputStreamReader str = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(str);

String s = br.readLine();

return s;

}

public static int getInt() throws IOException

{

System.out.println("Input your data:");

String st = getString();

return Integer.parseInt(st);//this is 208 row

}

}

[4849 byte] By [shuca] at [2007-10-2 15:28:32]
# 1

The line:

return Integer.parseInt(st);//this is 208 row

will throw a NumberFormatException if it cannot convert the value entered. What value are you typing when you type 'your name' as requested.

You could catch the NumberFormatException in the getInt method of the Get class and loop around until you get a string input that can be converted to an int. This will protect your code from incorrect user input.

JimDinosaura at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
It may be that the code in getString() returns a String that ends with a newline. If that is the problem, you can use return (Integer.parseInt(st)).trim();
atmguya at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

> It may be that the code in getString() returns a

> String that ends with a newline. If that is the

> problem, you can use

>

> return (Integer.parseInt(st)).trim();

1. getString will never return a String ending in newline. BufferedReader.readLine strips off the newline.

2. Even if you had a newline, String.trim doesn't trim newlines.

3. You would need to trim the String, not the int:

return (Integer.parseInt(st.trim()));

As JimDinosaur said, you are passing bad data (the value of "st").

In getInt, add this before trying to parse "st":

System.out.println("###"+st+"###");

What does it print?

MLRona at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

DOH. My bad for #1 and #3

> 2. Even if you had a newline, String.trim doesn't

> trim newlines.

On my system, the following code displays 2 and 1.String s = "d\n";

System.out.println(s.length());

System.out.println(+s.trim().length());

atmguya at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5

first thank you four your response, maybe I have found the problem. It'sjust as you said. But I find anthor problem.when I run those code, I enter a letter A,it should wait me input the number,but it didn't do this,

instead of mention the error.I hope you can help me solution this problem. I am new to java.I hope you can speak more particular about this problem. Thanks again

shuca at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

> DOH. My bad for #1 and #3

>

>

> > 2. Even if you had a newline, String.trim

> doesn't

> > trim newlines.

>

> On my system, the following code displays 2 and

> 1.String s = "d\n";

> System.out.println(s.length());

> System.out.println(+s.trim().length());

Sorry. You are correct on this one. The API isn't clear that it will do that, and I didn't actually try it.

MLRona at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 7

> Sorry. You are correct on this one. The API isn't

> clear that it will do that, and I didn't actually try it.

Actually, the 1.4 API mentions it removing anything for which Character.isWhitespace() is true and removing ASCII control characters, too.

The wording changed in the 1.5 API, and seems less clear about it.

MLRona at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 8

> > Sorry. You are correct on this one. The API

> isn't

> > clear that it will do that, and I didn't actually

> try it.

>

> Actually, the 1.4 API mentions it removing anything

> for which Character.isWhitespace() is true and

> removing ASCII control characters, too.

>

> The wording changed in the 1.5 API, and seems less

> clear about it.

I am using 1.4 - 1.5 might behave differently.

atmguya at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 9

> > > Sorry. You are correct on this one. The API

> > isn't

> > > clear that it will do that, and I didn't

> actually

> > try it.

> >

> > Actually, the 1.4 API mentions it removing

> anything

> > for which Character.isWhitespace() is true and

> > removing ASCII control characters, too.

> >

> > The wording changed in the 1.5 API, and seems less

> > clear about it.

>

> I am using 1.4 - 1.5 might behave differently.

I just tried on 1.5. The newline is trimmed on both 1.4 and 1.5 (I tried 1.4 earlier today).

MLRona at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 10
maybe I should give you the original problemI want to give a list which can manage the grade of the studentIt include delete scan add and insert Maybe all you done it in javacan you help me
shuca at 2007-7-13 14:49:04 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...