Results 1 to 4 of 4

Thread: Need some help

  1. #1
    marshalthrone is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default Need some help

    I'm taking this as a class but i'm stuck

    import java.util.*;

    public class Sort3 {

    public static Scanner in = new Scanner (System.in);

    public static void main (String [] args) {

    System.out.println (GetDouble());

    double a = GetDouble();

    double b = GetDouble();

    double c = GetDouble();

    }
    public static double GetDouble() {

    System.out.print ("Enter a double: ");

    double x = GetDouble ();


    return x;
    }
    }



    When i run this it prints out "Enter a double:" hundreds of times and a dont know y. I need to be able to have GetDouble Ask 3 sperate times so i can get a,b, and c. ???
    Also on a side note how would i check to make sure they entered a valid double??

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why have you started a new thread for this problem?
    In double x = GetDouble ();, your GetDouble method is calling itself.
    Read the API specs for the Scanner class to find out how to use it.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    1.Put ur requirement in a paper and then try to implement.See u are agaian and again calling GetDouble() results to infinite printouts.

    2.Gothru the api.


    I have modified ur code to understand the things much better.

    import java.util.*;

    Java Code:
    public class Test {
    
    public static Scanner in = new Scanner (System.in);
    
    public static void main (String [] args) {
    
    double a = GetDouble();
    
    double b = GetDouble();
    
    double c = GetDouble();
    
    }
    public static double GetDouble() {
    
    System.out.print ("Enter a double: ");
    
    [B]double x = in.nextDouble ();[/B]
    
    return x;
    }
    }
    Ramya:cool:

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    ...

    I have modified ur code to understand the things much better.
    ...
    Thank you for spoon feeding the OP. Now they don't have to go through the API at all.

Posting Permissions

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