In the code I have sent, there are 2 methods I am not able to code together.
Question:
In the code I have sent, there are 2 methods I am not able to code together. If it is okay, could you please check the ones I have already coded because when I put them into my schools autograder, there are errors. I would also like some help coding the methods countLines and countTokens.
package cp213;
import java.util.Scanner; import java.io.*;
/** * Class to demonstrate the use of Scanner with a keyboard and File objects. * * @author your name here * @version 2022-01-08 */ public class ScannerTest { public static void main(String args[]) throws FileNotFoundException { Scanner source = new Scanner(System.in); File file = new File("src/cp213/ScannerTest.java"); Scanner s = new Scanner(file); int count = countLines(source); System.out.println("The total is " + count + " "); readNumbers(source); s.close(); source.close(); } /** * Count lines in the scanned file. * * @param source Scanner to process * @return number of lines in scanned file */ public static int countLines(final Scanner source) { try { File inputFile = new File(source); } int count = 0;
/** * Count tokens in the scanned object. * * @param source Scanner to process * @return number of tokens in scanned object */ return count; } public static int countTokens(final Scanner source) {
/** * Ask for and total integers from the keyboard. * * @param source Scanner to process * @return total of positive integers entered from keyboard */ public static int readNumbers(final Scanner keyboard) { System.out.println("Enter a series of integers. Press 'x' to quit."); int tokens = 0; boolean valid = true; while (valid) { if (keyboard.hasNextInt()) { tokens += keyboard.nextInt(); } else if (keyboard.hasNext()) { String var = keyboard.next(); if (var.equals("x")) { valid = false; } else { System.out.println("This input is invalid. Try again please!"); } } } return tokens; } }
Applying Communication Theory For Professional Life A Practical Introduction
ISBN: 9781506315478
4th Edition
Authors: Marianne Dainton, Elaine D. Zelley