开发者

How can I extract the numbers from a string only using charAt(), length() and/or toCharArray() in Java

开发者 https://www.devze.com 2023-01-18 18:47 出处:网络
I have to do this for an assignment in my java class. I have been searching for a while now, but only find solutions with regex etc.

I have to do this for an assignment in my java class. I have been searching for a while now, but only find solutions with regex etc.

For my assignment however I may only use charAt(), length() and/or toCharArray(). I need to get from a string like gu578si300 for example just the numbers so it will become: 578300.

i know numbers are 48 - 57 in ASCII but i can't figure out how to do this in java. You guys any ideas?

i was thinking about a for loop that checks whether the (int) char is between 48-57 en if so puts the value into a seperate array. Howeevr i dont know how to programm that last thing.

I now have this;

public static St开发者_运维技巧ring filterGetallenreeks(String reeks){
    String temp = "";

    for (char c : reeks.toCharArray()) {
        if ((int) c > 47 && (int) c < 58)
            temp += c;
        }

    return temp;

however it is not working, it just outputs the same as goes in.

is it something in my mainm which looks like this. If i'm right the return temp; will return the temp string into the reeks string in the main right? why is my input still the same a sthe output?

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Voer een zin, woord of cijferreeks in:");
    String reeks = sc.nextLine();

    if (isGetallenreeks(reeks)){
        System.out.println("is getallenreeks");
        filterGetallenreeks(reeks);
        System.out.println(reeks);
    }


Since this is homework I will not be providing the complete solution, however, this is how you should go about it:

Do a for loop that iterates for the total amount of characters within the string (.length). Check if the character is a digit using the charAt and isDigit methods.


You could do a loop that checks a character in the string, and if it's a number, append it to another string:

//I haven't tested this, so you know.
String test = "gu578si300 ";
String numbers = "";
for(int i=0; i<test.length(); i++){
   if("0123456789".indexOf(test.charAt(i)) // if the character at position i is a number,
      numbers = numbers + test.charAt(i);  // Add it to the end of "numbers".
}
int final = Integer.parseInt(numbers);     // If you need to do something with those numbers,
                                           // Parse it.

Let me know if that works for you.


It seems like a reasonable approach, but I'd make a couple of changes from what you suggested:

  • If you need to result as a string then use a StringBuilder instead of an array.
  • Use character literals like '0' and '9' instead of ASCII codes to make your code more readable.

Update

The specific problem with your code is this line:

temp = temp + (int)c;

This converts the character to its ASCII value and then converts that to a decimal string containing the ASCII value. That's not what you want. Use this instead:

temp += c;
0

精彩评论

暂无评论...
验证码 换一张
取 消