How to Use a Scanner for Multiple Words in Java

Written by corey bieber
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use a Scanner for Multiple Words in Java
The Scanner class in Java is a simple way to enter data into your application. (Ablestock.com/AbleStock.com/Getty Images)

The Scanner class in Java is commonly used to read in data for use by the application. By constructing Scanner to read in data from the console, programmers can develop very simple command line user interfaces. Scanner's constructor accepts a variety of arguments, including file types, input streams, strings and byte channels. To create a simple user interface that accepts multiple words, we can use the class InputStream.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Create a new Java class called "ScanWords."

    public class ScanWords { }

  2. 2

    Place a main method in the ScanWords class.

    public static void main(String[] args) { }

  3. 3

    Import the Scanner and ArrayList packages from java.utils by placing the following code at the top of your class.

    import java.util.Scanner;

    import java.util.ArrayList;

  4. 4

    Create a new Scanner object to read in the words. You will want to construct the Scanner with the argument "System.in." This tells the Scanner that it will be reading in data through an input stream.

    Scanner input = new Scanner(System.in);

  5. 5

    Create an ArrayList to hold the words once they have been entered.

    ArrayList<String> scanned = new ArrayList();

  6. 6

    Add the following line of code to allow the end user to enter the number of words he intends to enter.

    System.out.println("How many words would you like to enter?");

       int num = input.nextInt();
    
  7. 7

    Add the following "for" loop to allow the end user to enter the words.

    for(int i = 0; i < num; i++){

           System.out.println("Enter word " + (i+1));
    
           scanned.add(input.next());
    
       }
    
  8. 8

    Add another "for" loop to test that the words have been captured and handled by the Scanner.

    System.out.println("You have entered:");

       for(int j = 0; j &lt; scanned.size(); j++){
    
           System.out.println(scanned.get(j));
    
       }
    
  9. 9

    Check your class. It should look like this:

    import java.util.Scanner;

    import java.util.ArrayList;

    public class ScanWords {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

       ArrayList&lt;String&gt; scanned = new ArrayList();
    
    
    
       System.out.println("How many words would you like to enter?");
    
       int num = input.nextInt();
    
    
    
       for(int i = 0; i &lt; num; i++){
    
           System.out.println("Enter word " + (i+1));
    
           scanned.add(input.next());
    
       }
    
    
    
       System.out.println("You have entered:");
    
       for(int j = 0; j &lt; scanned.size(); j++){
    
           System.out.println(scanned.get(j));
    
       }
    

    }

    }

  10. 10

    Run the class.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.