Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:38:57+00:00 2026-05-27T14:38:57+00:00

I have written the following line of code that reads the data in a

  • 0

I have written the following line of code that reads the data in a text file and stores it in a array. Everything works fine except for when I try and convert the String into int.

Here I am trying to scan the text file line by line and store every word separated by comma in an array. I cant figure out why its throwing NumberFormatException when I convert String into int and assign it to int id.

The following is the code I have written:

public boolean loadPapers(){
    String fileName = "paper.txt"; // file to be opened
    boolean fileOpened = true;
    int id, rank;
    String topic, author, date;

    try {
        Scanner fileData = new Scanner(new File(fileName));

        while(fileData.hasNextLine()){
            String line = fileData.nextLine();

            String[] words = line.split(","); // Separate words from sentence


            // get required parameters for Paper object
            id = Integer.parseInt(words[0]);  // throws numberFormatException
            topic = words[1];
            author = words[2];
            date = words[3];
            rank = Integer.parseInt(words[4]);  // throws numberFormatException

            // create new paper
            Paper entry = new Paper(id, topic, author, date, rank);
            paperList.add(entry);

        } // end while

        fileData.close(); // close file
    }  // end try

    catch (FileNotFoundException e) {
        fileOpened = false;
        swingErrorMessage("ERROR: File couldn't be opened.\nNo papers were loaded.", "File Error");
    } // end catch

    return fileOpened;

} // end loadPapers

The following is the content of text file:

46,Evolutionary Comp,Michael Smith,12/01/10,4
61,Fuzzy Logic and App,John Peterson,13/01/10,3
118,Neural Networks,Arthur London,20/01/10,5 
200,Evolutionary Comp,Scott Jones,30/01/10,1 
210,Fuzzy Logic and App,Joe Wang,01/02/10,4 
12,Evolutionary Comp,Andy Roberts,12/12/12,3 
123,Computer Science,Zhou You,12/12/12,3

The line that program fails on is:

id = Integer.parseInt(words[0]);  // throws numberFormatException

The error message looks like:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at PaperManagerApplication.Frame.loadPapers(Frame.java:409)
at PaperManagerApplication.PaperManager.main(PaperManager.java:16)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:38:58+00:00Added an answer on May 27, 2026 at 2:38 pm

    Your data is not clean, trim it first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a code that reads from a file named network.dat The code
I have a Java program that reads some text from a file, line by
I have written a following code to get just the file name without extension
I have written the following simple C code, which compiles and runs fine. However
I have a line of code written in C# that I need to convert
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
i have written the following code class Program { static void Main(string[] args) {
I have written the following code, CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); report.Load(@C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt); Report works
I have written the following code choice /m Do you want to add another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.