Why System.out.println "Input" comes out twice? - java

i'm new to programming and we've got an example from school to understand how Scanner works. My problem is that i do not understand why the message "Input" (System.out.println in the while loop) gets printed twice.
import java.util.Scanner;
class ScannerInput {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Give me a String:");
String zeichenkette = myScanner.nextLine();
System.out.println("Give me an Integer:");
int einInteger = myScanner.nextInt();
// Output input by user
System.out.println("String: " + zeichenkette);
System.out.println("Integer: " + einInteger);
boolean scannerSuccess = false;
while(!scannerSuccess) {
System.out.print("Input: ");
if(myScanner.hasNextInt()) {
einInteger=myScanner.nextInt();
myScanner.nextLine();
scannerSuccess=true;
} else if(myScanner.hasNextLine()){
myScanner.nextLine();
scannerSuccess=false;
}
}
myScanner.close();
}
}

else if(myScanner.hasNextLine()){
myScanner.next();
scannerSuccess=false;
}
try this code

Related

How do I categorize a list of user inputs into different orders?

I am supposed to make a program that asks users for a list of input. Then, from that list, my program is supposed to pick out the third answer and then print it out. It sounds really simple, but how do I assign numbers to each of the user inputs? Do I even do that? I am a beginner, and thank you so much for your help!
This is the code I have so far:
import java.util.*;
public class MyProgram
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
while(true) {
System.out.println("What do you appreciate in your life or school?");
String ans = scan.nextLine();
if(ans.equals(""))
{
break;
}
}
System.out.println("You said \"" + input3 + "\" as your third answer.");
}
}
You can strore the third input in a variable you initialized bevor the loop.
if there is no third input it prints: ""
import java.util.*;
public class MyProgram
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int index = 0;
String input3 = "";
while(true) {
System.out.println("What do you appreciate in your life or school?");
String ans = scan.nextLine();
if (index == 2) {
input3 = ans;
}
if(ans.equals("")){
break;
}
index++;
}
System.out.println("You said \"" + input3 + "\" as your third answer.");
}
}

How to get the result of another method from another method and print it

i want to get all the result from another method and compile it as one
package labexer1a;
import java.util.Scanner;
public class LabExer1A {
public static void main(String[] args) {
faveNumber();
faveCartChar();
mI();
fullName();
nickName();
}
public static void faveNumber() {
Scanner s = new Scanner(System.in);
System.out.print("Type your Number: ");
int faveNumber = s.nextInt();
}
public static void faveCartChar() {
Scanner s = new Scanner(System.in);
System.out.print("Type your favorite Cartoon or Anime Character: ");
String faveCartChar = s.next();
}
public static void mI() {
Scanner s = new Scanner(System.in);
System.out.print("Type your Middle Initial: ");
String mI = s.next();
}
public static void fullName() {
Scanner s = new Scanner(System.in);
System.out.print("Type your FullName: ");
String fullName = s.next();
}
public static void nickName() {
Scanner s = new Scanner(System.in);
System.out.print("Type your Nickname: ");
String nickName = s.next();
}
public static void result() {
System.out.print(faveNumber + "is my favorite number");
System.out.print("I love" + faveCartChar);
System.out.print("My name is" + fullName);
System.out.print("You can call me" + nickName);
}
}
You can change the fuctions return type from void to String or int for example and the return the result.
Example:
public static String fullName() {
Scanner s = new Scanner(System.in);
System.out.print("Type your FullName: ");
String fullName = s.next();
return fullName;
}
public static int faveNumber() {
Scanner s = new Scanner(System.in);
System.out.print("Type your Number: ");
int faveNumber = s.nextInt();
return faveNumber;
}
If you now want to acces the data just call the fuction and it will return it.
Example:
System.out.println(nickName() + " is my favorite number");
System.out.println("You can call me " + faveNumber());
And also I would recomend naming the functions more like getNickName so anyone can direclty see that it will return the nick name!

Scanner Issue with Java String Parsing

Feel like I'm missing something really basic here, but my brain is fried right now. Goal is to parse strings with a comma, gets through one loop and throws before the next line. Pretty sure it has something to do with userInput = scan.nextLine();
import java.util.Scanner;
public class ParseStrings
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String userInput = "";
boolean finished = false;
while (!finished)
{
System.out.print("Enter input string: \n");
userInput = scan.nextLine();
if (userInput.equals("q"))
{
System.out.println("First word: " + userInput);
finished = true;
} else
{
String[] userArray = userInput.split(",");
System.out.println("First word: " + userArray[0]);
System.out.println("Second word: " + userArray[1]);
System.out.println();
}
}
return;
}
}
Throwing:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at ParseStrings.main(ParseStrings.java:12)
Thank you in advance

How to accept enter as valid input to Scanner.nextLine()?

I just want the scanner to read new line as empty string then continue to next process if the user press enter. So valid input must be y,n,enter. Any idea how to do this?
This is my code:
String gender = "", employed = "";
Scanner in = new Scanner(System.in);
System.out.print("Gender M/F, press enter to skip... ");
while(!in.hasNext("[mfMF]$")){
System.out.print("Invalid, please choose m/f only... ");
in.nextLine();
}
if(in.hasNextLine()){
gender = in.nextLine();
}
System.out.print("Employed? y/n, press enter to skip... ");
while(!in.hasNext("[ynYN]$|")){
System.out.print("Invalid, please choose y/n only... ");
in.nextLine();
}
if(in.hasNextLine()){
employed = in.nextLine();
}
System.out.println(gender + " : " + employed);
Try This :
import java.util.*;
class Scanner1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;
do
{
s=sc.nextLine();
if(!(s.equalsIgnoreCase("y")||s.equalsIgnoreCase("n")||s.equalsIgnoreCase("")))
{
System.out.println("Please Enter valid input");
}
}while(!(s.equalsIgnoreCase("y")||s.equalsIgnoreCase("n")||s.equalsIgnoreCase("")));
}
}
So, to check if the user has pressed enter, you would have to make use of the isEmpty() method. The way to do that is shown below:
String enter = in.nextLine();
if (enter.isEmpty()) {
// do what is needed
}

Multi-word scanner input in java?

So I'm trying to use if-else statement dependant upon the user's input. It works when the user's input is only one word, however, multiple word inputs go unrecognized and triggers the else statement. How can i resolve this?
import java.util.Scanner;
public class MyFirstJavaClass {
public static void main(String[] args) {
#SuppressWarnings("resource")
Scanner myScanner = new Scanner(System.in);
String answer;
System.out.println("Catch the tiger or run away?");
answer = myScanner.next();
if (answer.equals("Catch the tiger" )) {
System.out.println("You've been mauled by a tiger! What were you thinking?");
answer = myScanner.next();
} else {
System.out.println("run away");
}
}
}
Replace:
answer = myScanner.next();
With:
answer = myScanner.nextLine();
next will only read in the next value until it reaches a space or newline. You want to read in the full line before making the comparison
try this :
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (scanner.hasNext()){
if (scanner.hasNextInt()){
choice = scanner.nextInt();
break;
} else {
scanner.next(); // Just discard this, not interested...
}
}
Reference :
Flush/Clear System.in (stdin) before reading
Try this
import java.util.Scanner;
public class MyFirstJavaClass {
public static void main(String[] args) {
#SuppressWarnings("resource")
Scanner myScanner = new Scanner(System.in);
System.out.println("Catch the tiger or run away?");
if (myScanner.hasNext("Catch the tiger")) {
System.out.println("You've been mauled by a tiger! What were you thinking?");
} else {
System.out.println("run away");
}
}
}

Categories

Resources