Need to fix program in Java [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want user to input text while it is not equal to "start".When it is equal to "start" I want to show "Bravo".In my code when I enter "start" it just continue to ask to input a text.What is missing in my code to process the operation i described.
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(komanda != "start");
System.out.println("Bravo");
}
}

You have to use the equals method to compare strings in java:
while (!komanda.equals("start"));
or even better
while (!"start".equals(komanda));
this does not crash if komanda is null
See How do I compare strings in Java? for more information.

do it this way
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(!"start".equals(komanda));
System.out.println("Bravo");

Related

I'm trying to get a toString to print out in a group together after a loop is cancelled [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
This is what I have so far and I tried to use the while (yorn=="yes") to cancel but it keeps going regardless and I am also trying to find a way to make it so the outputs go in one group together rather than being seperated.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String no = "no";
String yes = "yes";
String yorn="yes";
CollegeStudent Student1 = new CollegeStudent();
while (yorn=="yes") {
System.out.println("continue? yes/no:");
yorn=scan.next();
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
}
}
}
You need to use the equals method for string comparing in the java:
while (yorn.equals("yes"))
{
System.out.println("continue? yes/no:");
yorn=scan.next();
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
}
Since after you answer exists the logic in the cicle, after yes typing code has been executing anyway (one time). Simply solution for you will be:
System.out.println("Do you need to add a student? yes/no:");
yorn=scan.next();
while (yorn.equals("yes"))
{
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
System.out.println("continue? yes/no:");
yorn=scan.next();
}

Using Strings in a If, else statements JAVA [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I'm trying to make a program that can read whatever the user inputs and checks their input using if..else statement.
import java.util.Scanner;
class Answers{
public void FalseAnim() {
System.out.println("Game Over your answer is wrong. try again!");
}
public void CorrectAnim() {
System.out.println("your answer is correct");
}
}
public class quizgame {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
Answers ans = new Answers();
String strans1;
System.out.println("welcome to the quiz game!");
System.out.println("what is 1+1");
strans1 = input.nextLine();
if (strans1=="two"||strans1=="2") {
ans.CorrectAnim();
}
else {
ans.FalseAnim();
}
}
}
every time I run the program and input anything it goes straight into the else statement, even when I input either a "2" or a "two"
if ("two".equals(strans1)||"2".equals(strans1))
will work.
In your code, you are comparing references and not the value. Hence it is returning false either way.

Java - IF condition not reading entered String variable value [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String play = "y";
System.out.print("Enter something: ");
play = scan.next();
System.out.println(play);
if (play == "Y" || play == "y")
{
System.out.println("If test works!!");
}
System.out.println("Did it work???");
}
}
I assume this has something to do with when I press enter, it's storing that as well. I tried changing String play to a char, but then I get errors from Scanner saying it can't change a String to a char.
You should atmost avoid using “==“ when comparing objects especially strings. “==“ checks for object references. Change the comparison to use .equals method and it should work
if(play.equals(“Y”) || play.equals(“y”))
in case if “play” can be null, the below snippet is more safe.
if(“Y”.equals(play) || y.equals(play))

IF condition on strings [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm writing a simple code to test the value that was inputted to my constant value.
I declared this code as my constant value.
String LetMeThrough = "drunk";
String GotAnID = "drunk";
This is the whole code.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner DrunkTest = new Scanner(System.in);
String InputDrunk;
String InputDrunkAgain;
String LetMeThrough = "drunk";
String GotAnID = "drunk";
System.out.print("Type drunk: ");
InputDrunk= DrunkTest.next();
System.out.print("Re Type drunk: ");
InputDrunkAgain = DrunkTest.next();
if(InputDrunk == LetMeThrough & InputDrunkAgain == GotAnID){
System.out.print("You're not DRUNK");
}
else
System.out.print("You're F***** DRUNK");
}}
The problem is that if I type "drunk" on both.
I will get "You're F****** DRUNK" instead of the "You're not DRUNK".
When the inputted values is the same as my constant values.
You must use String::equals method to compare.

Using String name = input.next(); then making an If statement [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I was wondering how I can fix this:
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.next();
if (name == Donald)
System.out.println("Welcome back Admin");
else
System.out.println("Go Away");
}
}
I want to make it so that if the user inputs a specific name, then it will say something specific, anything else and it says go away.
I am a new student of Java and was messing around to see if this is possible
If I understand correctly strings are immutable and are frequently reused, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. That means you can't just ask if string1 == string2 because they might be separate instances. So you want to check with string.equals(string2) to 'see if the content is the same'.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.next();
if (name.equals("Donald"))
System.out.println("Welcome back Admin");
else
System.out.println("Go Away");
}
}

Categories

Resources