0

I have a code which I would like to have three variables call on a method that reads user input and determine if it is an integer(if not reprompt the user), then return the input to the variable. I get the following compiler error when I try to compile:

Error: The method getInt(int) in the type Methods is not applicable for the arguments (java.util.Scanner)

Here is my code:

import java.util.Scanner;

 public class Methods{   
   public static void main(String [] arg){  

 Scanner scan=new Scanner(System.in);

 int a,b,c;  

 System.out.println("Enter three ints");  

 a = getInt(scan.nextInt());  
 b = getInt(scan.nextInt());  
 c = getInt(scan.nextInt());

   }



   public static int getInt(String input){ 
  Scanner scan=new Scanner(System.in);

  int num = 0;
    while (num <1) {
     if(scan.hasNextInt()) {
    int number = scan.nextInt();
    num +=1;
     }
     else{
       System.out.println("Invalid input.  Please enter an integer value."); 
       input.next();
     }

} 
 }

Any help you could give would be appreciated!

klab102
  • 9
  • 2
  • Your method `getInt` takes an `int` variable as its parameter. You cannot pass a `Scanner` instance where an `int` is expected. Also, your code doesn't compile as given... you're redefining `input` (a parameter of type` int` to the function `getInt`) inside the function `getInt` to be of type `Scanner`. – Jeff B Mar 25 '16 at 15:56

2 Answers2

1

I recommend you to read "use Scanner to accept only valid int as input" https://stackoverflow.com/a/2913026/5980046

Community
  • 1
  • 1
Jakub S.
  • 4,461
  • 35
  • 29
  • how would i use this answer in my method though? i do think i understand how to accept integers only with scanner, but i dont know whats going wrong with calling on the method itself – klab102 Mar 25 '16 at 21:33
  • You just put the code in main,just compile and run. I assume you created Java console program. – Jakub S. Mar 26 '16 at 07:06
0

Your method getInt(int input) is expecting an integer as the argument.

But in your codes, you are providing a Scanner object instead of int:

a = getInt(scan);  
b = getInt(scan);  
c = getInt(scan);

This is what you probably wanted:

a = getInt(scan.nextInt());  
b = getInt(scan.nextInt());  
c = getInt(scan.nextInt());
user3437460
  • 16,235
  • 13
  • 52
  • 96
  • This gives me another error which occurs in my method: Error: Cannot invoke hasNextInt() on the primitive type int. which is in the line: while (!input.hasNextInt()){ – klab102 Mar 25 '16 at 21:40
  • @klab102 I use `nextInt()` in my example, not `hasNextInt()`. – user3437460 Mar 25 '16 at 21:47
  • I made that change in the main method, the error that I am receiving is in the method getInt, which is determining if the user input is an integer or not. – klab102 Mar 25 '16 at 21:54
  • @klab102 I ran the codes but I am not getting the error stated by you. Actually I already answered what you were asking. We won't know what is going on at your side now since you are changing your codes from your side. I can only answer you from your given codes. – user3437460 Mar 25 '16 at 22:16
  • I've edited my original post above with the change, but I'm getting so many compile errors in my method now. I've only check to see if a number is an integer in a main method before, which is why I am lost as for what I should do here. – klab102 Mar 25 '16 at 22:43