!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ID: 3602710 • Letter: #
Question
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Java!!!!!!!!!!!!!!!
Provide a constructor that takes a String as a parameter
Provide four methods:
hasNext(), which takes no parameters and returns a boolean
next(), which takes no parameters and returns a String
hasNextInt(), which takes no parameters and returns a boolean
nextInt(), which takes no parameters and returns an int
Not use Scanner in its implementation!
A MyScanner should behave just like a Scanner would if you only used the Scanner's String constructor and its hasNext(), next(), hasNextInt(), and nextInt() methods. Be sure that you understand exactly how Scanners behave, including the circumstances in which exceptions are thrown. Don't simply assume you know how those methods interact. For example, hasNextInt() doesn't report whether there is an integer token somewherein the Scanner; it reports whether the next token can be parsed as an int by Integer.parseInt().
Explanation / Answer
import java.util.Scanner;
import java.io.*;
public class NumericInput
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int integer;
long longInteger;
float realNumber;
double doubleReal;
String string1;
String string2;
System.out.println("Enter an integer, a long integer, "
+ "a floating-point ");
System.out.println("number, another floating-point number, "
+ "and a string.");
System.out.println("Separate each with a blank or return.");
integer = in.nextInt();
longInteger = in.nextLong();
realNumber = in.nextFloat();
doubleReal = in.nextDouble();
string1 = in.nextLine();
System.out.println("Now enter another value.");
string2 = in.next();
System.out.println("Here is what you entered: ");
System.out.println(integer + " " + longInteger + " " + realNumber +
" " + doubleReal + " " + string1 +
" and " + string2);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.