0

When I run the code, I get prompt to enter the number and I enter the number but nothing happens after that! I'm trying to implement method overloading here. Any help would be appreciated.

import java.util.Scanner;
public class MethodOverload {
 public static void main(String [] args){
     Scanner input = new Scanner(System.in);
     Scanner inputDoub = new Scanner (System.in);
     System.out.println ("Enter the int or double number");
     int x = input.nextInt();
     double y = inputDoub.nextDouble();
     //int x;
     //double y;
     System.out.printf("Square of integer value %d", x, "is", square(x));
     System.out.printf("Square of double value %f", y, "is", square(y));
      }

   public static int square(int intValue){
       System.out.printf ("\nCalled square method with int argument: %d", intValue);

       return intValue*intValue;
   }

   public static double square (double doubleValue){
       System.out.printf ("\nCalled sqauer method with double argument: %d", doubleValue);
       return doubleValue*doubleValue;
   }

}
vib321
  • 61
  • 7
  • 3
    Try using just one `Scanner`. Also, see [this thread](http://stackoverflow.com/questions/2912817/how-to-use-scanner-to-accept-only-valid-int-as-input) for an example of using a `Scanner` to enter multiple numbers. – Ted Hopp May 10 '15 at 13:57

2 Answers2

1
import java.util.Scanner;
public class MethodOverload {
public static void main(String [] args){
    Scanner input = new Scanner(System.in);
    System.out.println ("Enter the int or double number");
    double y = input.nextDouble();

    if(y % 1 == 0) {
        int x = (int) y;
        System.out.printf("Square of integer value %d is %d", x, square(x));
    }else{
        System.out.printf("Square of double value %f is %f", y, square(y));
    }

}

public static int square(int intValue){
    System.out.printf ("\nCalled square method with int argument: %d", intValue);

    return intValue*intValue;
}

public static double square (double doubleValue){
    System.out.printf ("\nCalled sqauer method with double argument: %f", doubleValue);
    return doubleValue*doubleValue;
}

}

If I understand correctly you simply want to get the users input, and if the users enters a double use one overloaded method, if he enters an integer use the other. The code above does this.

It simply stores the user input as a double, if the user input modulo 1 = 0 (meaning it's an integer) cast it to an integer and call the overloaded method passing an integer argument. Also, in the last overloaded square method, you used %d in your printf function instead of %f, use %f when you want to have a double.

Your first two printf statements were also wrong, the syntax only allows one String to be displayed, and the other arguments are for replacing all the % signs.

0

Your attempted formatting double with %d which is not correct. PFB change needed :

public static double square (double doubleValue){
    System.out.printf ("\nCalled sqauer method with double argument: %f", doubleValue);
    return doubleValue*doubleValue;
}

One observation: It doesn't make sense to use 2 separate instances of Scanner. No use. Correct your code like this:

Scanner input = new Scanner(System.in);
//Scanner inputDoub = new Scanner (System.in);
System.out.println ("Enter the int or double number");
int x = input.nextInt();
double y = input.nextDouble();
Rajesh
  • 2,120
  • 1
  • 10
  • 14
  • I tried with both the rectification that you have mentioned but it doesn't work. The same thing happens as I mentioned above. But with the other mentioned answer it works fine. I think by using if statement it works.. – vib321 May 10 '15 at 15:11
  • @vib321 Such things happen when Problem statements are not clear. I just tried to make your code working. It works other way. I would say you should go with solution suggested by Richard...if that fits your requirement. – Rajesh May 10 '15 at 16:49
  • No, I'm very much open to the solutions to my problem, as it will help me look at more perspectives. But the thing is the modifications that you suggested, I already tried, but the results are unmoved. – vib321 May 11 '15 at 13:29
  • What appeared to me - reading your problem statement; and the code - you are entering 2 numbers one after other[e.g. int 1 and then next double 2.33]....Your Problem statement says - "Where I'm getting wrong with the code? I'm trying to implement the method overloading"....If you observe .....overloading is already happening in your program. I have thought to fix errors in your code..just to make it run. – Rajesh May 11 '15 at 13:53