This question already has answers here:
How do I compare strings in Java?
(23 answers)
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
Below is my code for a "Do-While" loop. I can't figure out why it's terminating before I even get to enter in the value for my variable "redo" at the end of the do {} section. It terminates right after printing the "would you like to do this again" part.
import java.util.Scanner;
public class AlgWorkbench0402 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String redo;
do {
System.out.println("Enter in two integers and get the sum.");
int val1, val2;
val1 = Integer.valueOf(scanner.nextInt());
val2 = Integer.valueOf(scanner.nextInt());
System.out.println(val1 + val2);
System.out.println("Would you like to do this again? [y/n]");
redo = scanner.nextLine();
}
while (redo == "y" || redo == "Y");
}
}
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Scanner only reads first word instead of line
(5 answers)
Closed 4 months ago.
I am running this code to get 3 values: an integer, a string and a boolean from the user and print it in separate lines.
import java.util.*;
public class Practice {
public static void main(String[] args){
int a;
String b;
boolean c;
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
b = scanner.nextLine();
c = scanner.nextBoolean();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
I am trying to give input like this:
1
hello world
true
and am getting this error after writing the second line of input
next() can read the input only till the space. It can't read two words separated by a space. Also, next() places the cursor in the same line after reading the input.
nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
String b = scan.next();
boolean c = scan.nextBoolean();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
so basically you can use next() instead of nextLine() or reorder if you really need to use nextLine()
b = scanner.nextLine();
a = scanner.nextInt();
c = scanner.nextBoolean();
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
Why does this code print an extra line in the beginning, and also after accepting 2 strings it takes 0 to 2, which is 3 iterations to print the output?
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<=n;i++)
{ String s1="",s2="";
String str;
str= sc.nextLine();
int l=str.length();
for (int k=0;k<l;k++)
{
if (k%2==0)
s1=s1+str.charAt(k);
else if(k%2!=0)
s2=s2+str.charAt(k);
}
System.out.println(s1+" "+s2);
}
}
}
I tweaked your solution a bit. It should work.
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++)
{ String s1="",s2="";
String str;
str= sc.nextLine();
int l=str.length();
for (int k=0;k<l;k++)
{
if(k%2==0)
s1=s1+str.charAt(k);
else if(k%2!=0)
s2=s2+str.charAt(k);
}
System.out.println(s1+" "+s2);
}
sc.close();
Edit:
Also, you were iterating (n+1) times
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 5 years ago.
I have a Java program below. At first, I tried to get input n as this
int n = sc.nextInt();
But the output was different than expected. It runs the first iteration without taking the userName. After changing to
int n = Integer.parseInt(sc.nextLine());
It's working fine. What was wrong with "int n = sc.nextInt();"?
public class StringUserNameChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Number of usernames you want to enter
int n = Integer.parseInt(sc.nextLine());
while (n-- != 0) {
String userName = sc.nextLine();
System.out.println("Your username is " + userName);
}
}
}
When sc.nextLine() is used after sc.nextInt(), it will read a newline character after the integer input.
So, to run your code correctly, you'll have to use sc.nextLine() after sc.nextInt(), like this:
int n = sc.nextInt();
sc.nextLine();
while(n-- > 0) {
String username = sc.nextLine();
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Could some one please tell me why this calculator isn't working ? It just doesn't provide an awnser.
import java.util.Scanner;
public class Calcu {
public static void main( String[] args )
{
Scanner mati = new Scanner(System.in);
System.out.println("This program adds up or substracts two numbers");
System.out.println("Enter an operator");
String letter = mati.next(); //WAITS FOR THE PHRASE ADD OR SUBSTRACT
System.out.println("Enter your first number");
int userNumberone = mati.nextInt(); // Get's first Number
System.out.println("Enter your second number");
int userNumbertwo = mati.nextInt(); //Get's Following Number
if(letter == "add") {
int result = userNumberone + userNumbertwo;
System.out.println(result);
} else if(letter == "substract") {
int result1 = userNumberone - userNumbertwo; //If statement to add or substract.
System.out.println(result1);
}
}
}
You should use letter.equals("add") instead of letters == "add". This is explained here: How do I compare strings in Java?
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am making a Game of Life program, early stages of it. When I run the program and get to the "Do you want to make..." and i input "y", it will go to the else, print my test statement of test3, and end the program. What am I overlooking?
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String userInput = "";
char[][] initialGrid = new char[25][75];
char[][] world = makeInitialGrid(kb, userInput, initialGrid);
printGrid(world);
userInput = "y";
while (userInput == "y"){
System.out.println("Do you want to make a new generation? (y) yes (n) no");
userInput = kb.nextLine();
System.out.println(userInput);
if (userInput == "y"){
System.out.println("test1");
int numOfNeighbors = findNeighbors(world, 6, 2);
System.out.println("test2");
System.out.println(numOfNeighbors);
//makeNewGeneration(world);
} else {
System.out.println("test3");
break;
}
}
kb.close();
For string comparisons in Java, you need to use String#equals, not ==. Try if (userInput.equals("y")) { ... instead.