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 7 of 7

Thread: [SOLVED] Integer values from a mixed textfile in Java using Scanner

  1. #1
    Junior Member
    Join Date
    May 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question [SOLVED] Integer values from a mixed textfile in Java using Scanner

    Hello, I am not sure how to read Integer values from a textfile that has "strings" and "ints" . I want to save strings in an array and int in an array so I can use them later on.
    For example, ask for something and output this element.

    static void readArray(String fileName) throws FileNotFoundException {
     
    Scanner s = new Scanner(new File(fileName));
    ArrayList<String> Array = new ArrayList<String>(); 
    ArrayList<Integer> ArrayInt = new ArrayList<Integer>();
     
    // Here my try..
     
    //int j = 0;
    			while(s.hasNextInt())
    			{
    				ArrayInt.add(s.nextInt()); 
    			}
     
    			Out.println(ArrayInt);
     
    }

    My Output is: [] but there should be int values in it
    Last edited by hiro; May 16th, 2020 at 03:15 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,001
    Thanks
    53
    Thanked 2,445 Times in 2,396 Posts

    Default Re: Integer values from a mixed textfile in Java using Scanner

    how to read Integer values from a textfile that has "strings" and "ints"
    The Scanner class has methods that will tell you what data type the next token to be read is. Look at the hasNext... methods.
    If you only want to read integer values, use the hasNextInt method to test if the next token can be converted to an int value.
    If it can't, use next() to read it and ignore what was read.

    My Output is: [] but there should be int values in it
    The code you posted will never call the nextInt() method if the first token is not an integer.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Integer values from a mixed textfile in Java using Scanner

    while(s.hasNext())
    			{
    				if(s.hasNextInt())
    				ArrayInt.add(s.nextInt());
    				else
    					s.hasNext(); // from here on restart the loop? 
    			}
     
    			Out.println(ArrayInt);

    If I understand it right, there should be values now but output is still []. Maybe I should try has.Next(pattern)?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,001
    Thanks
    53
    Thanked 2,445 Times in 2,396 Posts

    Default Re: Integer values from a mixed textfile in Java using Scanner

    The call to hasNext() does NOT read anything. Use next() to read the non-integer.
    For debugging add a print statement that prints out the values that were skipped.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Integer values from a mixed textfile in Java using Scanner

     
    static void readArray(String fileName) throws FileNotFoundException {
     
    Scanner s = new Scanner(new File(fileName));
    ArrayList<Integer> ArrayInt = new ArrayList<Integer>();
     
    while(s.hasNext())
    			{
    				if(s.hasNextInt())
    				{
    					Out.println("Int: " + s.nextInt());
    					ArrayInt.add(s.nextInt());
    				}
    				else	{ s.next();
    					Out.println("String" + s.next());  
    				}
    			}
     
    			Out.println(ArrayInt);
    			s.close();
    		   }		   
     
    public static void main(String[] args) throws FileNotFoundException {
     
    			readArray("testing.txt");
    }

    I've tried this with a normal String instead of a textfile and it works fine. There seems to be an issue with textfile recognition or with new lines?
    New output is int: 10 and then lots of error messages

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,001
    Thanks
    53
    Thanked 2,445 Times in 2,396 Posts

    Default Re: Integer values from a mixed textfile in Java using Scanner

    if(s.hasNextInt())
    		{
    			Out.println("Int: " + s.nextInt());    //  Reads the int found by hasNextInt
    			ArrayInt.add(s.nextInt());   // This reads the next value.  What is it??
    The hasNextInt only checks the next token. That token could be followed by anything.
    Read the value into a variable, then print it and add it to the list.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    hiro (May 16th, 2020)

  8. #7
    Junior Member
    Join Date
    May 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Integer values from a mixed textfile in Java using Scanner

    Thank you for you patience I finally have it!

Similar Threads

  1. [SOLVED] Problem with returning integer values
    By Spootdude in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 1st, 2018, 03:06 PM
  2. Jtable sorting of integer values
    By aslanali555 in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2014, 08:42 AM
  3. How to make Java determine if a values is an integer or not.
    By MiniatureBeast in forum Object Oriented Programming
    Replies: 5
    Last Post: August 17th, 2011, 11:32 PM
  4. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 09:13 PM
  5. Mixed Fraction JAVA
    By x3iancute in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2010, 09:18 PM

Tags for this Thread