Calling a method with a scanner parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CRGoose
    New Member
    • Oct 2008
    • 2

    Calling a method with a scanner parameter

    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 RouteTotalOpera tionalTime(Scan ner input)" method into my main. If someone can explain to me how to call the method that would be great, thanks.
    Code:
    import java.util.*;
    public class Testpage
    {
    	public static void main (String [] args)
    	{
    		/*
    		want to call RouteTotalOperationalTime here and define it as variable name duration1
    		*/
    		double duration1 = RouteTotalOperationalTime();
    		System.out.println("Duration is: " + duration1);
    	}
    	public static double RouteTotalOperationalTime(Scanner input)
    	{
    		System.out.println("How many runs?");
    		int runs = input.nextInt();
    		double duration = 0;
    		for(int i=0; i<runs; i++)
    		{
    			System.out.println("How many hours?");
    			int hours = input.nextInt();
    			System.out.println("How many minutes?");
    			int minutes = input.nextInt();
    			double time = convertHoursMinutesToDouble(hours, minutes);
    			duration += time+.5;
    		}
    		return duration;
    	}
    	public static double convertHoursMinutesToDouble(int hours, int minutes)
    	{
    		double Hours = hours/1.0;
    		double Minutes = minutes/60.0;
    		double sum = Hours+Minutes;
    		return sum;
    	}
    }
    
    /*
    The Error:
    
    Testpage.java:7: RouteTotalOperationalTime(java.util.Scanner) in Testpage cannot
     be applied to ()
                    double duration1 = RouteTotalOperationalTime();
                                       ^
    1 error
    */
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    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.Scann er) and then you can pass it to the function.

    Comment

    • CRGoose
      New Member
      • Oct 2008
      • 2

      #3
      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.Scann er) 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

      Comment

      Working...