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: Reuse same Scanner to read another file

Threaded View

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reuse same Scanner to read another file

    Hi everyone
    thanks in advance for patience and taking the time to read my code
    I am trying to get one Scanner to read from 2 files i.e.
    Read from file1 then count contents make count the size of arr1 (size1), fill arr1 with file1
    Reset Scanner and repeat for file2 and arr2
    Do I need another scanner?
    If I do what is the scanner.reset() for?

    Here is my code so far
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
     
    public class FindInArray{
     
    	public static void main(String [] args){
     
    	int [] arr1 = new int[0];
    	int [] arr2 = new int[0];
    	int size1 = 0;
    	int size2 = 0;
    	Scanner sc = new Scanner(System.in);
    	System.out.println("Enter the filename");
    	String file1 = sc.nextLine();
    	System.out.println("Enter another filename");
    	String file2 = sc.nextLine();
    	try{
    		Scanner ns = new Scanner(new File(file1));
    		while(ns.hasNextInt()&&ns.nextInt()!= -1){
    			size1++;
    		}
    		ns.reset();
    		arr1 = new int[size1];
    		System.out.println(size1);
    		int x = 0;
    		while(x<arr1.length&&ns.nextInt()!= -1){
    			arr1[x] = ns.nextInt();
    			x++;
    		}
    		ns.reset();
    		x = 0;
    		ns = Scanner(new File(file2));
    		while(ns.hasNextInt()&&ns.nextInt()!= -1){
    			size2++;
    		}
    		arr2 = new int[size2];
    		while(x<arr2.length&&ns.nextInt()!= -1){
    			arr2[x] = ns.nextInt();
    			x++;
    		}
    	}
    	catch(FileNotFoundException e) {
    			System.out.println("That file was not found. Program terminating...");
    			e.printStackTrace();
    	}
    Last edited by pbrockway2; April 1st, 2013 at 05:47 PM. Reason: code tags added


Similar Threads

  1. Using for loop to read in data from text file using Scanner class
    By Scippi in forum Loops & Control Statements
    Replies: 23
    Last Post: February 26th, 2013, 10:53 PM
  2. Replies: 1
    Last Post: January 16th, 2013, 08:55 AM
  3. Can scanner read the specified line?
    By ice in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 28th, 2011, 09:35 AM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM