Reading keystrokes from a keyboard and using if statement in Java [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I am learning how to program in Java. I wrote the following code but, I can't get the if statements to work when the variable inputName is compared to a string and the statement is true. Does anyone have any tips on how to fix this?
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
String x = ("Hello type in your first name.");
System.out.println(x);
Scanner keyboard = new Scanner(System.in);
String inputName = keyboard.next();
if (inputName == "erick") {
System.out.println("Hi");
}
String v = "victor";
if (inputName == v) {
System.out.println("Hi");
}
String c = "christy";
if (inputName == c) {
System.out.println("Hi");
}
keyboard.close();
}
}

don't use == instead use equals() for string comparison.
== will check whether 2 references are referring the same object, while equals() will check the value being referred to.

Related

Why does my switch case work here, but if does not? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am learning how to use Lists, and in my following example the switch case works but (what I deem as) the equivalent if statement does not. Can you tell me why?
public class Kapitel14 {
public static void main(String[] args) {
ArrayList<String> testList = new ArrayList<String>();
testList.add("Cousin");
testList.add("Doof");
testList.add("Dorf");
testList.add("Dortmund");
testList.add("Franz");
System.out.println(listCount(testList));
}
public static int listCount(ArrayList<String> newList) {
int capDCounter = 0;
for (String element : newList) {
String firstLetter = Character.toString(element.charAt(0));
switch (firstLetter) {
case ("D"):
capDCounter++;
break;
default:
continue;
}
//if I use this instead it returns wrong results:
//if (firstLetter == "D")
// capDCounter++;
}
return capDCounter;
}
Use
if (firstLetter.equals("D"))
capDCounter++;
instead of
if (firstLetter == "D")
capDCounter++;
.equals() method should be used here as you want to compare the values of strings.

how to check if the variable is null? [duplicate]

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 5 years ago.
I know this doesnt work but how can I know if the user added even one char?
public class Program
import java.util.Scanner;
{
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String b = a.nextLine();
//I know this doesnt work but how can I know if the user added even one char?
if (b!=null){
System.out.println(b);
}
}
}
you can use .equals("") or .isEmpty()
check check if the variable is null
You can do this
if (!b.isEmpty()) {
System.out.println(b);
}

Java - Using a Method in a If Statement [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm trying to write a simple bit of code to get a input via BufferedReader and then execute some code within another method.
import java.io.*;
public class main {
public main() {
}
public static String input() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String out;
try {
out = br.readLine();
return out;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void someCode() {
//some code
}
public static void main (String[] args) {
input();
if(input() == "Input") {
someCode();
}
}
}
Thanks :)
Store the function in a variable like this:
String x = input();
if("Input".equals(x)) {
//do something
}
Also notice how I used .equals not == as == compares adresses in memory not the value.
You're calling the method once, throwing the result away, and then calling it again; the second time is probably not going to give you the results you want. Save the input in a variable instead:
String input = input();
if(input.equals("Input")) {
...
}
(You're also erroneously using == instead of .equals; see the code above.)

Strings from input are never equal? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
import java.util.Scanner;
public class Michal {
public static void main(String[] args) {
reply();
}
public static void reply() {
Scanner input=new Scanner(System.in);
String name=input.nextLine();
if(name=="john"){
System.out.println("bear!");
}else if(name=="mary")
{
System.out.println("lovely lady!");
}else{
System.out.println("I don't know that person.");
}
System.out.println(name);
input.close();
}
}
I consider myself still a beginner to Java ,so please don't be harsh in your answers. I was trying to create a program that returns an answer every time it gets a certain input , however it seems to return "I don't know that person" all the time.
if(name=="john")
is not the right way to compare strings. Use equals() instead:
if(name.equals("john")){
System.out.println("bear!");
} else if(name.equals("mary")){

How do I compare two instances of user input in Java? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
We had a programming exam last week and I wasn't able to do it.
We were asked to make a program that will ask the user to select either of these three options:
given name
middle name
last name.
After selecting, you are asked to enter a name using scanner.
Then it will ask you if you want to try again, if you input yes, the program will loop again and if you enter the same name, it will prompt an error saying you already set it that way.
I've done all of it except that part, comparing the values.
How do I compare the 1st and 2nd input using scanner?
Here is what I have done so far.
public class PXM {
private String givenname;
private String middlename;
private String lastname;
public void setGN(String gn) {
this.givenname = gn;
}
public void setMN(String mn) {
this.middlename = mn;
}
public void setLN(String ln) {
this.lastname = ln;
}
public boolean equals() {
}
}
import java.util.*;
public class TestPXM {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String yes;
int t;
PXM p1 = new PXM(); {
System.out.println("1. given name ");
System.out.println("2. middle name");
System.out.println("3. last name");
System.out.print("select method: ");
int method = sc.nextInt();
if (method == 1) {
System.out.println("enter given name: ");
String gn = sc.next();
p1.setGN(gn);
}
}
}
}
You don't use the scanner to compare. You would save the values input and compare against those.
I would use a Set to hold the values, because Set values are guaranteed to be unique, and that suits the application. You would use Set.contains() to find if you've already got a name.
Assuming you have two Strings, s1 and s2 and you want to compare them, you should use the equals() method:
if (s1.equals(s2)) {
// strings are the same
} else {
// strings are different
}
If you don't know how to use Scanner, take a look here.
You can use this:
Since you've already set the variable in:
p1.setGN(gn);
you can read the input again after suitable prompt like:
String gn=sc.next();
and then compare the stored variable in the object p1 using a getter method:
public String getGN()
{
return this.givenname;
}
Now if you do
if(gn.equals(p1.getGN())
{
System.out. println("You have already set that");
}
You will get the desired result.

Categories