6 Replies - 726 Views - Last Post: 10 February 2013 - 06:59 PM Rate Topic: -----

#1 Archimedes234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-February 13

operator * is undefined for the argument type(s) Scanner, double

Posted 10 February 2013 - 05:46 PM

I am trying to learn how to program in Java but I seem to be missing a detail here. I am trying to multiply two doubles to get a third double. I am not sure what the error means or how to correct it. All I have found online are references to libraries or vectors, but I have not learned those yet. Any help would be greatly appreciated. The code is below:

	public static void main(String[] args) {
		//Greeting
		System.out.println("Welcome to the Earnings Calculator");
		double base = 100000;  //Base Salary
		System.out.print("Current salary calculations based on base salary of ");
		System.out.println(base);
		//User sales input
		System.out.print("Enter your total sales ");
		Scanner salesTotal = new Scanner (System.in);
		System.out.println(salesTotal.nextLine());
		//Commission Calculation
		double commissionRate = 0.05; //Commission Rate
		double commission;
		commission = salesTotal * commissionRate;
		System.out.println(commission);
		
	}




Is This A Good Question/Topic? 0
  • +

Replies To: operator * is undefined for the argument type(s) Scanner, double

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8333
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: operator * is undefined for the argument type(s) Scanner, double

Posted 10 February 2013 - 05:49 PM

Scanner salesTotal = new Scanner (System.in);

salesTotal is a Scanner object not a number... how can you use a Scanner in a calculation ?

May be you want:

Scanner scan = new Scanner(System.in);
double salesTotal = scan.nextDouble();
double xxx = salesTotal * anotherDouble
Was This Post Helpful? 2
  • +
  • -

#3 Archimedes234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-February 13

Re: operator * is undefined for the argument type(s) Scanner, double

Posted 10 February 2013 - 06:25 PM

View Postpbl, on 10 February 2013 - 05:49 PM, said:

Scanner salesTotal = new Scanner (System.in);

salesTotal is a Scanner object not a number... how can you use a Scanner in a calculation ?

May be you want:

Scanner scan = new Scanner(System.in);
double salesTotal = scan.nextDouble();
double xxx = salesTotal * anotherDouble


I am very new to this and I am trying to understand what each of the two lines you added does. How do they change the entered data to the double that I need it to be?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8333
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: operator * is undefined for the argument type(s) Scanner, double

Posted 10 February 2013 - 06:48 PM

A Scanner is an onject to read from a file or the console
by itself it is not a number but it has methods that return number or String or lines that are the next on the file/console

scan.nextInt();
scan.nextFloat();
scan.nextBoolean();
scan.nextDouble();
scan.next(); // this is to rwan next String() should have been called nestString()
scan.nextLine();
Was This Post Helpful? 2
  • +
  • -

#5 Razakel  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 05-February 10

Re: operator * is undefined for the argument type(s) Scanner, double

Posted 10 February 2013 - 06:48 PM

View Postpbl, on 10 February 2013 - 05:49 PM, said:

Scanner scan = new Scanner(System.in);
double salesTotal = scan.nextDouble();


A Scanner object is not a numerical data type. It isn't used in arithmetic, because Java doesn't know what you're trying to add/subtract/multiply/divide.

The Scanner object is used to access the input buffer, and its member method "nextDouble()" is what actually returns whatever double the user happened to have entered recently, but since the Scanner is just used to access it, you need a variable to store the value read in. That's what double salesTotal is for.
Was This Post Helpful? 2
  • +
  • -

#6 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: operator * is undefined for the argument type(s) Scanner, double

Posted 10 February 2013 - 06:57 PM

Hello Archimedes234;
You could do something like this... however; I intentionally left some of the code out (mainly in the calculation method)...so you can figure it out. :online2long:/>/> .
package javahelp;
import java.util.Scanner;
import java.util./*for you to figure out*/;
class JavaHelp {
    private static double baseValue, totalSales, tempVariable, commissionRate, comission;
    private static boolean isCorrectBaseValue = /*for you to figure out =D*/;
    private static Scanner console = new Scanner(System.in);
    public static void main(String[] args) {
	//Greeting
        greetMethod();
        
        //Ask user for base value (makes program more diverse)
        baseValue = getUserInput();
        
	//User sales input
        totalSales = getUserInput();
        
        //Allow user to enter comission rate
        /*figure out*/ = getUserInput();
        
        //Call method to figure out comission
	comission = calculateComission(totalSales, commissionRate);
        
	System.out.println(/* figure out */);
    }
        
        static void greetMethod(){
            System.out.println("Welome to the Earnings Calculator =D");
        }
        static double getUserInput(){
            System.out.println("Please enter a value: ");
            do{
                try{
                    tempVariable = console.nextDouble();
                    if(tempVariable < 0){
                        throw new IllegalArgumentException("Age cannot be negative.");
                    }
                    else{
                        isCorrectBaseValue = /*for you to figure out*/;
                    }
                }
                catch(InputMismatchException e){
                    System.err.println(e.toString());
                }
                catch(Exception e){
                    System.err.println(e.toString());
                }
                finally{
                    System.out.println("thank you for value");
                }
            }while(!(isCorrectBaseValue));
            return tempVariable;
        }
        static double calculateComission(double x, double y){
            tempVariable = x * y;
            return tempVariable;
    }
}



If you can figure out what to put where, than you should be fine =D.

EDIT: Feel free to ask questions, as it is an essential part of learning =D... also don't feel overwhelmed. I just wanted to expose you to the power of Java

This post has been edited by k3y: 10 February 2013 - 06:58 PM

Was This Post Helpful? 1
  • +
  • -

#7 Archimedes234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-February 13

Re: operator * is undefined for the argument type(s) Scanner, double

Posted 10 February 2013 - 06:59 PM

Ok. Thanks a lot. I was seriously confused until I realized that 'scan' was a variable input you named in the first line. I had not realized you changed the variable name. Thanks again for all the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1