User & File Input

User Input

import java.util.Scanner // Input user data
  Scanner userInput = new Scanner(System.in); // creates an object of class scanner and names the object userInput
    Variable1 = userInput.next(); // Set Variable1 to the user input data as a string
    Variable2 = userInput.nextInt(); // Set Variable1 to the user input data as an Integer
    Variable3 = userInput.nextDouble(); // Set Variable1 to the user input data as a Double

next
The  method next reads one string of non-whitespace characters delimited by whitespace characters such as blanks or the beginning or end of a line.

  String word1 = keyboard.next(); // alternatively -> word1 = scannerObject.next();
  String word2 = keyboard.next();
     Input line: Hot Potato
     The value of word1 would be Hot, and the value of word2 would be Potato

nextLine
The method nextLine reads an entire line of keyboard input.

String line = keyboard.nextLine();
  // reads in an entire line and places the string that is read into the variable line
  // The end of an input line is indicated by the escape sequence '\n' (enter key is pressed)

String junk = scannerObject.nextLine(); // To get rid of '/n'

 

 

User Input Example
import java.util.Scanner;

public class ScannerDemo2
{
    public static void main(String[] args)
    {
        int n1, n2;
        Scanner scannerObject = new Scanner(System.in);

        System.out.println("Enter two whole numbers");
        System.out.println("seperated by one or more spaces:");

        n1 = scannerObject.nextInt( );
        n2 = scannerObject.nextInt( );
        System.out.println("You entered " + n1 + " and " + n2);

        System.out.println("Next enter two numbers.");
        System.out.println("Decimal points are allowed.");

        double d1, d2;
        d1 = scannerObject.nextDouble( );
        d2 = scannerObject.nextDouble( );
        System.out.println("You entered " + d1 + " and " + d2);

        System.out.println("Next enter two words:");

        String word1 = scannerObject.next( );
        String word2 = scannerObject.next( );
        System.out.println("You entered \"" + word1 + "\" and \"" + word2 + "\"");

        String junk = scannerObject.nextLine( ); //To get rid of '\n'
           // without the above code, the enter key would automatically run with the input of nextLine equal to ""
        System.out.println("Next enter a line of text:");
        String line = scannerObject.nextLine( );
        System.out.println("You entered: \"" + line + "\"");
    }
}

Input Dialog box

	
  String variable = JOptionPane.showInputDialog("Option pane statement"); // name automatically assigned to input
    String input = JOptionPane.showInputDialog("Input integer");
    int variable = Integer.parseInt(input); //parse integer from string

File Input

import java.io.FileInputStream;
import java.io.FileNotFoundException;
// Use nextInt(), nextLine(), etc. to read from the Scanner like reading from the console, except the input comes from the file
Try / Catch Block
Scanner fileIn = null ; // initializes fileIn to empty
try
{
	// Attempt to open the file
	fileIn = new Scanner( new FileInputStream("PathToFile"));
}
catch (FileNotFoundException e)
{
	// If the file could not be found, this code is executed
	// and then the program exits
	System.out.println("File not found.");
	System.exit(0);
}
... Code continues here
Example:

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class TextFileDemo
{
	public static void main(String[] args)
	{
		Scanner fileIn = null;  // Initializes fileIn to an empty object
		try
		{
			// Attempt to open the file
			fileIn = new Scanner(
				new FileInputStream("player.txt"));
		}
		catch (FileNotFoundException e)
		{
			// If the file could not be found, this code is executed
			// and then the program exits
			System.out.println("File not found.");
			System.exit(0);
		}

		// If the program gets here then
		// the file was opened successfully
		int highscore;
		String name;

		System.out.println("Text left to read? " + fileIn.hasNextLine());
		highscore = fileIn.nextInt();
		fileIn.nextLine();	// Read newline left from nextInt()
		name = fileIn.nextLine();

		System.out.println("Name: " + name);
		System.out.println("High score: " + highscore);
		System.out.println("Text left to read? " + fileIn.hasNextLine());

		fileIn.close();
	}
}