How to read ints from text file in Java

1 Answer

0 votes
package javaapplication1;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class JavaApplication1 {
  
    public static void main(String[] args) {
        try
        {   
            Scanner s = new Scanner(new File("d:\\data.txt"));
        
	    while(s.hasNextInt())
	    	System.out.println(s.nextInt());
	}
	catch(IOException e)
	{	
            System.out.println(e);
	}
    }
}
    
/*
run:
   
12
34
873
3
1917

*/

 



answered Jun 30, 2017 by avibootz
edited Jun 30, 2017 by avibootz

Related questions

1 answer 173 views
1 answer 252 views
1 answer 172 views
1 answer 258 views
1 answer 190 views
190 views asked Mar 8, 2015 by avibootz
1 answer 108 views
...