Using java's scanner input the code needs to return certain output as such:
Expected Output
Download
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = 1;
String typeCase = "";
while (count<4) {
if (!sc.hasNext("[0-9]+[\\.]?[0-9]*") || sc.hasNext(":ascii:")) {
typeCase = "String:";
count++;
} else if (sc.hasNextInt()) {
typeCase = "Int:";
count++;
} else if (sc.hasNextDouble()) {
typeCase = "Double:";
count++;
}
switch (typeCase) {
case "String:":
typeCase = "String: ";
System.out.println(typeCase + sc.nextLine());
break;
case "Int:":
typeCase = "Int: ";
System.out.println(typeCase + sc.nextInt());
break;
case "Double:":
typeCase = "Double: ";
System.out.println(typeCase + sc.nextDouble());
break;
}
}
}
Thank you guys in advance.
Expected Output
Download
- String: Welcome to HackerRank's Java tutorials!
- Double: 3.1415
- Int: 42
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = 1;
String typeCase = "";
while (count<4) {
if (!sc.hasNext("[0-9]+[\\.]?[0-9]*") || sc.hasNext(":ascii:")) {
typeCase = "String:";
count++;
} else if (sc.hasNextInt()) {
typeCase = "Int:";
count++;
} else if (sc.hasNextDouble()) {
typeCase = "Double:";
count++;
}
switch (typeCase) {
case "String:":
typeCase = "String: ";
System.out.println(typeCase + sc.nextLine());
break;
case "Int:":
typeCase = "Int: ";
System.out.println(typeCase + sc.nextInt());
break;
case "Double:":
typeCase = "Double: ";
System.out.println(typeCase + sc.nextDouble());
break;
}
}
}
Thank you guys in advance.