views:

123

answers:

5

So for an assignment I have to make a programm that asks for a input of a string and then detects palindromes.

thing is, also numbers can be put in. When more than half of the input of the string is a number it needs to regard the string as a numeric string and disregard the other symbols.

So what i thought is to put the input string into an array then look for the numbers (ASCII# between 48 and 57) and count those. Afterwards compare the number of Numbers vs number of Letters and see which one has more.

however, i can't seem to programm the thing that it counts the numbers in a string. can someone help me, i have this already:

public class opgave41 {



public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("input  a string:");
    String reeks = sc.nextLine();
    char[] array1 = reeks.toCharArray();

    int numbers;
    int other;

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

        if (int array1[i] < 57 || int array1[i] > 48) 
            numbers++;

        else
            other++;

    }
    System.out.prinln(numbers);
    System.out.prinln(other);
}

}

if i compile it I get this:

opgave41.java:38: '.class' expected
        if (int array1[i] < 57 || int array1[i] > 48) 
                            ^

opgave41.java:39: ')' expected numbers++;
^ 2 errors

how can i get this to worK?

A: 

You're checking array1.length which is the length of the character array. I don't think that's what you intend. You want array1[i] instead to get the current character. Also, I think you have this backwards:

if (int array1.length < 57 || int array1.length > 48) 

Assuming you fix this to use array1[i], it will still be checking that the current character is not a number. But then you proceed to increment the numbers counter. Furthermoer, you may also consider using the Character.isDigit() method.

Kirk Woll
A: 
  1. You are accessing array1.length, which never changes and is not a character.

  2. You got it backwards, characters < 57 or > 48 are NOT numbers.

  3. Use (int) to do casts.

I would do this:

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

Or a version that creates no garbage and uses no casts:

for (int i=0; i<reeks.length(); i++) {
    if (reeks.charAt(i) >= '0' && reeks.charAt(i) <= '9')
        numbers++;
    else
        others++;
}
Rogue
ok i changed things into this but then it says the variable might not be initialized.. i initialized it with int numbers; right?
sjaak
initialize it with `int numbers=0, others=0;`
Rogue
ok im stupid, have to assign a value to it. thanks it is working now!
sjaak
A: 

The logic is ok*, you just need to use the elements of the char array instead of the array length. So just replace

  if (int array1.length < 57 || int array1.length > 48) 

with

  if (array1[i] <= '9' && array1[i] >= '0') 

(note the && since you want both conditions to be true. If you use || it will mean less that 9 OR more than 0 which is true for any char)

* besides some syntactic errors

ring0
+1  A: 

After fixing the obvious syntactical errors, I got this code based on yours:

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("input  a string:");
    String reeks = sc.nextLine();

    int numbers = 0;
    int other = 0;

    for (char c : reeks.toCharArray()) {
      if ('0' <= c && c <= '9')
        numbers++;
      else
        other++;
    }

    System.out.println(numbers);
    System.out.println(other);
  }

I also replaced the magic numbers 48 and 57 with character literals, since that makes the intention clearer.

Roland Illig
+1  A: 

No need for loops, checks etc, its much easier with a regex in place for numbers.

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("input  a string:");
        String reeks = sc.nextLine();
        String symbols = reeks.replaceAll("[0-9]", "");

        System.out.println("others  - " + symbols.length());
        System.out.println("numbers - " + (reeks.length() - symbols.length()));

    }
johnbk