r/javahelp • • Jun 08 '14

What is wrong with this basic code?

I've recently started learning java, (as in yesterday) and so today I decided that I was going to try to make a basic program that would tell you the volume of a cube. For some reason eclipse is saying The operator * is undefined for the arguement type(s) java.lane.String, java.lane.String on this line of code: answer = widthheightlength; Here is the whole code, and I should say that this is my first post so I apologize in advanced if I pasted the code incorrectly.

package stringvars;

import java.util.Scanner;
public class test{
    public static void main(String[] args){

Scanner user_input = new Scanner(System.in);

String width, height, length, answer; 
	System.out.println("Enter the width of the Cube ");
	width = user_input.next();
	System.out.println("Enter the height of the Cube ");
	height = user_input.next();
	System.out.println("Enter the length of the Cube");
	length = user_input.next();
	answer = width*height*length;
	System.out.println("The volume of your cube is " + answer);
           }
     }
0 Upvotes

11 comments sorted by

5

u/c0demonk3y Jun 08 '14

Your input variables are strings, and you cannot multiply a string. You need to convert the strings to a numeric type first.

Probably worth having a thorough read of the Oracle Java tutorials

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

2

u/RedHawkCrazed Jun 08 '14

How could I change the strings to and numeric variables?

1

u/not_president_nixon Jun 08 '14

Parse them.

Now I have linked to the parse method for integers. You'll need to determine the correct numeric type to use for your particular homework assignment.

You'll also want to handle the exception that the parse method can throw.

Sincerely,

Richard Nixon

2

u/RedHawkCrazed Jun 08 '14

Something like this?

String str = "width";
  int width = Integer.parseInt(str);

2

u/not_president_nixon Jun 08 '14

That would throw an exception because you can't parse "width" into a number.

 String str = "100";
 int width = Integer.parseInt(str);

That would result in the integer variable width having 100 as its value.

You've already got the string values from the user input in your width, height, and length variables.

 int widthParsed = Integer.parseInt(width);
 int heightParsed = Integer.parseInt(height);
 int lengthParsed = Integer.parseInt(length);
 answer = widthParsed * heightParsed * lengthParsed;
 System.out.println("Ballsack: " + answer);

Next: what happens if the user enters bad data? What if they enter "BALL-SACK" as the height value? What if they enter 10.2 as the height value?

sincerely,

Richard Nixon

2

u/RedHawkCrazed Jun 08 '14

Oh, so you have to parse each individual string. Thanks for your help, I understand it now. And if you enter false data wouldn't that cause a runtime error?

2

u/not_president_nixon Jun 08 '14

And if you enter false data wouldn't that cause a runtime error?

Try it out and see what happens.

I guess my real point is that you should write code to handle bad user input. Look at some exception handling code. If I enter !0 instead of 10, I don't expect the program to fail completely. I expect to get an error message and another prompt for input.

I'm guessing that perhaps you aren't required to do that for this assignment but it's an important aspect and something to at least think about handling.

Sincerely,

Richard Nixon

2

u/RedHawkCrazed Jun 09 '14

As a 14 year old teaching himself java just out of interest, I don't have specific assignments or requirements. I'll look into it, thanks for your help.

2

u/megaman78978 Jun 08 '14

Yes it would.

It's usually a good idea to have a print statement or some other way to notify the user what type of value they should be entering.

To deal with improper input, you can learn more about Exception Handling in Java, although I would suggest learning other basic material first. The Oracle Java tutorials are pretty good when followed in order.

1

u/king_of_the_universe Jun 09 '14

You could make an extra method to read and sanitize the input, then call it three times, it's a nice exercise:

private static Integer readUserInput(final String dimension) {
    System.out.println("Enter the " + dimension + " of the cube:");
    final String input = user_input.next();
    // ...
}

... = Now parse the String. If it's not a proper number, ask again for the input (use a loop). If the user just returns an empty String, have this method return NULL instead of a number. In the main method, do this:

final Integer width = readUserInput("width");
if(width == null) {
    return;
}

and do so for the other two, too. This way, the user can abort the program any time. If none of the IFs kicked in, you can multiply the values and output the result.

"Integer" is the object version of the primitive "int". Something like that also exists for float, double, boolean, and the other primitives. The concept behind this is Java's autoboxing.

I used "Integer" instead of "int" so that the method could also explicitly return a value that says "Invalid.", which the value "null" quite nicely does. Usually, null should be avoided like a pest, but in other situations, it can be the best solution.