This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want to give user a possibility to stop inputing new lines in my String array by entering the word "end". I checked out - it really inputs 'end' before the if statement, but for some reason this doesn't break the while cycle:
Scanner scan = new Scanner(System.in);
String[] str = new String[50];
int j=0;
int f = 0;
System.out.println("Input 'end' to see your previous input!");
while(f != -1)
{
the input goes well, but the if statement doesn't work completely
String choice = scan.nextLine();
if(choice == "end")
{
f = -1;
}
else
{
str[j] = choice;
j++;
}
}
Use
choice.equals("end") instead of ==
See here
Also see equalsIgnoreCase() method for string comparision
Related
This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 2 years ago.
I keep getting this error on my code. I think its to do with my parseInt() command but im not sure. Im trying to create a stack of integers that come from a user input string such as "PUSH 5" and just extract the 5 from the string to push into the stack.
import java.util.*;
public class lab6
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
lab6stack theStack = new lab6stack(size);
String [] ar = new String [size];
for(int i = 0; i < size; i++)
{
ar[i] = sc.next();
if(ar[i].charAt(1) == 'U')
{
String sub = ar[i].substring(4);
int num = Integer.parseInt(sub);
theStack.push(num);
}
else if(ar[i].charAt(1) == 'O')
{
theStack.pop();
}
}
while (!theStack.isEmpty())
{
System.out.println(theStack.pop());
}
}
}
As others have said, sc.next() only returns "PUSH", not "PUSH 5". In my opinion, #Pshemo's solution works just fine, but you said that it is not how you want to do it. In that case, you can replace your sc.next() with sc.nextLine() to get "PUSH 5". That way, you can keep the rest of your code the same.
Hope this is what you want!
You're referring to the space between PUSH and your number in this line:
String sub = ar[i].substring(4);
As substrings also start counting on zero, your number should be parsed when using substring(5); here instead.
Here is an example with indexes:
"P"=0 ; "U"=1 ; "S"=2 ; "H"=3 ; " "=4 ; "3"=5
This is because you are not checking the length of the string before grabbing the character.
here:
if(ar[i].charAt(1) == 'U')
and here:
else if(ar[i].charAt(1) == 'O')
Check with ar[i].length() >= 1. For example:
if(ar[i].length() >= 1 && ar[i].charAt(1) == 'U')
You will also need to apply the other two fixes addressed in the other answers.
Use sc.nextLine() when reading the input and use substring(5) in the "PUSH" if case.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I can not figure out why my while loop won't break and it just keeps running I have tries making Lc 10 and I tried return but nothing will end the loop.
import java.util.Scanner;
public class BirthdayReminder
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String[] BDay = new String[10];
String[] friend = new String[10];
int Lc = 0;
String i;
while(Lc < 10) {
System.out.println("enter a friends name or zzz to quit");
i = input.nextLine();
if(i == "zzz") {
break;
}
else if(i != "zzz"){
friend[Lc] = i;
System.out.println("enter their birthday.");
i = input.nextLine();
BDay[Lc] = i;
Lc++;
return;
}
}
System.out.println("hi");
}
}
You need to check string equality using equals(), not ==. Neither block of code inside the if is getting entered, so Lc is never incremented.
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.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
i have an assignment where i have to make a program that reads input and evaluates whether or not it is a palindrome until the user terminates. i am having issues with the while loop. it works like it should upon first input, but then when it re-enters the loop, it always says it is not a palindrome, even when it is.
Scanner in;
in = new Scanner (System.in);
String original = ""; //variable for user input
String original2 = ""; //variable to help make input case insensitive
String reverse = ""; //variable to compare word in reverse
int length; //variable used to
int i; //variable used in for loop
//String response = "";
System.out.println("Enter a word to check if it is a palindrome!");
original = in.nextLine();
while (!original.equals("n"))
{
length = original.length();
original2 = original; //assigning a new variable to hold the input, so as to not change it
original2 = original.toLowerCase(); //converting input to all lower case using the holder variable
for (i = length -1; i >= 0; i--) //for loop reversing the word to check for palindrome status
{
reverse = reverse + original2.charAt(i);
}
if (original2.equals(reverse)) //if statement determining output if palindrome is true or not
{
System.out.println(original + " is a palindrome");
}
else
{
System.out.println(original + " is not a palindrome");
}
System.out.println("Enter another word or enter 'n' to end"); //allowing to continue
original = in.nextLine();
}
System.out.println("thank you!");
in.close();
}
i initially thought that it was an issue with the buffer, but i no longer think that is an issue.
i will really appreciate any help at all
You don't reset reverse, so the reversed string is constantly being built on
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want to be able to make my if statement restart the program if "Yes" is inputted, end the program if "No" is imputed, and say "Capitalization is important" if neither "Yes" or "No" is inputted.
Scanner sc = new Scanner(System.in);
final String vowels = "aeiouAEIOU";
System.out.println("Enter your word:");
String word = sc.nextLine();
while (!word.equalsIgnoreCase("done"))
{
String beforVowel = "";
int cut = 0;
while (cut < word.length() && !vowels.contains("" + word.charAt(cut)))
{
beforVowel += word.charAt(cut);
cut++;
}
if (cut == 0)
{
cut = 1;
word += word.charAt(0) + "w";
}
System.out.println(word.substring(cut) + beforVowel + "ay");
Scanner keyboard = new Scanner (System.in);
System.out.print("Do you wish to convert another setence ");
String name = keyboard.nextLine();
if(name = "Yes"){
new Prog508a().launch;
}
if(name = "No"){
break;
}
else{
System.out.print("Capitalzation is important.");
}
String name = keyboard.nextLine();
if(name = "Yes"){
new Prog508a().launch;
}
if(name = "No"){
break;
}
This is really not a good way to do this, I suggest you use method returns instead.
Also you should not use
if(name = "Yes")
Since it is not a comparison, a better way of doing this would be
if(name.contains("y") || name.contains("Y")){
As this allows the user to enter anything starting with y or Y, but if you really want to use 'Yes' then you should do
if(name.equals("Yes"))