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 9215233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:09:32+00:00 2026-06-18T02:09:32+00:00

I am having an extremely difficult time splitting each line from the text file

  • 0

I am having an extremely difficult time splitting each line from the text file into an array of strings and using it like I need to. The split() seems to work okay. I end up having an array of strings, where the first slot of the strings array contains a number that I need to parse as an int, to continue my code. For some reason, I keep getting the error shown below that I can’t seem to figure out.

My goal is it to simply store every line of the text file that contains letters, in an array, and parse the number which is going to be the first value of the line, as an integer. Once I accomplish this, I need to be able to use every preceding group of letters independently, so I am trying to get those in an array as well.

I appreciate any help with this.

Many thanks in advance!

NOTE: numGrammars is the first number shown on the first line of the text file.

My Code

    numGrammars = Integer.parseInt(fin.next());
    System.out.println("Num Grammars:" + numGrammars);


    for(int v=0; v < numGrammars; v++){
       int numVariables = Integer.parseInt(fin.next());
       System.out.printf("numVariables: %s", numVariables);

        for(int z=0; z < numVariables; z++){
            //reads in variable line
            String line = fin.nextLine();
            String[] strings = line.split(" ");

            for(int m=0; m < strings.length; m++){
               int numRules = Integer.parseInt(strings[0]);
               //All other array slots in strings array should be groups of letters on group per slot...

            }   
        }
    }

Console Output

Num Grammars:2
numVariables: 3Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Methods.readFile(Methods.java:34)
at Main.main(Main.java:12)

Text file I am reading from:

1
3
2 S AB BB
3 A BB a b
2 B b c
  • 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-06-18T02:09:33+00:00Added an answer on June 18, 2026 at 2:09 am

    Only use fin.nextLine(). After the call to next(), the cursor is right after the numVariables value 3, but before the newline. When you call nextLine() after that, it returns everything between the cursor and the newline, which is an empty string! Using nextLine() each time always places the cursor after the newline, and everything is OK.

    numGrammars = Integer.parseInt(fin.nextLine());
    System.out.println("Num Grammars:" + numGrammars);
    
    
    for(int v=0; v < numGrammars; v++){
       int numVariables = Integer.parseInt(fin.nextLine());
       System.out.printf("numVariables: %s", numVariables);
    
        for(int z=0; z < numVariables; z++){
            //reads in variable line
            String line = fin.nextLine();
            String[] strings = line.split(" ");
    
            for(int m=0; m < strings.length; m++){
               int numRules = Integer.parseInt(strings[0]);
               //All other array slots in strings array should be groups of letters on group per slot...
    
            }   
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an extremely difficult time getting a private method with arguments to be
I am having an extremely difficult time getting this to work. The applet is
I've been having an extremely difficult time trying to find an answer for this.
I'm having an extremely difficult time trying figure how to implemented some type of
I'm having an extremely difficulty time getting the flowplayer to show up and the
I'm having an extremely difficult day today. I've installed the latest version on Titanium
Hell I have been having an extremely tough time installing libtorrent and am unsure
I am having a lot of trouble with a text file given to me
Having used storyboards for a while now I have found them extremely useful however,
Having trouble with each function... Will try to explain by example... In my code,

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.