+ 1

What the hell is wrong with my code output?

I'm using netbeans IDE 8.1 I made an app because i wanted to practice loops, and class interactions. and well, it didn't go as expected. this app was supposed to add 5 on "speed" when typed "Faster" -10 speed when typed "Slower" change the "Color" to "Black" and when i type "Check" it prints it out and "End" exits the program. but my app keeps refusing to give me output until it gives me glitched outputs. Help is appreciated. package vehiclesimulator; import java.util.Scanner; public class Bycicle { static int speed = 0; static int size = 10; static int increment = 5; static int decrement = 10; static String color = "yellow"; static void Printstates(){ System.out.println("speed:" + speed + " size:" + size + " color:" + color);} static void speedUp() { String INPUT; INPUT = scan.nextLine(); speed = speed + 5; } static void applyBrakes() { String INPUT; INPUT = scan.nextLine(); speed = speed - 10; } static void Paint(String PaintColor){ String INPUT; INPUT = scan.nextLine(); color = PaintColor; } static String Black; public static Scanner scan = new Scanner(System.in); private static void programStart(){ String INPUT; INPUT = scan.nextLine(); scan.nextLine(); if("Faster".equals(INPUT)){ speedUp(); Printstates(); } if("Slower".equals(INPUT)){ applyBrakes(); Printstates();} if("Paint".equals(INPUT)){ Paint(Black); Printstates();} if("Check".equals(INPUT)){ Printstates();} if("End".equals(INPUT)) System.exit(0); } public static void main(String[]args) { programStart(); String INPUT; INPUT = scan.nextLine();

2nd Jun 2017, 9:31 PM
MassiveMayhem
5 Respostas
+ 3
Likely your issue: Everytime you call a function it expects more input: " INPUT = scan.nextLine();" * This would be inside the speedUp, applyBrakes, and paint method. So, when you type: "Faster" You go to SpeedUp(); Now, "what will you type next?" Says the awaiting compiler. It doesn't look like you even use the input in those methods anyway, so you might as well remove them. PS: The code for the main method got cut off
2nd Jun 2017, 10:05 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
For starters you don't have a loop at all in this code, but, I'm guessing that you knew that and are trying to fix the current single state or iteration of this code. There are also several INPUT variables throughout the code that aren't used. The only one that is used is the one in the programStart() method. All the others can be removed as they are declared inside their own methods and are destroyed at the end of those methods without ever being used. You also have variables that aren't used, some which don't change when I think you may mean to, and 1 that is declared, but is never set before it is used (Black). I'll try and revise the code to a readable format so that it works the way I believe you intend and post in a few moments.
2nd Jun 2017, 10:10 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Here take a look at this https://goo.gl/9uB38Q It may behave a bit more like what you intended.
2nd Jun 2017, 10:55 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@ MassiveMayhem there are a lot of issues with your code i have copied it and ran it in my eclipse IDE and for some reasons it worked but you see it would take time explaining this to you, okay this is how it works, the first issue with your code is this, you had too much of INPUT variable and each time you initialise it to the scan object, then it simply requests for users input try to press enter the moment you start running your code and you would find out it requests input only Four times then the program terminates this is cause of the way you had called Scan.nextline() in your code this is how the flow goes your main method starts and the first thing it does is to call the -->programstart method --> then in your program start method you had called scan.nexline() twice before the first if statement so the program requests for user input twice even if you had imputed something at that point nothing happens until the second time the system requests for user input private static void programStart(){ String INPUT; INPUT = scan.nextLine();....(1) call scan.nextLine();......(2)call -->if INPUT == Faster then speedup is called! at this point if you correctly input Faster then speedup is called and then in speed up goes another user input request ? .. static void speedUp() { String INPUT; INPUT = scan.nextLine();...(3)call speed = speed + 5; Now at the third call nothing happens the moment you strike enter the program leaves speedup() and returns to Programstart to execute the remaining command of the if condition which is -->printstate() method then you get to see the new state, as soon as that is done the program returns to --> the Main method and asks for another user input then if you strike enter the whole program terminates i hope this makes sense to you if it doesn't please let me know, the thing is 1 you need to manage your request for inputs from users and put it in the right place 2 you need a loop to check for all your conditions
3rd Jun 2017, 4:23 AM
John Emma
John Emma - avatar
+ 1
so in my own way i have helped you fix the code this is the link https://code.sololearn.com/caezmPDN4E1s/#java try not to run the code in code playground it requests for all input at a time and you just might not enjoy the flow try read through the code and see where you had gotten it wrong i really didn't change much just added some things and i really do hope this helps you
3rd Jun 2017, 4:28 AM
John Emma
John Emma - avatar