- #1
Apple&Orange
- 30
- 2
- TL;DR Summary
- Strings used in if statements
Edit: Never mind guys, figured it out. I needed to use s.equals :)
However, could someone explain to me why we can't use == on a technical level?
I'm curious to know on a technical level.
Hi guys
Doing a bit of programming, and I'm not too sure why I'm not getting the expected results.
Below is the code, and whenever I type in exit, it always asks me to "Enter the number of points" instead of printing "Terminate".
I guess I am missing something but not too sure what.
import java.util.Scanner;
public class Uppgift_6_my {
public static void main(String[] args) {
System.out.println("Enter the number of points: ");
Scanner testresultinput = new Scanner (System.in);
String testresult = testresultinput.next();
if (testresult == "exit") {
System.out.println("Terminate");
}
else {
System.out.println("Enter the number of points: ");
}
}
}
However, could someone explain to me why we can't use == on a technical level?
I'm curious to know on a technical level.
Hi guys
Doing a bit of programming, and I'm not too sure why I'm not getting the expected results.
Below is the code, and whenever I type in exit, it always asks me to "Enter the number of points" instead of printing "Terminate".
I guess I am missing something but not too sure what.
import java.util.Scanner;
public class Uppgift_6_my {
public static void main(String[] args) {
System.out.println("Enter the number of points: ");
Scanner testresultinput = new Scanner (System.in);
String testresult = testresultinput.next();
if (testresult == "exit") {
System.out.println("Terminate");
}
else {
System.out.println("Enter the number of points: ");
}
}
}
Last edited: