Results 1 to 6 of 6
  1. #1
    Danni D. is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default problem with return statement using if-else

    just signed in and this is my first thread. first time ever using a forum so sorry in advance for making any mistakes.
    I tried to wirte an object code using a method which is asking to to type in number an than return it to the my main for this I tried to use an if-else statement in oder to fulfill a condition but i does not work.

    Java Code:
        public static void main(String[] args) 
        {
            double kap=askForNumber(50000,0,"Type in initial kapital");
        }
        public static double askForNumber(double a,double b, String S)
         {
            a=50000;
            b=0;
            Scanner input=new Scanner(System.in);
            System.out.println(S);
            double kap=input.nextDouble();
            if(kap>a)
                return kap;
            else
                 System.out.println("type in between " + a + "and" + b);
         }
    I am new to java and i would be glad if someone would be so nice and help me with this I would rather say little problem.
    thanks.
    Last edited by pbrockway2; 05-19-2012 at 02:28 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: problem with return statement using if-else

    Your method states that it will return a double. Well what happens if the else block is run? Does the method return a double? Does it return anything?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: problem with return statement using if-else

    Hi Danni, welcome to the forums!

    When you post code use the "code" tags: put [code] at the start of the code and [/code] at the end. There is other markup you can use (described here,) but the code tags are essential for code to be readable when it appears on a web page.

  4. #4
    Danni D. is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: problem with return statement using if-else

    yes the method does return a double but unfortunately it returns any double no matter which double the user type in.
    @ pbrockway2 thanks for the hint next time I will try it that way.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: problem with return statement using if-else

    Quote Originally Posted by Danni D. View Post
    yes the method does return a double but unfortunately it returns any double no matter which double the user type in.
    @ pbrockway2 thanks for the hint next time I will try it that way.
    No, if the else block is run, then no double is returned. In that block your method has a System.out.println(...), but no return statement. You need to "walk through" your program to perceive the logic of it.

  6. #6
    Danni D. is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: problem with return statement using if-else

    yap your right. Thanks a lot.

Similar Threads

  1. return; statement
    By fallout87 in forum New To Java
    Replies: 1
    Last Post: 03-16-2012, 10:54 PM
  2. If Statement return
    By ricardo5222 in forum New To Java
    Replies: 4
    Last Post: 11-01-2010, 11:43 AM
  3. insert statement return problem in ibatis
    By dheerajsea123 in forum JDBC
    Replies: 1
    Last Post: 05-20-2010, 10:22 AM
  4. Problem with recurrsive return statement
    By pachufir in forum New To Java
    Replies: 3
    Last Post: 12-08-2009, 02:32 AM
  5. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 06:29 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
  •