1

I am working on problem of string manipulation.Following is my code snippet. I am really not sure why my code control is not going inside IF loop.

public class test{

public static void main(String args[]) {
    Scanner scan = new Scanner(System.in);
    int searchVariable = scan.nextInt();
    int numberOfValues = scan.nextInt();
    scan.nextLine();
    while (numberOfValues-- != 0) {
        String strg = scan.nextLine();
        String[] arrayInt = strg.split(" ");
        for (int i = 0; i < arrayInt.length; i++) {
            //   System.out.println("test 1===");
           // System.out.println("i value" + i);
           // System.out.println("i " + strg.charAt(i));
           // System.out.println(searchVariable);
            if (searchVariable==strg.charAt(i)) {
                System.out.println("===>");
                System.out.println("index " + i);
                return;
            }

        }
    }
}

}

Input

4

6

1 4 5 7 9 12

Output

output should be 4. As i am writing this code for finding integer 4 in the third line of input.

Problem

Program control is not going inside the loop if (searchVariable==strg.charAt(i)) .Please help!!

pulse
  • 135
  • 1
  • 16
  • 1
    How do you expect `if (searchVariable==strg.charAt(i))` to work? What do you think it should do? Why do you think so? – Pshemo Jan 16 '16 at 19:47
  • 2
    Duplicate of: http://stackoverflow.com/questions/2627371/java-charat-convert-to-int – Tunaki Jan 16 '16 at 19:49
  • 2
    Hint: what do you think is result of `4=='4'`? – Pshemo Jan 16 '16 at 19:49
  • @Pshemo hi, thanks for a quick reply..i updated the question...as i want to search `integer 4` in the third line of the input, that's why i am comparing it with every char at particular index. – pulse Jan 16 '16 at 19:49
  • @Pshemo i also tried it with `(int)strg.charAt(i)`. please let me know if i am wrong (which i could be)..and used `Integer.parseInt` also. – pulse Jan 16 '16 at 19:51
  • 1
    And what value did `(int)strg.charAt(i)` return? What do you think it represents? – Pshemo Jan 16 '16 at 19:52
  • @Pshemo it will represent ASCII of the charAt(i), my bad. But what would you suggest me in this case? – pulse Jan 16 '16 at 19:57
  • Not necessary ASCII but index in Unicode Table. You can find solution in post linked by Tunaki. – Pshemo Jan 16 '16 at 20:05
  • @Pshemo thanks very much!! :) :) – pulse Jan 16 '16 at 20:07

2 Answers2

1

The is problem in your logic: What you actually want is

if (searchVariable==Integer.parseInt(arrayInt[i]))...
ead
  • 27,136
  • 4
  • 67
  • 108
0

When java compares different objects, it checks different aspects, one of them is their type.

For example, the char '1' is different from the integer 1 and therefore '1'==1 will return false.

char c = '1';
System.out.println(c==1); //false

When comparing char to integer you should use parsing or the method Character.getNumericValue(c):

System.out.println(Character.getNumericValue(c)==1); //true

Also note that when java compares char to integer it uses the ascii value of the char, therefore ,char 'a' equals to the integer 97 and '1' equals integer value of 49: (and so on..)

System.out.println(c==49); //true
Noam Mansur
  • 332
  • 2
  • 9