import java.util.Scanner; public class inputParse { public static void main(Stri
ID: 3542786 • Letter: I
Question
import java.util.Scanner;
public class inputParse
{
public static void main(String[] args)
{
String input;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter any numbers or letters: ");
input = scanner.nextLine();
Scanner sc = new Scanner(input).useDelimiter(" ");
int intcount = 0;
int dblcount = 0;
int strcount = 0;
while (sc.hasNext())
{
if(sc.hasNextInt() == true)
{
System.out.println("Integer: " + sc.nextInt());
intcount++;
System.out.println(intcount);
}
else if(sc.hasNextDouble() == true)
{
System.out.println("Double: " + sc.nextDouble());
dblcount++;
System.out.println(dblcount);
}
else {
System.out.println("String: " + sc.next());
strcount++;
System.out.println(strcount);
}
}
}
}
Well this program indetifies if the user input is an integer a double or a string. Now the problem that I have is that I have to sum up the integers, then I have sum up the double and finally concatenate the string.
Any suggestions? Thank you
Explanation / Answer
Parameters
pattern -- a string specifying the pattern to scan
Return Value
This method returns true if and only if this scanner has another token matching the specified pattern
Exception
IllegalStateException -- if this scanner is closed
Example
The following example shows the usage of java.util.Scanner.hasNext() method.
package com.tutorialspoint;
import java.util.*;
import java.util.regex.Pattern;
public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World! 3+3.0=6";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
// check if the scanner's next token matches "World"
System.out.println("" + scanner.hasNext("World"));
// check if the scanner's next token matches "Hello"
System.out.println("" + scanner.hasNext("Hello"));
// print the rest of the string
System.out.println("" + scanner.nextLine());
// close the scanner
scanner.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.