Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to pass scanner to method?

    So I am working on an assignment, and I found examples online to pass the scanner to the method but I am still getting the error
    java.util.InputMismatchException. Heres what I have below, keep in mind this is my first time with java in about a year.

    Code:
    package edu.tridenttech.Cpt237.biggs.program0;
    
    public class Shipper {
    	private String shippingType;
    	private double tierOneCost;
    	private double tierTwoCost;
    	private double tierThreeCost;
    	private double totalShipping;
    	private int totalPackages;
    
    
    public Shipper(double tierOneCost, double tierTwoCost, double tierThreeCost) {
    	
    	this.tierOneCost = tierOneCost;
    	this.tierTwoCost = tierTwoCost;
    	this.tierThreeCost = tierThreeCost;
    
    }	
    
    
    public String getShippingType() {
    	
    	return shippingType;
    }
    
    public double getTotalShipping() {
    	
    	return totalShipping;
    }
    
    public  double EstimatedShipping() {
    	
    	return totalShipping;
    }
    
    public void addPackageShipping () {
    	
    }
    
    
    }

  2. #2
    New to the CF scene
    Join Date
    Dec 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry wrong class here is the Main Class

    Code:
    package edu.tridenttech.Cpt237.biggs.program0;
    
    import java.util.Scanner;
    
    public class Main {
    	public static Scanner scan = new Scanner(System.in);
    
    public static Package getOrder(Scanner scan) {
    	String item = " ";
    	int itemWeight = 0;
    	double itemPrice = 0.0;
    	
    	System.out.println("what is the name of the item you are looking for?");
    	item = scan.nextLine();
    	
    	System.out.println("What is the weight of the item?");
    	itemWeight = scan.nextInt();
    	scan.nextLine();
    	
    	System.out.println("What is the price of the item?");
    	itemPrice = scan.nextDouble();
    	
    	Package pkg = new Package(item, itemPrice, itemWeight);
    	
    	return pkg;
    }//end getOrder
    
    public static void main(String[] args) 
    {
    	Shipper shipA = new Shipper(1.50, 2.50, 8.00);
    	Shipper shipB = new Shipper(2.00, 4.00, 17.00);
    	Shipper shipC = new Shipper(3.50, 6.00, 22.50);
    	
    	char runProgram = ' ';
    	Package pack;
    	System.out.println("Do you wish to continue? (Y for yes, Q to quit)");
    	runProgram = scan.nextLine().charAt(0);
    	
    	while (runProgram == 'Y') 
    	{
    		pack = getOrder(scan);
    		
    		System.out.printf("the name of the item you are looking for is: " + pack.getitemName());
    		System.out.printf("the weight of the item is: " + pack.getWeight());
    		
    		
    		
    		
    		System.out.println("Do you wish to continue? (Y for yes, Q to quit)");
    		runProgram = scan.nextLine().charAt(0);
    }// end while
    
    	
    }
    }//end Main Class

  3. #3
    New to the CF scene
    Join Date
    Dec 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Figured it out. I was being stupid, and inputting a double for item weight when I have it set as a int.

  4. #4
    New Coder
    Join Date
    Jan 2016
    Location
    Malaysia
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ChasCreol View Post
    Figured it out. I was being stupid, and inputting a double for item weight when I have it set as a int.
    Well done


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •