Jump to content

Java Operator Undefined Argument Type?

Go to solution Solved by Hominis,

You need to use Integer.parseInt() because you can't do math operations on Strings. Or you could do Double.parseDouble()

 

EDIT:

 

After looking at it again, there is no reason why age, height, or weight are String variable types. You should be able to use int, or double for them.

Hey guys, I'm currently doing up a simple program in eclipse to take user input and output the data in a story with simple calculation involved (BMI)

import java.util.Scanner;public class InfoDisplay {	public static void main (String[] args) {				String name, nric, age, gender, weight, height, bmi;				Scanner in = new Scanner (System.in);				System.out.print("Enter Name: ");		name = in.nextLine();				System.out.print("Enter NRIC: ");		nric = in.nextLine();				System.out.print("Enter Age: ");		age = in.nextLine();				System.out.print("Enter Gender (Male/Female): ");		gender = in.nextLine();				System.out.print("Enter Weight: ");		weight = in.nextLine();				System.out.print("Enter Height: ");		height = in.nextLine();				bmi = ((weight * 703) /(height * height));				System.out.println("Hello! " + name);		System.out.println("Your NRIC is " + nric + "and age is" + age);		System.out.printf("Your Gender is " + gender + "and BMI is ");				in.close();	}}

The error:

The operator * is undefined for the argument type(s) String, int

Displays at the line

bmi = ((weight * 703) /(height * height));

What am I doing wrong? Previous java calculations work just fine when I type it out this way but not this time...

~~~SnapDragon~~~

| CPU: AMD Ryzen 9 9950X3D | CPU Cooler: Gigabyte Aorus Waterforce X II 360mm |RAM: 2x32GB G.Skill Trident Z5 Neo RGB 6000MHz | Mobo: Gigabyte X870E Aorus Xtreme X3D AI Top  | Storage: Samsung 9100 Pro 4TB + Samsung 990 Pro 4TB + Samsung 870 Evo 4TB + Samsung 870 Evo 2TB | Graphics Card: Gigabyte RTX 5090 Aorus Master 32G | Case: Lian Li Lancool 216 | PSU: Seasonic Vertex GX-1200 |

 

Link to comment
https://linustechtips.com/topic/366254-java-operator-undefined-argument-type/
Share on other sites

Link to post
Share on other sites

You need to use Integer.parseInt() because you can't do math operations on Strings. Or you could do Double.parseDouble()

 

EDIT:

 

After looking at it again, there is no reason why age, height, or weight are String variable types. You should be able to use int, or double for them.

Link to post
Share on other sites

import java.util.Scanner;

public class InfoDisplay {

public static void main (String[] args) {

 

 

Scanner in = new Scanner (System.in);

 

System.out.println("Enter Name: ");

String name = in.next();

 

System.out.println("Enter NRIC: ");

String nric = in.next();

 

System.out.println("Enter Age: ");

int age = in.nextInt();

 

System.out.println("Enter Gender (Male/Female): ");

String gender = in.next();

 

System.out.println("Enter Weight: ");

double weight = in.nextInt();

 

System.out.println("Enter Height: ");

double height = in.nextInt();

 

double bmi = ((weight * 703) /(height * height));

 

System.out.println("Hello! " + name);

System.out.println("Your NRIC is " + nric + "and age is " + age);

System.out.println("Your Gender is " + gender + "and BMI is " + bmi);

 

in.close();

}

}

System: Thinkpad T460

 

Link to post
Share on other sites

 

import java.util.Scanner;
public class InfoDisplay {
public static void main (String[] args) {
 
 
Scanner in = new Scanner (System.in);
 
System.out.println("Enter Name: ");
String name = in.next();
 
System.out.println("Enter NRIC: ");
String nric = in.next();
 
System.out.println("Enter Age: ");
int age = in.nextInt();
 
System.out.println("Enter Gender (Male/Female): ");
String gender = in.next();
 
System.out.println("Enter Weight: ");
double weight = in.nextInt();
 
System.out.println("Enter Height: ");
double height = in.nextInt();
 
double bmi = ((weight * 703) /(height * height));
 
System.out.println("Hello! " + name);
System.out.println("Your NRIC is " + nric + "and age is " + age);
System.out.println("Your Gender is " + gender + "and BMI is " + bmi);
 
in.close();
}
}

 

This is a good solution.

 

Although the last two output lines should be for readability:

 

System.out.println("Your NRIC is " + nric + " and age is " + age);
System.out.println("Your Gender is " + gender + " and BMI is " + bmi);
Link to post
Share on other sites

 

This is a good solution.

 

Although the last two output lines should be for readability:

 

System.out.println("Your NRIC is " + nric + " and age is " + age);
System.out.println("Your Gender is " + gender + " and BMI is " + bmi);

 

 

Oops don't know how I messed that up... :P

System: Thinkpad T460

 

Link to post
Share on other sites

Yep solved it after changing it to Double.parseDouble().

 

The declaration for each variable method seems a bit troublesome to me so I just prefer to declare it at the start rather than at each line.

 

Thanks all!

~~~SnapDragon~~~

| CPU: AMD Ryzen 9 9950X3D | CPU Cooler: Gigabyte Aorus Waterforce X II 360mm |RAM: 2x32GB G.Skill Trident Z5 Neo RGB 6000MHz | Mobo: Gigabyte X870E Aorus Xtreme X3D AI Top  | Storage: Samsung 9100 Pro 4TB + Samsung 990 Pro 4TB + Samsung 870 Evo 4TB + Samsung 870 Evo 2TB | Graphics Card: Gigabyte RTX 5090 Aorus Master 32G | Case: Lian Li Lancool 216 | PSU: Seasonic Vertex GX-1200 |

 

Link to post
Share on other sites

Yep solved it after changing it to Double.parseDouble().

 

The declaration for each variable method seems a bit troublesome to me so I just prefer to declare it at the start rather than at each line.

 

Thanks all!

 

then do it at the top ;)

import java.util.Scanner;public class InfoDisplay {	public static void main (String[] args) {			Scanner in;		String name, nric, gender;		double weight, height, bmi;		int age;		in = new Scanner (System.in);		 		System.out.println("Enter Name: ");		name = in.next();		 		System.out.println("Enter NRIC: ");		nric = in.next();		 		System.out.println("Enter Age: ");		age = in.nextInt();		 		System.out.println("Enter Gender (Male/Female): ");		gender = in.next();		 		System.out.println("Enter Weight: ");		weight = in.nextInt();		 		System.out.println("Enter Height: ");		height = in.nextInt();		 		bmi = ((weight * 703) /(height * height));		 		System.out.println("Hello! " + name);		System.out.println("Your NRIC is " + nric + "and age is " + age);		System.out.println("Your Gender is " + gender + "and BMI is " + bmi);		 		in.close();			}}

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to post
Share on other sites

 

then do it at the top ;)

 

 

Yeap I always prefer to do it at the top in almost all of my school assignments.

 

:D

~~~SnapDragon~~~

| CPU: AMD Ryzen 9 9950X3D | CPU Cooler: Gigabyte Aorus Waterforce X II 360mm |RAM: 2x32GB G.Skill Trident Z5 Neo RGB 6000MHz | Mobo: Gigabyte X870E Aorus Xtreme X3D AI Top  | Storage: Samsung 9100 Pro 4TB + Samsung 990 Pro 4TB + Samsung 870 Evo 4TB + Samsung 870 Evo 2TB | Graphics Card: Gigabyte RTX 5090 Aorus Master 32G | Case: Lian Li Lancool 216 | PSU: Seasonic Vertex GX-1200 |

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×