![]() |
|
[Java] Don't understand Java error code - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Don't understand Java error code (/showthread.php?tid=5432727) |
Don't understand Java error code - Louis C.K. - Oct 13, 2016 http://prnt.sc/ctkjr5 write a program to calc are and c of a circle, user input c for circum a for area. I need help, I will even pay for this. skype: eiffel.towerhf * - ka$h - Oct 13, 2016 Do you mind posting your code * - Louis C.K. - Oct 13, 2016 (Oct 13, 2016 05:56 PM)Ka$h Wrote: Do you mind posting your code http://pastebin.com/Satfvryw are u proficient in java? * - ka$h - Oct 13, 2016 Code public static void main(String[] args) {notice the extra key.nextLine, I am not exactly sure how to explain the details behind why it happens hopefully this helps you: http:// stackoverflow. com/questions/14452613/issues-with-nextline * - shashwat97 - Oct 13, 2016 This works (changed just two things, the way of taking input, as already pointed out above, and added a break in switch case) Code /*Sample runs- Code me@kali:~$ java circle.Circle * - randomized patterns - Oct 15, 2016 You should use toLowerCase and not have to worry about caps for your switch statement. It is more efficient and your switch statement will look clean. * - Reece Morgan - Oct 16, 2016 You shouldn't be using nextLine in this scenario. You should be using next. nextLine will move forward to the next line, and return anything that came before that next line. However the position of the scanner will still be set to the next line, so when you call charAt(0), your asking for the character at index 0 of the line AFTER your input (which is blank). Edit: I think that explanation is correct, although I could be wrong as I didn't actually test my theory. * - Stealth™ - Oct 16, 2016 (Oct 16, 2016 09:04 AM)Reece Morgan Wrote: You shouldn't be using nextLine in this scenario. You should be using next. Your explanation is only valid because OP is calling Scanner#nextLine twice for some reason. If he were calling it once, then everything would work :D |