0

I am using Java and have been trying to split my string input into 3 parts. For example, my input will be "AND 1 1", and I am expecting it to go into my if-loop where the condition is parts[0] == "AND". But this is not the case, and I am not sure why.

My code is listed below

Scanner stringInput = new Scanner(System.in);
String input = stringInput.next();
System.out.printf("%s\n", input);
String[] parts = input.split(" ");

if (parts[0] == "AND") {
    if (parts[1] == parts[2] && parts[1] == "1") 
        System.out.printf("1\n");
    else 
        System.out.printf("0\n");
}
else {
    if (parts[1] == "1" || parts[2] == "0")
        System.out.printf("1\n");
    else 
        System.out.printf("0\n");
}
boxme
  • 187
  • 1
  • 6
  • 14

8 Answers8

2

You are using stringInput.next() which will not read spaces. So that is the problem. Use stringInput.nextLine() instead of stringInput.next()

String input=stringInput.nextLine(); is correct when you are working with text that contains space.

Here is the edited code.

Scanner stringInput = new Scanner(System.in);
String input = stringInput.nextLine();
System.out.printf("%s\n", input);
String[] parts = input.split(" ");

if (parts[0] == "AND") {
    if (parts[1] == parts[2] && parts[1] == "1") 
        System.out.printf("1\n");
    else 
        System.out.printf("0\n");
}
else {
    if (parts[1] == "1" || parts[2] == "0")
        System.out.printf("1\n");
    else 
        System.out.printf("0\n");
}

You probably might have got an ArrayIndexOutOfBoundsException

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97
  • what is if (parts[0] == "AND") isn't it ALWAYS true since it checks wether both are objects of same type. – Ravitheja Jun 29 '13 at 07:21
  • It checks references for equality not the values. When you write something in double quotes, it is treated as a String object. Now, "AND" is another string object and parts[0] is another String object. Now the references are checked. This is why equals() is a great method. – JavaTechnical Jun 29 '13 at 07:23
2

In Java you can not be sure that the string is the object you think it is. For this reason, you should not use == to compare objects, but use the equals function.

if (parts[0] == "AND")

should be

if (parts[0].equals("AND"))

Strings are immutable, so the functions will always return new strings, when they have to do something on them. For this reason, using == will only work in some particular cases, but never when you process them.

Devolus
  • 21,661
  • 13
  • 66
  • 113
0

Try this piece of code:

if (parts[0].toString().equals("AND")){
    if (parts[1].equals(parts[2]) && parts[1].equals("1")) 
        System.out.printf("1\n");
    else 
        System.out.printf("0\n");
}else {
    if (parts[1].equals("1") || parts[2].equals("0"))
        System.out.printf("1\n");
    else 
        System.out.printf("0\n");
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
shreyansh jogi
  • 2,082
  • 12
  • 20
0

You are using the String.split() method in correct way. The parts[0] also contains the desired "AND" after using the method.

But the problem is you are checking equality of two strings using "==" instead of equals method. When you use == then it checks whether they are same reference of string while the equals method check the values of the string.

So your checking should be something like this

if (parts[0].equals("AND"))
stinepike
  • 54,068
  • 14
  • 92
  • 112
0

Use

 parts[0].equals("AND") to check equality of `String`
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

There are several issues:

  • as JavaTechnical pointed out, you should use stringInput.nextLine() vs stringInput.next()
  • as everyone pointed out, use .equals() vs ==, here's a thread explaining why: Java String.equals versus ==
  • test against a known vs an unknown, reorder your test like so "AND".equals(part[0]) should shield you from null pointer
Community
  • 1
  • 1
ikumen
  • 11,275
  • 4
  • 41
  • 41
0

Just to complete the Devolus answers (I can't put comments in comments yet U_U)

== compares the references, it means that is only true if two variables are pointing to the same object

e.g.

  Object a =  new Car()
  Object b =   a  
  Object c =  new Car()





  (a == b) //true
  (a == c)  //false

  a.equals(b)  //true  
  a.equals(c)  //true 
luisZavaleta
  • 1,160
  • 11
  • 21
0

Try using .substring(), for example:

String a = "AND 1 1"
b = a.substring(1,3)
c = a.substring(4,5)
System.out.println(b)
System.out.println(c)

Would print "AND" then "1"

Matthew
  • 185
  • 2
  • 12