0

How to find if there is a " , " " ? " " ! " " ; " " . " in a list of characters the user must enter, I made a while loop and i broke it when the user enters any number from 0 to 9 ..

Sample run :

Enter any character (a digit 0-9 to stop): a B , R x u ! @ . C W 2
The list you entered contains 3 punctuations signs. 

part of what i have done

    int count = 1;
    while ( count > 0 )
    { 
        Scanner input = new Scanner(System.in);
        System.out.print("Enter any character and a digit 0-9 to stop: ");
        char ch = input.next().charAt(0);
        if ( ch>=0 && ch<=9)
           break;
    }

original q. :

program that keeps prompting the user to enter a character different than a digit. The first digit entered by the user stops the input and the program should then display the number of punctuations characters entered (one of this list ! . , ; ?). When none is found, display a message “Characters entered without punctuation”.

XerXes
  • 37
  • 2
  • 11

2 Answers2

0

Scanner is not a class I have used a lot and irrelevant to the question so I'm going to ignore that in my code here and assume you can do those parts on your own.

First you can fix your infinite loop here:

int count = 1;
while ( count > 0 ) // count is never changed
{
    // ~~

    char ch = /* ~~~~ */;
    if ( ch>=0 && ch<=9) // Unicode codes 0-9 are non-characters
       break;
}

Instead you can do this:

while (true)
{
    // ~~

    char ch = /* ~~~~ */;
    if (ch >= '0' && ch <= '9')
       break;
}

while (true) is not a bad thing to do as long as your own exit condition is not vague and it works.

For checking punctuation you can devise your own logic. There are three simplest solutions I can think of to check if a character is punctuation:

String punctuationAsString = "!.,;?";

char[] punctuationAsArray = {
    '!', '.', ',', ';', '?'
};

while (true)
{
    // ~~

    char ch = /* ~~~~ */;
    if (ch >= '0' && ch <= '9') {
       break;
    }

    // simple one line
    if (punctuationAsString.contains(ch)) {
        System.out.println("is punctuation");
    } else {
        System.out.println("not punctuation");
    }

    // String#contains basically does this
    boolean punc = false;
    for (int i = 0; i < punctuationAsArray.length; i++) {
        if (ch == punctuationAsArray[i]) {
            punc = true;
            break;
        }
    }

    System.out.println((punc ? "is" : "not") + " punctuation");

    // verbose but clear
    switch (ch) {
        case '!':
        case '.':
        case ',':
        case ';':
        case '?': System.out.println("is punctuation");
                  break;

        default:  System.out.println("not punctuation");
    }
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
0

try this:

    int count = 0;
    String userInput = "a B , R x u ! @ . C W 2";  

    if(userInput.matches("^[^\\d].*")){
        Pattern p = Pattern.compile("[!,./;?]");
        Matcher m = p.matcher(userInput);
        while (m.find()){
            count++;
        }
    }
Vipul Paralikar
  • 1,508
  • 10
  • 22