Hi,
I was trying to learn about dealing with Strings. I came across with StringTokenizer. It seemed like it makes things easier, but I cannot seperate a text if I put a space between words. Here is my code for it:
When I try this, I put "Here we go, trying to use String,Tokenizer. Hope the best!", it only shows "Here" as the output. That's why I included lines to count the tokens. And it seems I only have 1 tokenizer here.public class Tokening { String str; Scanner input; StringTokenizer token; public void methodOne(){ input = new Scanner(System.in); System.out.println("Enter some text here:"); str = input.next(); token = new StringTokenizer(str,":;., "); int tokenCount = token.countTokens(); System.out.println(tokenCount); while(token.hasMoreTokens()){ String z = token.nextToken(); System.out.println(z); } } }
Looking deeper into seperating strings, I found out that there is a String method called "split". But this didn't help too. I was trying to use more than one delimiters. It failed. I was convinced one would enough, but it produced the same output as StringTokenizer. Here is the code for this - In the same class, so the variables are the same:
public void metotIki(){ input = new Scanner(System.in); System.out.println("Enter some text here:"); str = input.next(); String[] stg = str.split(","); for(int x=0;x<stg.length;x++){ System.out.println(stg[x]); }
So, I want to seperate text when the user puts a comma, a period and the other signs like ; : ? and I want to do it whether there are whitespaces between the words. How can I do that?