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

    Question Attempted user input using a double

    Hello everyone,

    New user to the forums and practically new to Java. I did take a course while I was in college but that was a while ago. I recently started taking a free udemy course on Java as I really wanted to get back into Java. Up until now I have been able to solve most of my coding problems on my own. This one has been sticking me all night and I like to try to learn one thing a day at least when doing code. I'm not very far into the Java tutorials for the course I am in but I always try to write my own code based on some parameters I define before hand. For some reason, the below code is not working out for me. I thought I had the scanner right and Eclipse is only throwing an error on the last line that states "else(ernie == 0)". What am I missing here? Any help would be greatly appreciated and please explain what I am doing wrong so I can learn and correct the issue

    Basically just trying to take user input, incorporate some if and else if statements to return a line based on if the condition is met.



    ************************************************** *******
    Java Code:
    import java.util.Scanner;
    
    //Importing the scanner now for user input
    
    public class Ernie {
    
    	public static void main(String[] args) {
    		
    		double ernie;
    		Scanner input = new Scanner(System.in);
    		//Creating the scanner object
    		
    		double value = input.nextDouble();
    		//Input stuff
    
    		if(ernie > 10)
    			System.out.println("say is greater than 10");
    		
    		else if(ernie < 10)
    			System.out.println("say Ernie is less than 10");
    		
    		else(ernie == 0)
    			System.out.println("say Ernie equals" + ernie);
    	}
    }
    ************************************************** *******

    EDIT: Corrected the post using the CODE tool. Didn't know it existed :)
    Last edited by AAChaoshand; 01-13-2015 at 04:16 AM.

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

    Default Re: Attempted user input using a double

    I ended up fixing the code sorta. I never actually initialized the variable. Silly novice mistake. I still have the problem of how to use the variable without initializing it yet...

    Java Code:
    import java.util.Scanner;
     
    //Importing the scanner now for user input
     
    public class Ernie {
     
        public static void main(String[] args) {
             
            double ernie = 0;
            Scanner input = new Scanner(System.in);
            //Creating the scanner object
             
            double value = input.nextDouble();
            //Input stuff
     
            if(ernie > 10)
                System.out.println("say is greater than 10");
             
            else if(ernie < 10)
                System.out.println("say Ernie is less than 10");
        }
    }
    Last edited by AAChaoshand; 01-13-2015 at 04:27 AM.

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

    Default Re: Attempted user input using a double

    Revised code that accomplished my goal....yay for figuring it out myself. I needed to initialize the variable inside the input which I was either to dumb to see at first or just didn't think it through hard enough.

    Java Code:
    import java.util.Scanner;
    
    //Importing the scanner now for user input
    
    public class Ernie {
    
    	public static void main(String[] args) {
    		
    		Scanner input = new Scanner(System.in);
    		//Creating the scanner object
    		
    		System.out.println("Input a value for Ernie: ");
    		double ernie = input.nextDouble();
    		//Input stuff
    
    		if(ernie > 10)
    			System.out.println("Ernie took " + ernie + " apples " + ernie + " oranges");
    		
    		else if(ernie == 0)
    			System.out.println("He doesn't like apples and oranges?");
    		
    		else if (ernie < 10)
    			System.out.println("apples and oranges");
    	}
    }
    Last edited by AAChaoshand; 01-13-2015 at 05:08 AM.

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

    Default Re: Attempted user input using a double

    Basically just trying to take user input, incorporate some if and else if statements to return a line based on if the condition is met.
    I'm not sure I understand. Is it ernie or value that you want the if statements to look at? Either way the other one isn't doig anything and should be removed.

    You *can't* use a variable before it is initialised. It's not allowed by the language. In fact, you can't use a variable before it might not have been initialised where "might not" has a lengthy definition you can mostly ignore. The important point is that anything you try to do with an unitialised variable is regarded by Java as meaningless and the compiler will stop you. (Contrast this with JavaScript where the end user of the program gets to deal with it as best they can.)

    ---

    Use braces with if statements. You've got to. It's in the Bible somewhere.

    [Edit] Our posts crossed. I'm glad you got it sorted out.

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

    Default Re: Attempted user input using a double

    I figured that out at the same time you posted that and it was a good learning experience for sure. Thanks for responding to me and explaining the issues. I'm still learning the rules but the course is taking us through the syntax and explaining bits and pieces as we progress which isn't good enough for me as I like to get hands on.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Attempted user input using a double

    Quote Originally Posted by pbrockway2 View Post
    (Contrast this with JavaScript where the end user of the program gets to deal with it as best they can.)
    Off-topic: Javascript actually hoists all variable declarations to the top of the function so it is actually even more interesting than you might think :) But nowadays there are wonderful tools such as JSHint which actually report code quality problems in a human readable way.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Problem with BlueJ (double input)
    By deyan8 in forum New To Java
    Replies: 5
    Last Post: 06-24-2014, 11:41 AM
  2. Trouble with Converting String Input to Double
    By NoviceJavaNoob in forum New To Java
    Replies: 4
    Last Post: 05-03-2014, 08:40 AM
  3. How to make user input be of the type double?
    By abv in forum New To Java
    Replies: 9
    Last Post: 08-06-2012, 09:24 AM
  4. how can check input is double or integer?
    By HearT.Hunt3r in forum New To Java
    Replies: 6
    Last Post: 10-15-2011, 07:12 AM
  5. read only double input from text file
    By napi1234 in forum New To Java
    Replies: 6
    Last Post: 06-28-2010, 04:06 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
  •