-3

I have this homework in my school where I need to separate the numbers and the letters and print them apart. My problem is I can only return one value which is the number not the letters in my if/else statement, I need to return both values so I can print the numbers and the letters separately.

Any suggestion on how I could do it with only one method?

public static String Separation(String output) {
    String number = "";
    String letter = "";
    for (int i = 0; i < output.length(); i++) {
        char x = output.charAt(i);
        if (Character.isDigit(x)) {
            number += x;
        } else {
            letter += x;
        }
    }
    return number;
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    You can try this: https://stackoverflow.com/questions/6271731/whats-the-best-way-to-return-a-pair-of-values-in-java – joao86 Apr 04 '19 at 14:02
  • 1
    Unrelated: method names go camelCase in java, and should include a verb, in your case `separateDigitsAndLetters()` or something alike. – GhostCat Apr 04 '19 at 14:15
  • Just wondering: is something still unclear here, or why didn't you accept an answer so far? – GhostCat Jun 19 '19 at 13:26

4 Answers4

10

You could do things like:

  • return an array of String: return new String[] { number, letter };
  • or return a List of String: return Arrays.asList(number, letter)

Or, more OOP: you create a small class that holds two Strings, like:

class Entry {
  String number;
  String letter;
...

and then you return an instance of that class. Of course, you have to adapt the signature of your method accordingly, to use the desired return type instead of public String ....

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1
public class Result {
   private String number;
   private String letter;

   public Result(String number, String letter) {
      this.number = number;
      this.letter = letter;
   }

   public void setNumber(String number) {
      this.number = number;
   }

   public void setLetter(String letter) {
      this.letter = letter;
   }

   public String getNumber() {
      return number;
   }

   public String getLetter() {
      return letter;
   }
}

then

public static Result Separation(String output) {
   String number = "";
'  String letter = "";
   for (int i = 0; i < output.length(); i++) {
       char x = output.charAt(i);
       if (Character.isDigit(x)) {
           number += x;
       } else {
           letter += x;
       }
   }
   return new Result(number, letter);
}

Edited: Change from c# to java

  • Here it is @GhostCat, deleted because I saw there are almost the same answers here, so didn't wanted to overload with similar things. If you think it can be deleted, leave a comment. Cheers! – Milenko Jevremovic Apr 05 '19 at 09:41
  • 1
    You were still coming in early, and all of that makes sense, so just keep it ;-) ... the more interesting part would be to get the OP to accept one of the answers, but far too often the newbies just come in, ask a question, get an answer ... and walk away. – GhostCat Apr 05 '19 at 10:29
1

… I need to separate the numbers and the letters and print them apart.

Do you really need a static method that returns something?

public class WTF {

    String number;
    String letter;

    public void separation(String output) {
        number = "";
        letter = "";
        for (int i = 0; i < output.length(); i++) {
            char x = output.charAt(i);
            if (Character.isDigit(x)) {
                number += x;
            } else {
                letter += x;
            }
        }
    }

    public static void main(String args[]) {
        WTF wtf = new WTF();
        wtf.separation("2 to 5 the jury decides");
        System.out.println(wtf.number + " " + wtf.letter);
    }

}
fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
-2

You can use two separate lists, one for numbers and other for letters.