0

Hello I am new to java and I am having a hard time on converting the letters from strings to numbers for example, the user will input "I am Okay" and the output will become 901-13015-11-1-25 since, I = 9 space = 0 A = 1 M = 13 space = 0 O = 15 K = 11 A = 1 Y = 25. I tried using switch, but I am having a hard time, because the program will print what I input in the string, well here is the test code I created, I hope someone can help me or give me tips and advices.

import java.util.*;
public class New2
{
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Input the First string: ");
    String fs = input.nextLine();
    
    fstoInt(fs);
    System.out.println(fs);

}

public static int fstoInt(String fs) {
    int num = 0;
    switch (fs) {
    case "A":
        num = 1;
        break;
    case "B":
        num = 2;
        break;
    case "C":
        num = 3;
        break;
    case "D":
        num = 4;
        break;
    case "E":
        num = 5;
        break;
    case "F":
        num = 6;
        break;
    case "G":
        num = 7;
        break;
    }
    return num;
}
}
Marcel
  • 81
  • 6
  • 2
    You need to iterate the string one character at a time. – Andy Turner Jan 11 '21 at 14:19
  • Probably [best to avoid](https://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad) the wild card import `import java.util.*;` – costaparas Jan 11 '21 at 14:20
  • you can use `fs.charAt(i)-64` inside a for loop – ATP Jan 11 '21 at 14:21
  • Does this answer your question? [Java - how to convert letters in a string to a number?](https://stackoverflow.com/questions/15027231/java-how-to-convert-letters-in-a-string-to-a-number) – costaparas Jan 11 '21 at 14:23

1 Answers1

1

output will become 901-13015-11-1-25 since, I = 9 space = 0 A = 1 M = 13 space = 0 O = 15 K = 11 A = 1 Y = 25

I don't think you read the assignment properly.

That would mean that: AA becomes 11. And K... also becomes 11. Not sure this is what you wanted.

switch (fs) {
    case "A":

assuming it's all consecutive, with "A" becoming 1, and "Z" becoming 26: characters are just numbers (their unicode value), and those letters are in sequence. So, instead of a gigantic switch block, all you need is:

int num = fs.charAt(0) - 'A' + 1;

this will turn "A" into 1, and "Z" into 26.

As the comments already said, your problem is that fs isn't just "A". It's "Hello". You need to loop through the characters. As space gets special treatment (turning into 0), and presumably anything that isn't "A" - "Z" should crash:

for (char c : fs.toCharArray()) {
    int num = charToCode(c);
}

and then write charToCode:

public int charToCode(char c) throws IllegalArgumentException {
    if (c == ' ') return 0;
    if (c >= 'A' && c <= 'Z') return c - 'A' + 1;
    throw new IllegalArgumentException(
      "Only spaces and capital letters allowed");
}
rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • uhm I mean in my assignment all characters from A have the formula A = 0+1, B = 1+1, C = 2+1 and so on... – Marcel Jan 12 '21 at 09:49
  • Anyways thank you for the advice I learned something new from your comment and given code. – Marcel Jan 12 '21 at 10:06
  • @Marcel yes, and with that formula, A is 1, and K is 11, and if you just pile numbers one after the other with nothing in between, `AA` becomes `11` and `K` also becomes `11`. – rzwitserloot Jan 12 '21 at 14:11
  • Thank you, but now I am having trouble on reversing the strings because the output needs to be reverse, I tried using arrays but I cannot seem to get it working. – Marcel Jan 13 '21 at 04:02