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?
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.
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.
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.
Jeff Sak wrote:. . . since I am a lot newer to Java I use code closer to C++.
It does no good to disparage your teachers.
. . . He actually barely teaches in class . . .
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.