Results 1 to 7 of 7
  1. #1
    AAChaoshand's Avatar
    AAChaoshand is offline Member
    Join Date
    Jan 2015
    Location
    Texas
    Posts
    22
    Rep Power
    0

    Default Suggestions welcomed for my novice code

    So as before, I always do my own code after each tutorial in the udemy course I am taking. I got this code working great after just 15 or so minutes but am wondering if there is an easier or more efficient way of writing my below code. Please take into account we have only covered the bare basics so far. Data types, classes, objects from classes, methods, arrays, and loops. I'm sure I'm missing much more in there but it's hard to remember when you are studying things for your professional life as well as your personal life (the java).

    At first I was using one scanner and it wasn't working since I needed two separate user entries in the console and it would just skip the second input as nothing typed. I designed a second scanner to take input for the second query which is oil type for your car. I am thinking I could have stored these variables elsewhere until everything was done but how could I go about doing that? I am trying to keep it simple stupid (KISS method) so I can easily reference my code and re-learn or just refresh on what I did.


    The ultimate goal of this basic code is just to get type inputs and pass them to the fuelType method for a printout to confirm the code is working.

    Java Code:
    import java.util.Scanner;
    
    class Car {
    	public void fuelType(int octane, String oilType) {
    		System.out.println("I am running " + octane + " octane in my car and use " + oilType + "as my engine oil");
    	}
    }
    
    
    public class MyExample {
    
    	public static void main(String[] args) {
    		
    		Car corvette = new Car();
    		
    		//First Scanner for user's first input
    		Scanner input = new Scanner(System.in);
    
    		//Second Scanner for user's second input
    		Scanner input2 = new Scanner(System.in);
    		System.out.println("Please input your fuel type for your car. (87, 89, 91, 93, etc.)");
    		
    		//integer variable to get passed to the fuelType method
    		int gas = input.nextInt();
    	
    		System.out.println("Please input your oil type for your car.");
    		//String lube to get passed to the fuelType method
    		String lube = input2.nextLine();
    		
    		//Calling the method here with everything typed above.
    		corvette.fuelType(gas, lube);
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Suggestions welcomed for my novice code

    At first I was using one scanner and it wasn't working
    Why wasn't it working? (ie code, and a description of what that code does)

    On the face of it a single Scanner instance is enough. It pays to have a close read of the Scanner documentation for the two methods you are using to see what they do.

  3. #3
    AAChaoshand's Avatar
    AAChaoshand is offline Member
    Join Date
    Jan 2015
    Location
    Texas
    Posts
    22
    Rep Power
    0

    Default Re: Suggestions welcomed for my novice code

    Well it was working but the second input was getting skipped over so it would just read the text from the Car fuelType method while ignoring lube or in the final case oilType instead of asking me for input again. I had been told you usually don't need two scanner so I guess I could pass the input into a variable temporarily then call it in the last line when it references the fuelType method with both variables gas, and lube. Still trying to get good at this stuff so I appreciate all feedback.

    I will review the documentation provided. I just have to keep doing this stuff to get good at it or understand it better. I have been using System.out.println commands for everything so I can see my code work.
    Last edited by AAChaoshand; 01-23-2015 at 03:37 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Suggestions welcomed for my novice code

    The second input was getting skipped over because the nextInt() and similar routines leave the line terminator in the buffer. So the nextLine() call returned an empty line.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Suggestions welcomed for my novice code

    The second input was getting skipped over because the nextInt() and similar routines leave the line terminator in the buffer.
    To spell it out a little, because this is perplexing the first time you see it:

    Java Code:
    System.out.println("Please input your fuel type for your car. (87, 89, 91, 93, etc.)");
    int gas = input.nextInt(); // returns an int, and leaves the scanner looking at the newline character
         
    System.out.println("Please input your oil type for your car.");
    String lube = input.nextLine(); // reads the newline character, and returns an empty string
    When this code is run whatever the user entered for the oil type will be left in the buffer, unread by the program. One solution is to say input.nextLine() just after the "int gas = input.nextInt();". Do you see what the effect of this would be?

  6. #6
    AAChaoshand's Avatar
    AAChaoshand is offline Member
    Join Date
    Jan 2015
    Location
    Texas
    Posts
    22
    Rep Power
    0

    Default Re: Suggestions welcomed for my novice code

    Ah so essentially wiping the buffer for the next input by telling it input is now essentially null?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Suggestions welcomed for my novice code

    More or less, but I wouldn't say "null", but "empty". The point is that - after nextInt() - the buffer contains a newline character that will screw up any future reads. So you call nextLine() and that empties out the buffer.

Similar Threads

  1. Question [Novice]
    By lovepo1314 in forum New To Java
    Replies: 5
    Last Post: 10-14-2010, 02:48 PM
  2. Replies: 8
    Last Post: 10-29-2009, 03:35 PM
  3. Please post your suggestions about this code
    By mathansj in forum Advanced Java
    Replies: 8
    Last Post: 02-15-2009, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •