How to see if a string has more numbers than other symbols in java

回眸只為那壹抹淺笑 提交于 2019-12-11 11:33:59

问题


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?


回答1:


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.




回答2:


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()));

    }



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/3850495/how-to-see-if-a-string-has-more-numbers-than-other-symbols-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!