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

Thread: Using the same scanner multiple times in code help

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using the same scanner multiple times in code help

     
    import java.io.File;
    import java.util.Scanner;
     
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
     
    public class TextAnalysis {
     
    	public static void main(String[] args) throws Exception {
     
    		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     
    		JFileChooser chooser = 
    				new JFileChooser("c:/users/zachary/demo");
     
    		int outcome = chooser.showOpenDialog(null);
     
    		if (outcome == JFileChooser.APPROVE_OPTION) {
     
    			File f = chooser.getSelectedFile();
    			Scanner words = new Scanner(f);
     
     
    			String word = JOptionPane.showInputDialog("Enter word");
    			if (word != null) {
     
    				int count = countThe(words, word);
    				JOptionPane.showMessageDialog(null, "Count: " + count);
    				int allwords = totalwords(words);
    				JOptionPane.showMessageDialog(null,  "Total Words: " + allwords);
     
    			}
     
    		}
     
    	}
     
    	public static int countThe(Scanner words, String word) {
    		int count = 0;
     
    		while (words.hasNext()) {
    			if (words.next().equalsIgnoreCase(word)) {
    				count++;
    			}
    		}
    		return count;
    	}
     
    	public static int totalwords(Scanner words) {
    		int x = 0;
     
    		while (words.hasNext()) {
    			x++;
    		}
     
    		return x;
    	}
     
    }

    Alright, so what I'm trying to do is import a .txt file, use it as a scanner, and then have it search for the number of times the input word is used inside of the .txt file. That part works. I also want it to count the total number of words in the file. That part doesn't work and I can't seem to figure out why, especially as that should be the easier part of this. I'm very new to java, please help me!

    EDIT: It returns "x", so it is reaching that part of the code, but it's like it is already at the end of the text file so there isn't a chance for it to go through the while method. Is there a way I can reset the scanner to start from the beginning of the file again? Or am I completely off about that?
    Last edited by theostorm; September 16th, 2012 at 03:40 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Using the same scanner multiple times in code help

    I think adding a couple of println statements to the code so you can see what it is doing will help you figure out how to make it do what you want it to do.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using the same scanner multiple times in code help

    Quote Originally Posted by jps View Post
    I think adding a couple of println statements to the code so you can see what it is doing will help you figure out how to make it do what you want it to do.
    Thanks for the suggestion. It looks like it must be at the end of the file, because I can get it to print out whatever number I want before the "while (words.hasNext)" statements. I've figured out what it's doing, but now I'm stuck on trying to get it to do what I want to do. Is there a way I can import my text file and then create multiple scanners out of it? Or anyway that I can reset the scanner to the beginning after it goes through each method? Thanks for the help.

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using the same scanner multiple times in code help

    I've decided to take a different approach, this one works so far!
    [code]public class TextAnalysis {

    public static void main(String[] args) throws Exception {

    UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());

    JFileChooser chooser =
    new JFileChooser("c:/users/zachary/demo");

    int outcome = chooser.showOpenDialog(null);

    if (outcome == JFileChooser.APPROVE_OPTION) {

    File f = chooser.getSelectedFile();
    Scanner words = new Scanner(f);
    int count = 0;
    int totalwords = 0;

    String word = JOptionPane.showInputDialog("Enter word");
    if (word != null) {

    while (words.hasNext()) {
    totalwords++;
    if (words.next().equalsIgnoreCase(word)) {
    count++;
    }
    }
    JOptionPane.showMessageDialog(null, "Count: " + count);
    JOptionPane.showMessageDialog(null, "Words: " + totalwords);





    }

    }

    }
    }
    [code]

Similar Threads

  1. Multiple nullpointerexceptions in my code.
    By javastudnt in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 2nd, 2011, 11:30 PM
  2. while(true){ section of code runs 200 times}
    By jack_nutt in forum Java Theory & Questions
    Replies: 7
    Last Post: June 23rd, 2011, 07:06 AM
  3. Scanner code won't work
    By r19ecua in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 21st, 2011, 04:49 PM
  4. Replies: 27
    Last Post: February 17th, 2011, 05:42 PM
  5. Help writing some Scanner code
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 3rd, 2010, 04:27 AM

Tags for this Thread