Results 1 to 6 of 6
  1. #1
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Only read ints from text file?

    I am trying to write a program which outputs ints from a text file to the screen, but the text file also contains strings.. How do I go about this?

    Here is what I have:

    Java Code:
    import java.io.FileReader;
    import java.util.Scanner;
    
    class decoder{
    	public static void main(String [] args){
    		try{
    			Scanner scan = new Scanner(new FileReader("encodedtextfile.txt"));
    			while(scan.hasNext()){
    				System.out.println(scan.nextInt());
    			}
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    }
    This only outputs ints until a string is encountered, at which point following exception is produced:

    java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at decoder.main(decode.java:9)

    I assume this is a result of passing a string through the nextInt method..

    What changes can I make to my code so that only ints are output to the screen?

    Thanks in advance.

    Afterwards, I would also like to do the same for the strings (print only the strings to the screen, and no ints). As there is no nextString method, or any method to that effect (that I know of), I would appreciated if someone could provide some advice on that.
    Last edited by cherrychives; 04-25-2012 at 06:58 AM.

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Only read ints from text file?

    Just off the top of my head:
    Put a separate try/catch in the while loop or just outside of it to catch an InputMismatchException (i.e. catch (InputMismatchException ime) and change the original catch to an IOExeception.

  3. #3
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Only read ints from text file?

    Thanks, shall.

    I tried what you said:

    Java Code:
    import java.io.FileReader;
    import java.util.Scanner;
    
    class decoder{
    	public static void main(String [] args){
    		try{
    			Scanner scan = new Scanner(new FileReader("encodedtextfile.txt"));
    			while(scan.hasNext()){
    			try{
    				System.out.println(scan.nextInt());
    			}catch(InputMismatchException ime){
    			ime.printStackTrace();
    			}
    			}
    		}catch(IOException e){
    			e.printStackTrace();
    		}
    	}
    }
    But now it won't compile at all:

    decode.java:11: cannot find symbol
    symbol : class InputMismatchException
    location: class decoder
    }catch(InputMismatchException ime){

    decode.java:15: cannot find symbol
    symbol : class IOException
    location: class decoder
    }catch(IOException e){

    2 errors
    Last edited by cherrychives; 04-25-2012 at 08:09 AM.

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Only read ints from text file?

    You need to include:
    import java.io.IOException;
    import java.util.InputMismatchException;

    Also, you want to call scan.next() in the InputMismatchException, so the scanner will step over the string.

    The InputMismatchException variable name can be an reasonable name. If you want to, you can change it to e (I realize I gave the suggestion of ime).

    A good IDE will point this out for you and will give you the option having it correct it (e.g. NetBeans IDE).

  5. #5
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Only read ints from text file?

    When I said "A good IDE..", I meant the imports and not the variable name.

  6. #6
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Only read ints from text file?

    Thank you again, shall. That worked perfectly.

Similar Threads

  1. Longs are read a Ints
    By nhmllr in forum New To Java
    Replies: 6
    Last Post: 10-02-2011, 03:31 AM
  2. Replies: 2
    Last Post: 03-25-2011, 02:36 PM
  3. Read text file
    By Sean04 in forum New To Java
    Replies: 18
    Last Post: 06-16-2010, 01:43 AM
  4. Read from a text file?
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 04:42 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •