• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I end program on user input for a string  RSS feed

 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making an anagram solver and have come up with the code that works for it, but whenever I try to add code to the program that makes it end when the user inputs a blank line or other than two words in the line it doesn't work. I have been trying to use while loops and if else statements, but it doesn't seem to work because it is a string and not an integer. Should I use a break statement, or am I supposed to convert the string into an int or array? I'm not sure where to go from here.

 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Sakaida wrote:



Don't worry about that yet - first get your program to work correctly. Are hello and hello anagrams? No. Are hello and ehllo anagrams? No. But your program says they are both anagrams ...

Please ignore - confusing anagram with palindrome!
 
Jeff Sak
Ranch Hand
Posts: 38
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An anagram is a word that is made using the same letters as the first word, so technically hello and hello are anagrams, and the same goes for hello and elloh, seeing as they have the exact same letters in each word. If you were to try a word that really wasn't an anagram in this program, such as state and asset, it would come back as false which is correct. I made this program to come back with these results on purpose, so that aspect of it works. I just don't understand what method I should use to have the user decide when to end the program.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Sakaida wrote:An anagram is a word that is made using the same letters as the first word, so technically hello and hello are anagrams, and the same goes for hello and elloh, seeing as they have the exact same letters in each word. If you were to try a word that really wasn't an anagram in this program, such as state and asset, it would come back as false which is correct. I made this program to come back with these results on purpose, so that aspect of it works. I just don't understand what method I should use to have the user decide when to end the program.


Sorry, of course you're right, I was confusing anagram with palindrome! So sorry ...

Ok, so this will actually quit the program if you enter a "nonword", however, shouldn't the program actually give you another chance if you enter in something wrong?
 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you need or you should use System.exit in this or other similar programs.

Have you tried to use a loop?
while user input not an empty string, check if it is an anagram, if not, get another input. You might need to use trim method.

By the way, class names suppose to start with an upper case - you got differently at the moment.
Also you got missing curly braces in below line. It is error prone, and even worse - non readable. And why you got those spaces inside parenthesis? I've seen one guy writing code that way, I'm not sure it is common.
 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to use different loops, but for some reason it wasn't allowing it to compile because it was a string, probably because of how I input it(I'm still new to java). And when it did compile through, it still allowed me to click enter a bunch of times and make empty lines until I did input two words. I am trying to make it so the loop is continuous until they either enter an empty line or something other than two words, but it doesn't seem to work properly, and instead will only run until I get two words to work with. I was trying a while loop, and at least attempted to make it so that while there are two words, to enter if it is an anagram or not, but even still it only ran one time and terminated.
 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying while loops such as the one below, but it wouldn't compile through. I think because it only accounts for one string and not two, but I could be wrong. I also tried in.hasNextLine() and that didnt compile through either.

 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me with how to make a loop for a string? I keep thinking that I should put something that says while s1 has "" but that leaves room for an empty line also.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a big fan of Scanner. I find it to be tempermental. Scanner.hasNext() will block when you don't want it to. I came up with this but it is a bit verbose.
 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping to figure out how to incorporate the two variables into the main function, since I have already coded an anagram test around them. I was just hoping to find a quick two lines of code to run a loop around until the user didn't input any data. I was thinking of doing an if loop, kind of like the one I put in this reply, but I don't know how to incorporate both variables(s1 and s2 into it. I know I need to fix the system input for s1 and s2 to be before the if loop, but even when I did that it wasn't working. Any advice?

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Sak wrote:whenever I try to add code to the program that makes it end when the user inputs a blank line or other than two words in the line it doesn't work. I have been trying to use while loops and if else statements, but it doesn't seem to work because it is a string and not an integer.

That shouldn't make a difference.

However, assuming that you want your user to enter two words before they press the ENTER key, you could use in.nextLine() as Carey showed you.

Personally, I almost always use nextLine() when I'm dealing with keyboard input, because it saves a lot of the "gotchas" you get with other methods, and always leaves the Scanner in a state to accept another line.

You can always convert the result to numbers, booleans, whatever you need, after you've got the data, but not until.

Also: The simplest way to signal an error in a program is to throw an Exception, viz:
  throw new Error( "Fatal Error: " + msg );

I think this will also cause the program to exit with a non-0 return code, but I'm not absolutely sure.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this with some responmses of a JVM to various “kill” signals. But that looks to me like trying to write C code in Java┬«. Winston is correct; if you want to shut down the JVM early, throw an exception.
 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would make sense since I am taking a C++ and a Java class at the same time, and since I am a lot newer to Java I use code closer to C++. The only thing is the teacher for Java hasn't gone over how to use exceptions yet. He actually barely teaches in class and then gives us HW assignments that we have to go online to figure out. He wants us to use two variables(s1, s2) and for it to loop until there is a blank line. I don't expect anyone to code it for me, but I am having a lot of trouble finding how to end the code with a blank line online for two variables. I've made a boolean function for the anagram, but I don't know what to put in the main function to run the loop until nothing is entered.
 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried a do while loop and put do while(s1!="\n") but that didnt seem to work either, and instead just kept running until I put two words in.
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Sak wrote:. . . since I am a lot newer to Java I use code closer to C++.
That is like using German grammar in English. Don't. The two languages are completely different and the similarity of the syntax is liable to confuse you.
. . . He actually barely teaches in class . . .
It does no good to disparage your teachers.

You can easily get an empty line from Scanner#nextLine. This old post explains it. If you don't understand how to get empty lines, your program might go wrong. I suggest maybe a do loopDon't try that for reading files, but only for keyboard input. Note the simplest way to use a do loop has you reading the line in two place. There are other ways to do it with a while loop but the syntax looks pretty horrible.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't try that for reading files...

Actually (sorry Campbell) don't do that at all unless you always want "// ..." executed at least once.
is more usual (and classic Dykstra ).

Winston
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the correction. Winston. I was assuming that somebody entering data by hand would always write a first line.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!