message variable holds the same value

I am trying to learn a bit of java before jumping into a project so when i do i'll know or have an idea on how to handle some situations. I am working on receiving user input. Which won't serve too big of a purpose later on but i still am gonna learn how. I'm currently having trouble getting my message variable to clear after a message has been printed to the console. It holds the same value after i enter any type of text. Anyone know why?
import java.util.Objects;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String message = scanner.nextLine();

while (!Objects.equals(message, "QUIT")) {
if (Objects.equals(message, "Hello")) {
System.out.println("Hello there!");
scanner.nextLine();
}
else if (Objects.equals(message, "Bye")) {
System.out.println("Cya!");
scanner.nextLine();
}
}
}
}
import java.util.Objects;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String message = scanner.nextLine();

while (!Objects.equals(message, "QUIT")) {
if (Objects.equals(message, "Hello")) {
System.out.println("Hello there!");
scanner.nextLine();
}
else if (Objects.equals(message, "Bye")) {
System.out.println("Cya!");
scanner.nextLine();
}
}
}
}
32 Replies
JavaBot
JavaBot9mo ago
This post has been reserved for your question.
Hey @DevLop games! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Tomasm21
Tomasm219mo ago
Because in the loop you don't assign a new string value to the message variable.
DevLop games
DevLop gamesOP9mo ago
I need to reassign a value?
Tomasm21
Tomasm219mo ago
yes
DevLop games
DevLop gamesOP9mo ago
I just tried and i get the same issue Except the new value doesn't get recorded
Tomasm21
Tomasm219mo ago
perhaps you are doing it wrong
DevLop games
DevLop gamesOP9mo ago
How am i supposed to do it then??
Tomasm21
Tomasm219mo ago
you have to assign it not in the condition
DevLop games
DevLop gamesOP9mo ago
import java.util.Objects;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String message = scanner.nextLine();

while (!Objects.equals(message, "QUIT")) {
if (Objects.equals(message, "Hello")) {
System.out.println("Hello there!");
message = scanner.nextLine();
}
else if (Objects.equals(message, "Bye")) {
System.out.println("Cya!");
message = scanner.nextLine();
}
}
}
}
import java.util.Objects;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String message = scanner.nextLine();

while (!Objects.equals(message, "QUIT")) {
if (Objects.equals(message, "Hello")) {
System.out.println("Hello there!");
message = scanner.nextLine();
}
else if (Objects.equals(message, "Bye")) {
System.out.println("Cya!");
message = scanner.nextLine();
}
}
}
}
Where
Tomasm21
Tomasm219mo ago
before condition
DevLop games
DevLop gamesOP9mo ago
At the start of the loop?
Tomasm21
Tomasm219mo ago
yes close the scanner after the loop.
DevLop games
DevLop gamesOP9mo ago
Why do i need to do that?
Tomasm21
Tomasm219mo ago
It's a bit better practice. To avoid memory leak for the scanner. when the application ends. If you are using Eclipse or IntelliJ then you might see that scanner is underlined in yellow
DevLop games
DevLop gamesOP9mo ago
I don't see yellow
Tomasm21
Tomasm219mo ago
too bad. Those IDEs can give you valuable information about the syntax
Tomasm21
Tomasm219mo ago
No description
DevLop games
DevLop gamesOP9mo ago
You mean the tool tip?
Tomasm21
Tomasm219mo ago
yea
DevLop games
DevLop gamesOP9mo ago
I have that Also i'm noticing the application is ending with code 130 Why is this?
Tomasm21
Tomasm219mo ago
I don't know. What editor are you using to wrote the code and to compile at running?
DevLop games
DevLop gamesOP9mo ago
intellij community edition
Tomasm21
Tomasm219mo ago
Try to close scanner and check whether you get the same ending code.
DevLop games
DevLop gamesOP9mo ago
I am closing it
import java.util.Objects;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String message = "";

while (!Objects.equals(message, "QUIT")) {
message = scanner.nextLine();
if (Objects.equals(message, "Hello")) {
System.out.println("Hello there!");
}
else if (Objects.equals(message, "Bye")) {
System.out.println("Cya!");
}
}
scanner.close();
}
}
import java.util.Objects;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String message = "";

while (!Objects.equals(message, "QUIT")) {
message = scanner.nextLine();
if (Objects.equals(message, "Hello")) {
System.out.println("Hello there!");
}
else if (Objects.equals(message, "Bye")) {
System.out.println("Cya!");
}
}
scanner.close();
}
}
Tomasm21
Tomasm219mo ago
ok I don't know regarding 130. But the initial question is solved.
DevLop games
DevLop gamesOP9mo ago
Soo can i get an explanation on what code 130 is? I cannot find anything explaining what it is exactly and what can cause it.
Tomasm21
Tomasm219mo ago
If your console outputs it, you can copy it and search with google. You can put a comma and write IntelliJ console output as well
DevLop games
DevLop gamesOP9mo ago
Could you just tell me Just tell me i'm not seeing any useful information here
Tomasm21
Tomasm219mo ago
If I will find it then ok paste here what do you see in your console
DevLop games
DevLop gamesOP9mo ago
Process finished with exit code 130 This is the only relevant thing
Tomasm21
Tomasm219mo ago
Have you pressed "Ctrl+C" to stop the program? If yes then that's the reason. otherwise if you type QUIT then it should end normally and exit code should be 0.
JavaBot
JavaBot9mo ago
Post Closed
This post has been closed by <@769926604872613899>.

Did you find this page helpful?