0
class liste
{
    int numara;
    String ad;
    String soyad;
    liste sonraki;
} 

public static void main(String[] args) throws IOException
{
    BufferedReader br = new BufferedReader(new FileReader("C:\\bagli.txt"));
    BufferedReader reader = null;  
    String s;
    Scanner klavye = new Scanner(System.in);
    listeler mylist = new listeler();
    while(br.ready())
    {   
        s=br.readLine();

        String[] firstLine = s.split("#");
        liste kayıt = new liste();
        kayıt.numara = Integer.parseInt(firstLine[0].trim());
        kayıt.ad =  firstLine[1].trim();
        kayıt.soyad =  firstLine[2].trim();
        mylist.ekle(kayıt);
    }

I get the following error

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

on the following line:

kayıt.numara = Integer.parseInt(firstLine[0].trim());

What should be the correct code ? bagli.txt in

33#ahmet#korkusuz

44#hanife#demir

66#murat#tok
8
  • 1
    what is listeler and why aren't you using List<MyElement> list = new ArrayList<MyElement>(); to store a list of elements? Commented Oct 10, 2014 at 11:52
  • 3
    It would appear that firstLine[0] has some non-printable character in it (since "33" can, of course, be parsed -- so there's something we can't see in the exception message). Use a debugger to single-step through the code to that line and see what exactly the string in firstLine[0] is, so you know what characters to remove from it. Commented Oct 10, 2014 at 11:54
  • why don't you print and check the value of firstLine[0].trim() Commented Oct 10, 2014 at 11:55
  • I suspect that the number in the file is "33" with extra quotes and not just 33 without quotes. Commented Oct 10, 2014 at 11:56
  • 1
    @Davio: Good thought, but the quotes are added by the exception message. If you try to parse it with quotes, you get Exception in thread "main" java.lang.NumberFormatException: For input string: ""33"" Commented Oct 10, 2014 at 11:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.