Menu-driven application using If-Else Statement - Java [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
I am trying to create a menu-driven application that uses If-Else statement. Unfortunately, it always results in the Else or Exit message. What's wrong with my code? Thank you!
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("""
Good day!
Get to now Java programming language!
Please enter the letter that corresponds to your choice.
A. What is Java language
B. How to display a message
C. How to get users' input
E. Exit the program
Choice: """);
String choice = console.nextLine();
while (true) {
if (choice == "A") {
String WhatIs = "Java is a Object-Oriented Programming Language \n";
System.out.println(WhatIs);
} else if (choice == "B") {
String display = "Just use System.out.println \n";
System.out.println(display);
} else if (choice == "C") {
String GetUser = "Import the package and use the Scanner method \n";
System.out.println(GetUser);
} else {
String Exit = "Thank you for using this application.";
System.out.println(Exit);
break;
}
}
}
}

As the answer that #maloomeister mentioned in the comment:
Strings need to be compared via equals. So change choice == "A" etc., to choice.equals("A").
The detailed explanation is provided in this answer.

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))

i want to repeat a statment in java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i want to ask a question for a bedsize and while the answer is not what i choose it will be i want that it will ask the user to answer again
import java.util.Scanner;
public static void main(String[] args) {
String newBedType ;
Scanner sc1 = new Scanner(System.in) ;
System.out.println("you want a single bed or doublebed? ") ;
newBedType = sc1.next() ;
while (newBedType != "single" + "doublebed") {
System.out.println("please choose againe the bed size: ");
newBedType = sc1.next() ;
switch (newBedType) {
case "single" : System.out.println("i see you like sleeping alone");
break ;
case "doublebed" : System.out.println("got company ;) ");
break ;
}
}
}
}
the code kinda works it shows the cases if i write the correct string but it will continue to ask me forever.....
i just stared learning java so be easy on me i know its a stupid question but after hours of trying and searching here(though i did found in python but dont know how to "translate" it to java)
i cant figure it out... thanks to anyone willing to help :)
Your issue is with the line:
while (newBedType != "single" + "doublebed")
This doesn't do what you think it does. You are comparing the variable newBedType with the string "singledoublebed", the addition operator is concatenating those two strings. You want the line:
while (!newBedType.equals("single") && !newBedType.equals("doublebed"))
Note the use of the .equals() method, as string comparisons in Java do not act as expected with the == or != operators.

Java while loop working with user inputs [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 5 years ago.
I'm learning Java and trying to make a small program.
I made a list and a while loop. Everytime the user inputs something it should save the input to the list. Only if the input is "0" then i want it to break the while loop and print out everything whats inside the list. So it has to keep asking the user for an input till he insert 0. At the moment i don't have a teacher. I'm doing this all on my own. Don't blame me for my bad writting skills...
import java.util.ArrayList;
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
while (true) {
ArrayList nummer = new ArrayList(); // make new list
Scanner input = new Scanner(System.in); // start scanner
System.out.print("Voer uw naam in: ");
String naam = input.nextLine(); // scanner waiting for input + enter
if (naam == "0") {
System.out.println("Wrong, exit!");
input.close();
for (Object item : nummer) { // foreach-loop
System.out.print(item);
}
break;
} else {
nummer.add(naam);
continue;
}
}
}
}
Could someone take a look to it and tell me what's wrong with it?
Thanks!

Categories

Resources