user1781192
user1781192

Reputation: 7

detect space in string

How do I determine if a space is in the input entered by the user? The user's input is stored in a string. The part I'm referring to is in between the asterisks. I read that regex could be used but I do not know how to use it to detect if there is a space in the string s.

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class LAnalyze{

public static void main(String[] args) {

    int j = 0;
    String s = null;
    Scanner scan = new Scanner(System.in);


    System.out.println("Enter an input:  ");
    s = scan.nextLine();

    ****************************************************
              Pattern pattern = Pattern.compile("\\s");
    Matcher matcher = pattern.matcher(s);
    boolean found = matcher.find(); 

    if(spaceIsFound)
             *******************************************************
    {
        System.out.println("Invalid character(s) entered.. Program terminated!\n");
        System.exit(0);
    }
    else if( s.charAt(0)=='a'||s.charAt(0)=='b'||s.charAt(0)=='c'||s.charAt(0)=='d'||s.charAt(0)=='e'||s.charAt(0)=='f'||
            s.charAt(0)=='g'||s.charAt(0)=='h'||s.charAt(0)=='i'||s.charAt(0)=='j'||s.charAt(0)=='k'||s.charAt(0)=='l'||
            s.charAt(0)=='m'||s.charAt(0)=='n'||s.charAt(0)=='o'||s.charAt(0)=='p'||s.charAt(0)=='q'||s.charAt(0)=='r'||
            s.charAt(0)=='s'||s.charAt(0)=='t'||s.charAt(0)=='u'||s.charAt(0)=='v'||s.charAt(0)=='w'||s.charAt(0)=='x'||
            s.charAt(0)=='y'||s.charAt(0)=='z'||s.charAt(0)=='A'||s.charAt(0)=='B'||s.charAt(0)=='C'||s.charAt(0)=='D'||
            s.charAt(0)=='E'||s.charAt(0)=='F'||s.charAt(0)=='G'||s.charAt(0)=='H'||s.charAt(0)=='I'||s.charAt(0)=='J'||
            s.charAt(0)=='K'||s.charAt(0)=='L'||s.charAt(0)=='M'||s.charAt(0)=='N'||s.charAt(0)=='O'||s.charAt(0)=='P'||
            s.charAt(0)=='Q'||s.charAt(0)=='R'||s.charAt(0)=='S'||s.charAt(0)=='T'||s.charAt(0)=='U'||s.charAt(0)=='V'||
            s.charAt(0)=='W'||s.charAt(0)=='X'||s.charAt(0)=='Y'||s.charAt(0)=='Z') {

        System.out.print( (s) + ": Identifier\n");
        for (int k = 0; k < s.length(); k++) {  
            j++;
            System.out.println("Token " + (j) + " = " + (s.charAt(k)));

        }
    }
    else if(s.charAt(0)=='0'||s.charAt(0)=='1'||s.charAt(0)=='2'||s.charAt(0)=='3'||s.charAt(0)=='4'||s.charAt(0)=='5'||
            s.charAt(0)=='6'||s.charAt(0)=='7'||s.charAt(0)=='8'||s.charAt(0)=='9') {

        System.out.print( (s) + ": Unsigned Integer\n");
        for (int m = 0; m < s.length(); m++) {  
            j++;
            System.out.println("Token " + (j) + " = " + (s.charAt(m)));
        }
    }   
    else {
        System.out.println("Invalid character(s) entered.. Program terminated!\n");
        System.exit(0);
    }

}

}

Upvotes: 0

Views: 6739

Answers (2)

no id
no id

Reputation: 1672

You don't need to use regex just to check if string contains spaces. Use String.contains() instead.

Upvotes: 2

thedayofcondor
thedayofcondor

Reputation: 3876

You can use s.indexOf(' '); If it returns -1 the string does not contain spaces.

Also, you can use switch(s.charAt(0)){ case 'a': case 'b': ... break; case 'c': ... }

in place of the multiple if.

Upvotes: 4

Related Questions