Stuck with creating if statement from string input [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I am trying to learn Java and want it so you input your name, and if name input matches your name it will print Hello [your name], I am doing this using an if statement and make if so if the input is equal to a string equal to my name it will print hello plus the input. However it doesn't... the else statement is what confuses me because I got it to print the two value to see if they where equal and they both where... help would be appreciated thanks.
package AgeTester;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class HelpSystem {
public static void main(String args[]) {
String inp = "Jono";
JFrame frame = new JFrame("Input Dialogue");
String name = JOptionPane.showInputDialog(frame, "What is your name?");
if (inp == name) {
System.out.printf("Hello", name);
} else {
System.out.println(inp + name);
}
System.exit(0);
}
}

When you compare two strings you should use the .equals(String) method
if (inp.equals(name)) {
remember: == tests for reference equality (whether they are the same object).

You're not using the equals() method, it's what we use to comapre strings in Java not ==.
if (inp.equals(name)) {
System.out.printf("Hello", name);
} else {
System.out.println(inp + name);
}

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();
}

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.

Java encode and decode program cant pass key word through args [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
So, I'm not sure what is happening here, if I place encode or decode in the args[0], it should work right, but it doesn't. I have all the imports and I have a utility class that I'm using as well. I don't understand why, when I run the program with these arguments: java Prog4 encode fly message.txt it won't work right. It'll go straight to the last else statement.
public class Prog4 {
public static void main(String[] args){
if (args.length != 3){
System.out.println("Enter the right amount of arguments!");
System.exit(0);
}
String command=args[0];
String key= args[1];
String fileName = args[2];
File file = new File(args[2]);
String fileExtention="";
if(args[0]=="encode"){
fileExtention=".crypt";
}
else if (args[0]=="decode"){
fileExtention=".decrypt";
}
else{
System.out.println("Enter decode or encode!");
System.exit(0);
}
try this:
args[0].equals("encode")
and this:
args[0].equals("decode")
to compare the strings in java...
you use == to check if the references are equal.
you should use .equals() to check if the values are equal.
args[0]=="encode"
is WRONG!
it checks for object reference equality, not value equality!
use:
args[0].equals("encode");
or
args[0].equalsIgnoreCase("encode");
to ignore the case

Java if+String dont work together [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm learning java and I have a problem:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;
import java.util.*;
public class edytor{
public static void main(String[] args) throws FileNotFoundException
{
Scanner czynowy = new Scanner(System.in);
System.out.println("Do you wanna editing existing file?");
String tn = czynowy.nextLine() ;
if(tn=="t")
{System.out.println("bleble"); }
Scanner odpowiedz = new Scanner(System.in);
System.out.println("Type file name");
String polozenie = odpowiedz.nextLine() ;
System.out.println("################################");
PrintWriter zapis = new PrintWriter(polozenie);
Scanner tekst = new Scanner(System.in);
String tekst1 = tekst.nextLine() ;
zapis.println(tekst1);
zapis.close();
}
}
It's compiling, but
when in string tn I type t char this doesn't print "bleble". What should I do to make it work?
Greetings!
if (tn.equals("t") {...}
String is an object, and if you create 2 strings, even if they will have same value, they won't be equal to eachother
string1 == string2 // false
the == checks the object identity. while the .equals() method in String, checks the value.
The other way of doing this will be to use for loop, to loop through every char in each string, and check if it matches the character with the same place in another string.
or in your case, do this:
if (tn.getBytes()[0] == 't') {...}
You need to use
if (tn.equalsIgnoreCase("t") {
...
}
The reason you cannot compare two Strings with == is because Strings are objects. When you try to compare two objects directly, you are comparing their locations in memory. So, even if the contents of two Strings may be equal, their memory locations will not be equal.

Never runs `if` , always `else` [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
For some reason, my (basic) program always prints the text I reserved for my else statement.
I am a newb when it comes to Java, so if I am making an obvious mistake I apologize. I also searched high and low for an answer, but couldn't find one. Could you take a look at this:
package test;
import java.util.Scanner;
public class tutorial_7 {
private static Scanner x;
public static void main(String args []) {
x = new Scanner(System.in);
System.out.print("Apples, or oranges: ");
String bog = x.next();
if (bog == "Apples") {
System.out.print(1);
}
if (bog == "Oranges") {
System.out.print(2);
}
else {
System.out.print(3);
}
}
}
}
Why is the text reserved for my if statements never being output? Everything seems to be fine.
Regards,
JavaNoob
Don't use == to compare strings, it's for object identity.
Comparing strings should be done with the equals() method, such as:
if (bog.equals ("Oranges")) {
How do I compare strings in Java?
if (bog.equals("Apples")){
System.out.print(1);
}
if (bog.equals("Oranges")){
System.out.print(2);
}
else{
System.out.print(3);
}

Categories

Resources