import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /** * This class contains methods that read input from the keyboard, and * from a text file. */ public class Input { /** * Some sample code that reads two integers from the keyboard and * prints them out. It will crash if the user doesn't behave. */ public static void readTwoInts() { System.out.println("Please enter two integers:"); Scanner input = new Scanner(System.in); // Create scanner reading from keyboard int a = input.nextInt(); // Read an integer System.out.println("Got it -- I read the "+a+" you entered"); int b = input.nextInt(); // Read another one System.out.println("Thanks. Got the "+b+" as well."); } /** * Asks the user some questions, then suggests a user name for them. */ public void suggestUsername() { System.out.println("Please enter your first and last names:"); Scanner input = new Scanner(System.in); // Create a Scanner, reading from keyboard String firstName = input.next(); // Read two "words" from the input, String lastName = input.next(); // and store them in variables. System.out.println("Thanks, "+firstName+". What's your favorite number?"); int fav = input.nextInt(); // Now that we've got the user's inputs, we'll do some operations // to create a user name for them String name = firstName.substring(0,1) + lastName; System.out.println("Your user name is "+name+(2*fav)); } /** * We wrote a version of this in class. I'm putting it here so you have another * example of some working code to look at as you implement the other methods. * * @param filename The name of the file to read from. */ public static void printAllLines(String filename) throws FileNotFoundException { File theFile = new File(filename); // Create File object corresponding to the file if (theFile.exists()) { Scanner scan = new Scanner(theFile); // Scanner reads from file! while(scan.hasNext()) { String line = scan.nextLine(); System.out.println(line); } } else { System.out.println("The file doesn't exist!"); } } /** * This method asks the user for a filename, then calls the * printAllLines method above. Doing it this way means we don't need * to have BlueJ pop up a dialog box for us to get the filename. */ public static void printContents() throws FileNotFoundException { Scanner scan = new Scanner(System.in); System.out.println("Enter a filename:"); String filename = scan.next(); printAllLines(filename); } /** * Prints only the values greater than zero. This is basically the method above * but now with an if statement in the body of the loop, and reading integers * rather than strings. */ public void printPositiveValues(String fileName) throws FileNotFoundException { int data; Scanner input; input = new Scanner(new File(fileName)); while(input.hasNextInt()) { data = input.nextInt(); if (data > 0) { System.out.println(data); } } } /** * Sums values until we encounter a negative number. This assumes that * there's at least one number in the file, though it still works even if * the first (and maybe only) value is negative. */ public int sumUntilNegative(String fileName) throws FileNotFoundException { int total; int data; total = 0; Scanner input = new Scanner(new File(fileName)); data = 0; // "Trick" it to get the loop started while(data >= 0 && input.hasNextInt()) { data = input.nextInt(); // Might be negative... if (data > 0) { // If it's not, add to total total = total + data; } } return total; } /** * Prints information each time it finds two identical neighboring values. This * requires keeping TWO values from the sequence -- current and previous. */ public void findIdenticalNeighbors(String fileName) throws FileNotFoundException { int current; // The most recently read value int previous; // The one before that Scanner input = new Scanner(new File(fileName)); previous = input.nextInt(); while(input.hasNext()) { current = input.nextInt(); if (current == previous) { System.out.println("Two in a row: "+current); } previous = current; } } }