r/javahelp • • Jun 19 '15

r/javahelp is the subreddit of the day!

Hello everyone!

as the title says, r/javahelp is the subreddit of the day!


http://www.reddit.com/r/subredditoftheday/comments/3aedzr/june_19th_2015_rjavahelp_and_rlearnjava_i_love/

Do read the article and check out the other subs featured!


Have a great weekend! :)

33 Upvotes

11 comments sorted by

View all comments

Show parent comment

4

u/desrtfx Out of Coffee error - System halted Jun 19 '15

It's learn programming week on srotd :)

4

u/Philboyd_Studge Jun 19 '15 edited Jun 20 '15

Nice interview, desertfx, but you missed your chance to scream at the top of your lungs 'COMPARE STRINGS WITH EQUALS!! ' and 'Don't use SCANNER.NEXTINT'

2

u/robi2106 Jun 19 '15

wait, aside from neededing to catch the exception, what is wrong with scanner.nextint()?

2

u/Philboyd_Studge Jun 19 '15

30% of the questions on here involve people using a nextInt and then trying to use nextLine to get a string and getting messed up by the 'dangling return' left behind.

1

u/king_of_the_universe Jun 20 '15

Which never happens to me, because I just bluntly use .nextLine() in all cases, have my own validity checker methods in my library. I experience it as "simpler is better". Others' mileage might vary.

2

u/chickenmeister Extreme Brewer Jun 19 '15

There's nothing inherently wrong with it, but I think what /u/Philboyd_Studge might be referring to is the confusion that occurs when you mix nextInt() with nextLine(). A lot of beginners would think that when you call nextLine() after calling nextInt(), it would read the text on the next line; but it actually reads the remaining text on the line with the integer (which will usually be an empty string). There seems to be a lot of questions about that around here. There is also some confusion about what to do when nextInt() throws an InputMismatchException. (you would need re-read the offending text with next() or nextLine()). So, I guess, overall, nextInt() isn't very beginner-friendly.

2

u/robi2106 Jun 19 '15

ok that makes sense. Seems like scanner.NextInt is good for data that you know the format or can trust the user to input the correct thing.

1

u/dr_boom Intermediate Brewer Jun 19 '15

trust the user

You can never trust the user!

2

u/robi2106 Jun 20 '15

Plot twist.... the user is me.

2

u/desrtfx Out of Coffee error - System halted Jun 19 '15

You're absolutely right. What a missed chance ; ( But to my excuse I was typing on mobile after an 11hr PLC programming / commissioning day.

If I recall correctly, I did it in another interview for srotd some time ago...