This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

parsing strings  RSS feed

 
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm stuck on an assignment for school.

Here is the scenario as it's presented:
(1) Prompt the user for a string that contains two strings separated by a comma.
(2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings.
(3) Extract the two words from the input string and remove any spaces. Store the strings in two separate variables and output the strings.
(4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit.


I have been able to get my code to work, up to the point where it looks for additional input. I have inserted a break statement at line 30 to prevent an infinite loop, but of course, this also stops everything.

How do I get the loop to keep looking for additional input until 'q' is entered? Do I need to create a character variable?

Here is my code as written:



I have been beating my head against the desk for about 2 hours trying to figure this out. I'm sure it's something simple that I'm missing, but.......
 
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you get into an infinite loop without the break is because when the code enters that second (else) block there is nothing to take in more user input, so it's left with the current value, outputting it again and again and again.

So...how about asking for new input?

A couple of other things.
I would use equals rather than contains for the while condition.  After all, what if the user input "quite,indeed"?
And the else clause doesn't need an if.  We already know that the userInput contains a comma, otherwise it would have entered the if clause.
 
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:The reason you get into an infinite loop without the break is because when the code enters that second (else) block there is nothing to take in more user input, so it's left with the current value, outputting it again and again and again.

So...how about asking for new input?

A couple of other things.
I would use equals rather than contains for the while condition.  After all, what if the user input "quite,indeed"?
And the else clause doesn't need an if.  We already know that the userInput contains a comma, otherwise it would have entered the if clause.



I have taken your suggestion. I have changed the "contains" to "equals" (Line 17). It doesn't seem to have much effect on the functionality, but I do understand what you're saying.

The same goes for the else if at line 24. I have changed it to just "else".

I have removed the break at line 30 and replaced it with another userInput statement as well as re-indexed the comma.

I have also changed the "println" to "printf" within my else statement.

Here is my updated code.



What I am encountering now is proper spacing.

If the input is Allen , Jane it is not picking up the extra spaces. How do I make it so that they're being picked up in the output as well?

Thanks again for all of your help
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:After line 22 you have to re-compute the indexOf(",") for the new input.

 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:After line 22 you have to re-compute the indexOf(",") for the new input.



Isn't that what I'm doing in line 35?

My output is picking up the extra spaces, the "expected output" doesn't have them.

The yellow marks in the attached screenshots represent whitespace.



screenshot1.jpg
[Thumbnail for screenshot1.jpg]
screenshot2.jpg
[Thumbnail for screenshot2.jpg]
screenshot3.jpg
[Thumbnail for screenshot3.jpg]
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill Platt wrote:

Carey Brown wrote:

Carey Brown wrote:After line 22 you have to re-compute the indexOf(",") for the new input.


Isn't that what I'm doing in line 35?


No. On line 22 you get new input but line 35 is not reached because it's inside the 'else'.

You need to trim() these.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the space before the new-line?
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could be simplified with a bit of re-arrangement.
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Bill Platt wrote:

Carey Brown wrote:

Carey Brown wrote:After line 22 you have to re-compute the indexOf(",") for the new input.


Isn't that what I'm doing in line 35?


No. On line 22 you get new input but line 35 is not reached because it's inside the 'else'.

You need to trim() these.



We haven't covered the trim(), I will have to read up on it. I'm not sure how it works yet.

As for the extra spaces before the new line, it seemed like the right thing to do, apparently, my thinking is flawed.


 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help Carey.

I got it

I changed lines 10 and 11


It passed all of the checks from zyBooks.
 
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using printf(), prefer %n instead of \n. Same result but more consistent with format string conversion specifiers. %n is specifically for a line separator.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's your code cleaned up a little bit:

Main differences to note:

1. Didn't need to declare index, firstInput, and secondInput where you had them before. Prefer to make the scope of variables as small as possible. You're only using index, firstInput, and secondInput in the one conditional block of code so it's safer to declare them right there. A wider scope than necessary tends to open you up to bugs, as you saw with your index variable in your earlier buggy code. Here, index is declared close to where it's being used.

2. Renamed variables to firstWord and secondWord. You could get away without these variables but that would be at the cost of some clarity in the code.

3. Did the trim() with the substring() to get the value of firstWord and secondWord in one shot. The display statements are just, well, displaying the values.

4. Extracted the prompt to a separate method. That way, you only need to change in one place if you want to change the prompt text.

5. Flipped the conditional blocks around to avoid use of negation in the if-statement. Also, that makes the happy case the primary focus rather than the error case.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you make a habit of doing refactoring and extracting little bits of functionality into their own methods, your code becomes better organized and cleaner. I find well-factored code more enjoyable to read:
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Main differences to note:

1. Didn't need to declare index, firstInput, and secondInput where you had them before. Prefer to make the scope of variables as small as possible. You're only using index, firstInput, and secondInput in the one conditional block of code so it's safer to declare them right there. A wider scope than necessary tends to open you up to bugs, as you saw with your index variable in your earlier buggy code. Here, index is declared close to where it's being used.

2. Renamed variables to firstWord and secondWord. You could get away without these variables but that would be at the cost of some clarity in the code.

3. Did the trim() with the substring() to get the value of firstWord and secondWord in one shot. The display statements are just, well, displaying the values.

4. Extracted the prompt to a separate method. That way, you only need to change in one place if you want to change the prompt text.

5. Flipped the conditional blocks around to avoid use of negation in the if-statement. Also, that makes the happy case the primary focus rather than the error case.





Thank you Junilu.

I see what you did, and it's much appreciated. I suspect that is the difference between someone who's proficient and someone who's been dabbling for a mere 4 weeks.

The flipping of the if/ else makes a lot of sense. It's not something that I thought of, but I can see where it makes it easier to read and understand.

The additional classes also make a lot of sense, they're just a little beyond the scope of the assignment (or my abilities at this point--- take your pick).
I'm working on expanding my knowledge though.

Thanks again
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!