Friday, 6 September 2013

How do I find what's inbetween " " in an inputted string?

How do I find what's inbetween " " in an inputted string?

I'd like to retrieve whatever is in quotes that someone enters as a
string. I'm assuming I need to set a boolean value in the if statement and
do something with that but I'm not sure how.
When the user inputs a string mixed with words and numbers all separated
by one space:
hey 110 say "I am not very good at Java" but " I can fish pretty well"
I want to determine what's a word, what's a digit and what's inside a quote.
I'm having trouble with the quote part
I want to be able to take the "I am not very good at Java" and the "I can
fish pretty well" and print out what's inside the quotes, but there can be
multiple quotes in the string.
I can't use
split,
trim,
tokenizer,
regex,
or anything that would make this really easy unfortunately also I can't
scan this twice so using loops inside my if is not ideal.
Below is what I have now. It's all in this method where I try to identify
if something in the string is a word, number or a quote:
class Bean {
int num;
String word;
String quote;
String userInput;
public Bean(String userInput)//
{
num=0;// initializing used variables and fields
this.userInput=userInput;
}
public void set(String userInput)// method set returns void
{
num=0;// reset each variable so new input can be passed
String empty="";
String wordBuilder="";
userInput+=" ";
String quoteBuilder="";
boolean quote=false;
for(int index=0; index<userInput.length(); index++)// goes through
each character in string
{
if(Character.isDigit(userInput.charAt(index)))// checks if
character in the string is a digit
{
empty+=userInput.charAt(index);
}
else {
if (Character.isLetter(userInput.charAt(index))) {
wordBuilder += userInput.charAt(index);
} else {
if (userInput.charAt(index) == '"') {
quote = true;
}
if (quote = true) {
quoteBuilder += userInput.charAt(index);
}
}
}
//if it is then parse that character into an integer and
assign it to num
num = Integer.parseInt(empty);
word = wordBuilder;
empty = "";
wordBuilder = "";
}
}
}

No comments:

Post a Comment