简体   繁体   中英

Java Scanner Class ( System.in)

I have the below code that is not reading or infinitely looping when a user inputs text using System.in. If I hard code the text into the Scanner variable it works fine so I am not sure what is wrong with the System.in portion of this code. Any help is appreciated.

import java.util.Scanner; // needed to use the Scanner class 

public class HW2 { 

    static Scanner in = new Scanner(System.in); 


    public static void main(String [] args) { 

        System.out.println("Enter your line here"); 


        int the =0; 
        int and =0; 
        int is = 0; 
        int was =0; 
        int noword =0; 

        while (in.hasNext()){ 
            String word = in.next(); 

            if (word.equals("the")){ 
                the++; 
            } 
            else if( word.equals("and")){ 
                and ++; 
            } 
            else if (word.equals("is")){ 
                is++; 
            } 
            else if (word.equals("was")){ 
                was++; 
            } 
            else noword++; 

        } 

        System.out.println("The number of occurrences of the was"+ the); 
        System.out.println("The number of occurrences of and was"+ and); 
        System.out.println("The number of occurrences of is was"+ is); 
        System.out.println("The number of occurrences of was was"+ was); 


    } 
}

As System.in is always available while the program is running unless you close it. It will never exit the while loop. So you could add else if (word.equals("exit")) { break; } else if (word.equals("exit")) { break; } . This way, whenever you type 'exit' it will close the while loop and execute the code AFTER the while loop.

As has been mentioned, a Scanner attached to System.in will block while looking for more input. One way to approach this would be to read a single line in from the scanner, tokenize it, and then loop through the words that way. That would look something like this:

//...
String line = in.nextLine(); // Scanner will block waiting for user to hit enter
for (String word : line.split(" ")){
    if (word.equals("the")) {
        the++;
    }
//...

You can always substitute one loop structure (for, while, do-while) for another. They all do the same thing, just with different syntax to make one a bit simpler to use than others depending on the circumstances. So if you want to use a while loop, you can do something like this:

// ...
String line = in.nextLine();
String[] tokens = line.split(" ");
int i = 0;
while (i < tokens.length){
    String word = tokens[i];
    if (word.equals("the")) {
        the++;
    }
// ...
    i++;
} // end of the while loop

However, I'm of the opinion that a for loop is cleaner in the case of looping over a known set of data. While loops are better when you have an unknown dataset, but a known exit condition.

Depends, do you want to just read 1 line of text and then count the words individually?

Because is you want only one line you could take the input string using the Scanner library and split the string into individual words and apply the if-statement then. Something like:

   public static void main(String [] args) { 

    System.out.println("Enter your line here"); 

    int the =0; 
    int and =0; 
    int is = 0; 
    int was =0; 
    int noword =0; 

    String input = in.nextLine();

    String words[] = input.split(" ");

    for (String s : words) {

        if (s.equals("the")){ 
            the++; 
        } else if( s.equals("and")){ 
            and++; 
        } else if (s.equals("is")){ 
            is++; 
        } else if (s.equals("was")){ 
            was++; 
        } else {
            noword++;
        }

    }

    System.out.println("The number of occurrences of the was: "+ the); 
    System.out.println("The number of occurrences of and was: "+ and); 
    System.out.println("The number of occurrences of is was: "+ is); 
    System.out.println("The number of occurrences of was was: "+ was); 


} 

This way you won't need a while loop at all. So it's more processor and memory efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM