Java while loop working with user inputs [duplicate] - java

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!

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

Java: How do I get a Scanner object to ignore unwanted/invalid inputs? [duplicate]

This question already has answers here:
Scanner input validation in while loop
(3 answers)
Closed 5 years ago.
I am trying to set up a simple text tree that reads in either single-characters (y/n) or integers that correspond to a printed list (1-4). I want to know the easiest way to have the program ignore user inputs that don't correspond to the given options like so:
import java.util.Scanner;
public class simpleMenu
{
Scanner sc = new Scanner(System.in);
String choicePick;
public static void main(String[] args)
{
System.out.println("Would you like to continue? (y/n)");
choicePick = sc.next();
if(choicePick.equals("y"))
{
// The program continues.
}
else if(choicePick.equals("n"))
{
// The program closes.
}
else
{
/*
The scanner ignores the input, ideally without having to restate the question.
The program does not quit or move on until "y" or "n" is entered.
*/
}
}
}
Bonus points if you can help me implement a 'back' option that takes me to the previous choice.
While the next string is not Y or N sc.next() until it is. Then use that string.

Need to fix program in Java [duplicate]

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

Categories

Resources