Java Chapter 4 – Console and File Input

4.1  Console Input

A console program can accept input from the keyboard to allow the user to enter variable values during runtime.  The easiest way to get console input in Java is to use the Scanner class.  A scanner looks for tokens in the input.  A token is a series of characters that ends with whitespace (space, tab, carriage return, or end of file).  You need to put import java.util.*; at the top of the program. 

myinput.java

import java.util.*;

public class myinput
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);

      System.out.println("Enter your name: ");
      String Name = in.nextLine();

      System.out.println("Your name is " + Name);
   }
}

Output

Enter your name:
David

Your name is David

Here are additional console input commands for the primitive variable types:

int Length = in.nextInt();
byte B = in.nextByte();
short Age = in.nextShort();
long Size = in.nextLong();
double Length = in.nextDouble();
char C = in.next().charAt(0);

There is an issue when you switch from inputting a number to a string.  Scanner doesn't read the newline for the number and will skip the string input.  Here is a simple fix:

	Scanner in = new Scanner(System.in);
	System.out.println("Enter an integer: ");
	int A = in.nextInt();
	System.out.println("Enter a string: ");
	in.nextLine();    // fix problem with Java not reading newline on integer input
	String B = in.nextLine();


The Scanner class also has a some boolean methods shown below.

hasnext.java

import java.util.*;
public class hasnext
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);

      System.out.println("Enter a number: ");

      if (in.hasNextInt())
         System.out.println("You entered an integer");
      else if (in.hasNextDouble())
         System.out.println("You entered a double");
      else
         System.out.println("You didn't enter a number");
   }
}

Output

Enter a number:
3.14

You entered a double



4.2  File Input

File input must be performed within an exception handler.  Notice that the main method throws IOException.  Below is an example program that reads in 4 integers using the Scanner class.

finput.java

import java.io.*;
import java.util.*;

public class finput
{
   public static void main (String[] args) throws IOException
   {
      File file = new File("data.txt");
      Scanner infile = new Scanner(file);

      int A;
      while (infile.hasNextInt())
      {
         A = infile.nextInt();
         System.out.println(A);
      }
   }
}

data.txt


73 42 69 27
 

Output

73
42
69
27

A scanner object will divide a line of input into tokens (fields) using white space as the delimiter.  The command infile.hasNext() means there is another token in the file.  The next program shows how to read data from a file with mixed text and numbers.  The numbers are read in as a string and then converted to the appropriate type.

finput2.java

import java.io.*;
import java.util.*;

public class finput2
{
   public static void main (String[] args) throws IOException
   {
      File file = new File("data.txt");
      Scanner infile = new Scanner(file);

      String A,B;
      Double C;
      while (infile.hasNext())
      {
         A = infile.next();
         B = infile.next();
         C = Double.parseDouble(B);
         System.out.println(A+" "+C);
      }
   }
}

data.txt


Sean 55.4
Davin 90.4

 

Output

Sean 55.4
Davin 90.4