tags:

views:

95

answers:

4

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 String 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);
    }
+2  A: 

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.

npinti
Note that IsDigit (http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Character.html#isDigit%28char%29) is not the same as testing if the character is between '0' and '9'. Making this change could introduce (or fix, I guess) a subtle bug.
Mark Byers
+1  A: 

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;
Mark Byers
You and Claus are forgetting Java generics don't support primitive types. `ArrayList<Character>`.
Mark Peters
@MarkPeters: Thanks for your comment, but actually I changed my mind - now I prefer StringBuilder for this task. If he wants the result as a string then it is unnecessary to go via ArrayList first.
Mark Byers
Yes i do need to have a string in the end. However i van only use the above three methods. And i think the purpose is to learn for loops etc. Can it be deno with those three?
sjaak
already solved it thanks for the help!
sjaak
A: 

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.

Salem
thanks for the input, i tried it but the compilergives this error (after modifieing it so it fits into my program). opgave4.java:98: ')' expected if("0123456789".indexOf(reeks.charAt(i)) // if the character at position i is a number, ^1 error
sjaak
where does it expect the )?
sjaak
actually i update my problem, could you take a look again why it is not working. It now puts out the exact same i put in, not only the numbers.
sjaak
already solved it thanks for the help!
sjaak
Haha sorry I didn't respond in time. I know I didn't actually test that code, it was just some rough scratchwork.
Salem
A: 

You should add the character, not the number: instead of temp = temp + (int) c; you should write temp = temp + (char) c; Also, why not using return temp instead of reeks = temp; return reeks?

MByD
i changed it, but it still doesn;t work. I am now thinking it has something to do with the returning of my string to the main, in which it prints it. Can you look at my updated problem? thanks!
sjaak
ok i am stupid, already got it to work with this line:reeks = filterGetallenreeks(reeks);
sjaak