Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: reading string in from text file

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default reading string in from text file

    Hey Guys... im getting an error could you help?

     

    Exception in thread "main" java.lang.NumberFormatException: For input string: "bogus"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at lab10.main(lab10.java:47)




    Heres my .txt file
    100
    200
    bogus
    300
    150
    weasel
    400
    600
    250
    3.5
    800
    50
    500
    450
    600
    550


    import java.util.*;
    import java.io.*;
    public class lab10
    {
     
    	public static void main(String [] args)
    	{
    		Scanner inScan;
    		Scanner fScan = null;
    		inScan = new Scanner (System.in);
    		String nextItem;
    		int nextInt = 0;
    		int i = 0;
     
     
    		int [] A = new int [5];
     
    		boolean check = true; 
     
    	while (check = true){
    	      System.out.println("Please enter the file to read from: ");
    		  String fName = inScan.nextLine();
     
     
     
     
     
     
    		try {
     
     
    			fScan = new Scanner (new File(fName));
     
    			while (fScan.hasNextLine())
    			{
     
    				nextItem = fScan.nextLine(); // Stores first number into nextItem
     
     
    				// nextLine READS STRINGS
    	// something going on here.... ^ v reason for this
     
    				// TAKES String and makes it INT!  
    				nextInt = Integer.parseInt(nextItem);
     
     
     
     
     
     
    				A[i] = nextInt; // array 
     
     
     
     
     
    			    i++;  // increments the counter 
     
     
     
     
    			   // System.out.println( " is not an integer -- ignored");
     
                }
     
     
     
     
     
     
     
    			System.out.println("Here are your " + i + " items:");
    			for (int j = 0; j < i; j++)
    			{
    				System.out.println(A[j] + " ");
     
    			}
     
     
     
    		} catch (FileNotFoundException e) {
     
    			System.out.println("Your file is invalid ");
     
     
     
     
     
    		}
     
     
     
    	}// end while
     
     
    	}
     
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: reading string in from text file

    You are trying to convert each line of the file into an integer,
    nextInt = Integer.parseInt(nextItem);

    So when you hit a text string containing non numeric characters you are going to have a problem. The method parseInt() throws an exception NumberFormatException when it tries to parse a string that contains non numeric data, you need to find a way of handling this. Either use a try/catch block to catch the exception and deal with it appropriately or parse the strings yousrself beforehand to check if they are inetegers or not.

    Integer (Java Platform SE 6)

    Chris

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: reading string in from text file

    Hello basketball8533.

    Welcome to the Java Programming Forums.

    The clue is in the exception. java.lang.NumberFormatException: For input string: "bogus"

    The String "bogus" in the txt file is causing the issue because you are adding the values to an integer array.
    You cannot add Strings to an integer array.

    I notice there is also a double value in the txt file.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: reading string in from text file

    Quote Originally Posted by Freaky Chris View Post
    You are trying to convert each line of the file into an integer,
    nextInt = Integer.parseInt(nextItem);

    So when you hit a text string containing non numeric characters you are going to have a problem. The method parseInt() throws an exception NumberFormatException when it tries to parse a string that contains non numeric data, you need to find a way of handling this. Either use a try/catch block to catch the exception and deal with it appropriately or parse the strings yousrself beforehand to check if they are inetegers or not.

    Integer (Java Platform SE 6)

    Chris
    Ah you just beat me to it!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: reading string in from text file

    nextItem = fScan.nextLine(); // Stores first number into nextItem


    // nextLine READS STRINGS
    // something going on here.... ^ v reason for this

    // TAKES String and makes it INT!
    nextInt = Integer.parseInt(nextItem);

    You could just have

    int x = fScan.nextInt();

    However, bogus is a String.

    How can you turn it into an int?

Similar Threads

  1. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 11:56 AM
  2. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  3. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 06:16 PM