Stich19
Stich19

Reputation: 5

Pass data from input scanner to another method

Trying to pass the inputs of costMaterials and hoursWorked from main() to calculationMethod() in order to properly calculate thirdPrice. I tried changing void for main string to double so that main() could return values. That didn't work for a reason that I still don't know/understand. I have also searched the oracle site for a tutorial concerning this issue and cant seem to find one. Any help would be greatly appreciated.

import java.util.Scanner; //Imports input device
public class CraftPricing
{
    public static void main(String[] args)
    {
        Scanner inputDevice = new Scanner(System.in); //Sets up input device
        String productName; //Used for naming product
        double costMaterials, hoursWorked; //Gives variables decimal format
        System.out.println("Enter the name of the product "); //Enter product name
        productName = inputDevice.nextLine(); //Inputs product name
        System.out.println("Enter the cost of materials prior to discount "); //Enter cost of materials
        costMaterials = inputDevice.nextDouble(); //Inputs cost of materials
        System.out.println("Enter the number of hours worked "); //Enter hours worked
        hoursWorked = inputDevice.nextDouble(); //Inputs hours worked
        System.out.printf("The cost of " + productName + " is %.2f\n" , calculationMethod());
        //Output product name and cost
    }
    public static double calculationMethod() //Method used to calcualte price
    {
        double itemDiscount = 0.75; //Gives decimal format to variable
        double payRate = 14.00; //Gives decimal format to variable
        double shipHandle = 6.00; //Gives decimal format to variable
        double firstPrice = payRate * hoursWorked; //Calculates first portion of equation
        double secondPrice = costMaterials + firstPrice; //Calculates second portion of equation
        final double thirdPrice = itemDiscount * secondPrice + shipHandle;
        //Calculates final portion of equation
        return thirdPrice; //Returns double to main() for output
    }
}

Upvotes: 0

Views: 1699

Answers (4)

Kevin Anderson
Kevin Anderson

Reputation: 4592

The reason trying to change main didn't work is that Java couldn't figure out how to start your program. To run a program, Java looks for a method meeting certain criteria:

  • It must be called main; AND
  • It must be declared static; AND
  • It must have void return type; AND
  • It must accept exactly one argument that is an array of String.

It is conventional to use the name args for the argument to main, but actually any name you like will do. However, everything else must be exactly as described or Java won't know where to start your program.

Upvotes: 0

Aritra Chatterjee
Aritra Chatterjee

Reputation: 460

This is happening because the inputs that you have scanned in the main method, is not visible to the calculation method. One solution would be to declare the hoursWorked, costMaterials as static variables in your class, that is outside the main method. Then it would work.

public class CraftPricing { 
 private static double hoursWorked;//<--Just add this
 private static double costMaterials; //<--Just add this
 public static void main(String[] args)
    {
        Scanner inputDevice = new Scanner(System.in); //Sets up input device
        String productName; //Used for naming product

        System.out.println("Enter the name of the product "); //Enter product name
        productName = inputDevice.nextLine(); //Inputs product name
        System.out.println("Enter the cost of materials prior to discount "); //Enter cost of materials
        costMaterials = inputDevice.nextDouble(); //Inputs cost of materials
        System.out.println("Enter the number of hours worked "); //Enter hours worked
        hoursWorked = inputDevice.nextDouble(); //Inputs hours worked
        System.out.printf("The cost of " + productName + " is %.2f\n" , calculationMethod());
        //Output product name and cost
    }
    public static double calculationMethod() //Method used to calcualte price
    {
        double itemDiscount = 0.75; //Gives decimal format to variable
        double payRate = 14.00; //Gives decimal format to variable
        double shipHandle = 6.00; //Gives decimal format to variable
        double firstPrice = payRate * hoursWorked; //Calculates first portion of equation
        double secondPrice = costMaterials + firstPrice; //Calculates second portion of equation
        final double thirdPrice = itemDiscount * secondPrice + shipHandle;
        //Calculates final portion of equation
        return thirdPrice; //Returns double to main() for output
    }
}

Upvotes: 0

Tanvir Sourov
Tanvir Sourov

Reputation: 417

You can simply pass the parameters in the following way:

public static void main(String[] args)
{
    Scanner inputDevice = new Scanner(System.in); //Sets up input device
    String productName; //Used for naming product
    double costMaterials, hoursWorked; //Gives variables decimal format
    System.out.println("Enter the name of the product "); //Enter product name
    productName = inputDevice.nextLine(); //Inputs product name
    System.out.println("Enter the cost of materials prior to discount "); //Enter cost of materials
    costMaterials = inputDevice.nextDouble(); //Inputs cost of materials
    System.out.println("Enter the number of hours worked "); //Enter hours worked
    hoursWorked = inputDevice.nextDouble(); //Inputs hours worked
    System.out.printf("The cost of " + productName + " is %.2f\n" , calculationMethod(costMaterials, hoursWorked));
    //Output product name and cost
}
public static double calculationMethod(double costMaterials, double hoursWorked) //Method used to calcualte price
{
    double itemDiscount = 0.75; //Gives decimal format to variable
    double payRate = 14.00; //Gives decimal format to variable
    double shipHandle = 6.00; //Gives decimal format to variable
    double firstPrice = payRate * hoursWorked; //Calculates first portion of equation
    double secondPrice = costMaterials + firstPrice; //Calculates second portion of equation
    final double thirdPrice = itemDiscount * secondPrice + shipHandle;
    //Calculates final portion of equation
    return thirdPrice; //Returns double to main() for output
}

Here, you just need to pass the parameters to the calculationMethod function. Alternatively, declaring them static inside the class can also work.

Upvotes: 0

TuyenNTA
TuyenNTA

Reputation: 1204

Let's declare your calculatioinMethod like this:

public static double calculationMethod(double costMaterials, double hoursWorked)

and call it in main:

calculationMethod(costMaterials, hoursWorked)

Upvotes: 1

Related Questions