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
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    what is `listeler` and why aren't you using `List list = new ArrayList();` to store a list of elements? – EpicPandaForce Oct 10 '14 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. – T.J. Crowder Oct 10 '14 at 11:54
  • why don't you print and check the value of `firstLine[0].trim()` – amit bhardwaj Oct 10 '14 at 11:55
  • I suspect that the number in the file is `"33"` with extra quotes and not just `33` without quotes. – Davio Oct 10 '14 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""` – T.J. Crowder Oct 10 '14 at 11:58
  • I have no clue. The code seems fine, but its not working and it doesn't seem like a logical error. – rert588 Oct 10 '14 at 12:21
  • Try reading file with scanner class. I know that's probably not the problem but it doesn't hurt to try. – rert588 Oct 10 '14 at 12:28
  • This appears to be the same problem as listed here: https://stackoverflow.com/questions/16135123/java-lang-numberformatexception-for-input-string-22 –  Jan 13 '15 at 19:29

0 Answers0