-2

i have array which is:

String[] array= {azem, 1 , soaib, 3}; // a[0]= azem , a[1]= 1, a[2]=soaib, a[3]= 3

My code is not printing any thing. I also saw ascii table and get the values of numbers in string that is from 48 to 57 but no printing. Mycode:

String input = "1"; // integers as input 
//String takeinput;
// String input = "48" + takeinput;
for(int i=0; i<array.length; i++){
    if(array[i]== input){
       System.out.print(array[i-1]);
       System.out.print(array[i]);
    }
user3440716
  • 639
  • 2
  • 12
  • 23
  • 1
    Use array[i].equals(input) method to compare 2 Strings. – RKC Mar 22 '14 at 06:34
  • no success. it shd also work when input is integer and prints integer and string that is output: "azem 1" – user3440716 Mar 22 '14 at 06:37
  • 1
    @user3440716 Use `.equals()` to compare two `String`s, and you may want to replace you second `System.out.print()` by `System.out.println()` to be sure to show what you are printing. – Florent Bayle Mar 22 '14 at 06:42

3 Answers3

1

In Java, the == compares if the two references point to the same object in memory, not whether they actually point to equivalent strings.

Use array[i].equals(input) instead.

musical_coder
  • 3,886
  • 3
  • 15
  • 18
  • not printing anything. but when i remove for loop and if statement and only writes System.out.print(0); it prints. – user3440716 Mar 22 '14 at 06:41
1

This,

String[] array= {"azem", "1" , "soaib", "3"}; // a[0]= azem , a[1]= 1, a[2]=soaib, a[3]= 3


String input = "1"; // integers as input 
//String takeinput;
// String input = "48" + takeinput;
for(int i=0; i < array.length; i++){
    if(array[i].equals(input)) {  // Modified
       System.out.print(array[i-1]);
       System.out.print(array[i]);
    }

Or better, if you know every odd position (indexed 0) would be number, you can rewrite the code as,

int input = 1; // Modified : integers as input 
for(int i=1; i < array.length; i += 2){
    if(Integer.valueOf(array[i]) == input) {  // Modified
       System.out.println(array[i-1] + ":" + array[i]);
    }

This would be a cleaner and a faster solution.

Mohan
  • 1,850
  • 1
  • 19
  • 42
  • The array definition will not compile. You have to put everything inside quotes: `String[] array= {"azem", "1" , "soaib", "3"};` – Florent Bayle Mar 22 '14 at 06:46
  • sorry, I just copied the above code and was concentrating on only if(array[i].equals(input)) { // Modified line. Edited. Thanks for the input. – Mohan Mar 22 '14 at 06:47
  • actually i have taken this string from file and yes it is in this form. but all techniques i applied but no printing – user3440716 Mar 22 '14 at 06:47
  • @user3440716 If it comes from a file, maybe there are some extra characters (like whitespaces), have you tried to `trim()` it ? – Florent Bayle Mar 22 '14 at 07:56
  • i have done trim. but am near to complete the code by placing user inpts and displayingthe output – user3440716 Mar 22 '14 at 08:19
0

Did you try Integer.parseInt(...) while comparing strings with numeric values?

Integer.parseInt("26") == Integer.parseInt("56")

Update

You can also try comparing strings with .equals() method, as below.

array[i].equals(input);

Also, use println() instead of print() to be sure of what is printing.

Shishir

Shishir Kumar
  • 7,981
  • 3
  • 29
  • 45
  • if(Integer.parseInt(stringArr[i])== Integer.parseInt("input")){ System.out.print(array[i-1]); System.out.print(array[i]); } ; Giving errro – user3440716 Mar 22 '14 at 06:54