Connecting Tech Pros Worldwide Forums | Help | Site Map

Calling a method with a scanner parameter

Newbie
 
Join Date: Oct 2008
Posts: 2
#1: Oct 5 '08
For my class i have to write a program that has a method to combine integer time input values into a single double value, return that to a method that sums the values, then return that to a main. The method names i have written have to be exactly as they are shown, and i cannot for the life of me figure out how to call the "public static double RouteTotalOperationalTime(Scanner input)" method into my main. If someone can explain to me how to call the method that would be great, thanks.
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Testpage
  3. {
  4.     public static void main (String [] args)
  5.     {
  6.         /*
  7.         want to call RouteTotalOperationalTime here and define it as variable name duration1
  8.         */
  9.         double duration1 = RouteTotalOperationalTime();
  10.         System.out.println("Duration is: " + duration1);
  11.     }
  12.     public static double RouteTotalOperationalTime(Scanner input)
  13.     {
  14.         System.out.println("How many runs?");
  15.         int runs = input.nextInt();
  16.         double duration = 0;
  17.         for(int i=0; i<runs; i++)
  18.         {
  19.             System.out.println("How many hours?");
  20.             int hours = input.nextInt();
  21.             System.out.println("How many minutes?");
  22.             int minutes = input.nextInt();
  23.             double time = convertHoursMinutesToDouble(hours, minutes);
  24.             duration += time+.5;
  25.         }
  26.         return duration;
  27.     }
  28.     public static double convertHoursMinutesToDouble(int hours, int minutes)
  29.     {
  30.         double Hours = hours/1.0;
  31.         double Minutes = minutes/60.0;
  32.         double sum = Hours+Minutes;
  33.         return sum;
  34.     }
  35. }
  36.  
  37. /*
  38. The Error:
  39.  
  40. Testpage.java:7: RouteTotalOperationalTime(java.util.Scanner) in Testpage cannot
  41.  be applied to ()
  42.                 double duration1 = RouteTotalOperationalTime();
  43.                                    ^
  44. 1 error
  45. */
  46.  

Expert
 
Join Date: Sep 2007
Posts: 856
#2: Oct 6 '08

re: Calling a method with a scanner parameter


It means you have to pass the method a Scanner object. Thus, you have to create a Scanner first (don't forget to import java.util.Scanner) and then you can pass it to the function.
Newbie
 
Join Date: Oct 2008
Posts: 2
#3: Oct 6 '08

re: Calling a method with a scanner parameter


Quote:

Originally Posted by Laharl

It means you have to pass the method a Scanner object. Thus, you have to create a Scanner first (don't forget to import java.util.Scanner) and then you can pass it to the function.

thanks a bunch gah i can't believe i missed that, believe it or not ive spent about 3 hours going over that, :-) compiles nice and smooth now ty again
Reply