-1

I am trying to create a program in java which asks the user to enter a string, the number equivalent of the string is then given as the output. eg ABC(2), DEF(3), GHI(4), JKL(5), MNO(6), PQRS(7), TUV(8), WXYZ(9). my code is below. The problem is once the user inputs their string to be converted I get no output(and no errors). I was wondering if someone could have a look and give me some advice. what i am trying to do is turn the string that the user has entered into an array and then use a for loop with nested if statements to go through the array and whatever letter there is in [i] give the equavilent number. but the system.out isnt working?

import java.util.Scanner;


public class keyPad {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Enter a string:");
    Scanner userInput = new Scanner(System.in);     
    String convertString = userInput.next();

    int length = convertString.length();
    String[] stringArray = new String[length];


    for(int i=0; i<length; i++){

        if(stringArray[i] == "a" || stringArray[i] == "b" || stringArray[i] == "c"){
            System.out.println("1");
        }

        if(stringArray[i] == "d" || stringArray[i] == "e" || stringArray[i] == "f"){
            System.out.print("2");
        }

        if(stringArray[i] == "g" || stringArray[i] == "h" || stringArray[i] == "i"){
            System.out.print("3");
        }

        if(stringArray[i] == "j" || stringArray[i] == "k" || stringArray[i] == "l"){
            System.out.print("4");
        }

        if(stringArray[i] == "m" || stringArray[i] == "n" || stringArray[i] == "o"){
            System.out.print("5");
        }

        if(stringArray[i] == "p" || stringArray[i] == "q" || stringArray[i] == "r" || stringArray[i] == "s"){
            System.out.print("6");
        }

        if(stringArray[i] == "t" || stringArray[i] == "u" || stringArray[i] == "v"){
            System.out.print("7");
        }

        if(stringArray[i] == "w" || stringArray[i] == "x" || stringArray[i] == "y" || stringArray[i] == "z"){
            System.out.print("8");
        }
    }
}

}
Ciaran
  • 697
  • 1
  • 12
  • 35

2 Answers2

0

Instead of using == in your if statements, use string1.equals(string2); Look at this stackOverflow answer for more: How do I compare strings in Java?

You also need to add the input string into the array which you are checking.

Community
  • 1
  • 1
MLavrentyev
  • 1,827
  • 2
  • 24
  • 32
  • 1
    something like this stringArray[i].equals("a") – Ciaran May 02 '15 at 00:43
  • Yes, exactly. A String is an object, and using `==` checks if the two reference variables are referring to the same object (which they are not in your case). The `.equals()` on the other hand, checks if these two `String` objects are carrying the same information. – MLavrentyev May 02 '15 at 00:45
  • i tried doing that and now i get an error Exception in thread "main" java.lang.NullPointerException at Ex17.main(Ex17.java:19) – Ciaran May 02 '15 at 00:49
  • This is because you haven't added anything to the array you are using. Try adding the inputs into string array you are using to check everything. Because there is nothing in there, there is a NullPointer Exception – MLavrentyev May 02 '15 at 00:50
  • so would i have to create another for loop to go through the entered string and put each letter into the array? im pretty new to java and not very good with arrays :/ – Ciaran May 02 '15 at 01:01
  • @Phil Yes. Except not character, but word because `userInput.next();` gets the next word (up to a space). – MLavrentyev May 02 '15 at 01:50
0

In your program you take the input from the user but you havent put it in the array you are comparing .The string array is empty.So you are not getting an output.