Another simple Java Question from a newbie.

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Re: Another simple Java Question from a newbie.

Post by TheMindRapist on Sat Jul 24, 2010 8:48 pm
([msg=42646]see Re: Another simple Java Question from a newbie.[/msg])

sanddbox wrote:That was probably added by mistake - using next is fine since "yes" or "no" is only one word.

No, I think it is better to use nextLine in place of next. I believe his error was that by using next he read the line without advancing to the next line, then when the loop was ran next there was nothing left in the line to assign to sentence. sentence then became null, thus a length of 0, thus an IndexOutOfBounds error. I could be wrong though, it's been a few years since I used Java.

Edit: s/a/an

Editx2: I should have mentioned that redeclaring/making a local variable the Scanner object worked because then it's new, so it starts with the next line of input, kind of artificially moving past the end of the reply line, even though next was called on the reply line. Not sure if that made sense, I had trouble coming up with a better wording than "making it new makes it new" :P
Last edited by TheMindRapist on Sat Jul 24, 2010 9:04 pm, edited 2 times in total.
Image
User avatar
TheMindRapist
Contributor
Contributor
 
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by sanddbox on Sat Jul 24, 2010 8:53 pm
([msg=42647]see Re: Another simple Java Question from a newbie.[/msg])

TheMindRapist wrote:
sanddbox wrote:That was probably added by mistake - using next is fine since "yes" or "no" is only one word.

No, I think it is better to use nextLine in place of next. I believe his error was that by using next he read the line without advancing to the next line, then when the loop was ran next there was nothing left in the line to assign to sentence. sentence then became null, thus a length of 0, thus a IndexOutOfBounds error. I could be wrong though, it's been a few years since I used Java.


That would actually make sense. I guess we didn't have to initialize a new scanner every time :P
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2337
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by TheMindRapist on Sat Jul 24, 2010 8:53 pm
([msg=42648]see Re: Another simple Java Question from a newbie.[/msg])

Amidda1217 wrote:Here is the final code "I think." I also added a reset of question and exclam at the end of the "do" to make the responses correct when the type of response changes.

This isn't a big deal, but it's pointless to do assign true to answer each time reply is "yes" - answer starts off as true, and when it becomes false the loop and program ends. That's why in my revised code I checked if reply wasn't equal to yes. Also, perhaps even simpler would be to set reply to "yes" initially and then have the condition of the loop be

Code: Select all
while(reply.equalsIgnoreCase("yes"))

which would cut out a variable from your code. Like I said in my last post though, I don't know if this is for an assignment and you have to practice booleans X number of times.
Image
User avatar
TheMindRapist
Contributor
Contributor
 
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by Assassian360 on Sat Jul 24, 2010 9:36 pm
([msg=42649]see Re: Another simple Java Question from a newbie.[/msg])

TheMindRapist wrote:
sanddbox wrote:That was probably added by mistake - using next is fine since "yes" or "no" is only one word.

No, I think it is better to use nextLine in place of next. I believe his error was that by using next he read the line without advancing to the next line, then when the loop was ran next there was nothing left in the line to assign to sentence. sentence then became null, thus a length of 0, thus an IndexOutOfBounds error. I could be wrong though, it's been a few years since I used Java.


Yeah, TheMindRapist is right. You will encounter a very similar issue when trying to use the nextInt() or nextDouble() methods. Which is quite possible if you are doing some kind of course. Since it will take the relevant number/s out but it will leave it an an end of line (even though it skips over properly when you keep calling nextInt()). Causing this crash due to 0 string length from using nextLine(). I see lots of students try to get around this by creating new Scanner objects. If you know that after a number has been entered you are going to be entering a string, you can have a line:

keyboard.nextLine();

By itself, and that will clear the stream to be starting at the next line. I forget whether there is a better way.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by Amidda1217 on Sat Jul 24, 2010 10:03 pm
([msg=42650]see Re: Another simple Java Question from a newbie.[/msg])

Code: Select all
reply = keyboard.nextLine();
          if (reply.equalsIgnoreCase("yes")) {
              answer = true; }
else {
        answer = false;}
            //System.out.println(sentence.charAt(length - 1)); //prints last char of sentence


instead of that i switched it to

Code: Select all
boolean answer = true;
if (reply.equalsIgnoreCase("no")){
answer = false;}


i can tell you guys are going to help me a lot at becoming more efficient. Thank you very much for all of the help.
The user can't be patched.
Amidda1217
New User
New User
 
Posts: 31
Joined: Thu Jun 04, 2009 6:49 am
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by sanddbox on Sat Jul 24, 2010 10:05 pm
([msg=42651]see Re: Another simple Java Question from a newbie.[/msg])

You might as well instantiate answer up at the start of the method.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2337
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by Amidda1217 on Sat Jul 24, 2010 10:18 pm
([msg=42652]see Re: Another simple Java Question from a newbie.[/msg])

sanddbox wrote:You might as well instantiate answer up at the start of the method.

sorry i didn't mention that. It was already initiated before, i just changed it from false to true.
The user can't be patched.
Amidda1217
New User
New User
 
Posts: 31
Joined: Thu Jun 04, 2009 6:49 am
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by sanddbox on Sat Jul 24, 2010 11:57 pm
([msg=42653]see Re: Another simple Java Question from a newbie.[/msg])

Amidda1217 wrote:
sanddbox wrote:You might as well instantiate answer up at the start of the method.

sorry i didn't mention that. It was already initiated before, i just changed it from false to true.


I know - it just seems unnecessary to set it to true everytime "yes" is answered. If it's true by default all you have to do is change it to false when they say "no". Still, it makes no difference.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2337
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by TheMindRapist on Sun Jul 25, 2010 12:51 am
([msg=42658]see Re: Another simple Java Question from a newbie.[/msg])

sanddbox wrote:I know - it just seems unnecessary to set it to true everytime "yes" is answered. If it's true by default all you have to do is change it to false when they say "no". Still, it makes no difference.

I think (hope) that he meant the line

Code: Select all
boolean answer = true;

is outside the loop, at the beginning of his program, although that isn't at all clear from his post. I might be wrong though.
Image
User avatar
TheMindRapist
Contributor
Contributor
 
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by Amidda1217 on Sun Jul 25, 2010 4:03 am
([msg=42665]see Re: Another simple Java Question from a newbie.[/msg])

TheMindRapist wrote:
sanddbox wrote:I know - it just seems unnecessary to set it to true everytime "yes" is answered. If it's true by default all you have to do is change it to false when they say "no". Still, it makes no difference.

I think (hope) that he meant the line

Code: Select all
boolean answer = true;

is outside the loop, at the beginning of his program, although that isn't at all clear from his post. I might be wrong though.

Yes, that is exactly what I meant. I will try and make my posts more clear. boolean answer = true; is at the beginning of my program (outside the loop.)
The user can't be patched.
Amidda1217
New User
New User
 
Posts: 31
Joined: Thu Jun 04, 2009 6:49 am
Blog: View Blog (0)


Previous

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests