-4

So I have this:

String banana = "5";
int apple = 7;

result = banana * apple;

and I get this error:

not a statement bad operand types for binary operator '' first type: String second type: int*

How to I fix that?

Jere
  • 915
  • 1
  • 7
  • 23
user3561829
  • 7
  • 1
  • 3
  • Yes error it says correct, you can't multiply string and int. You need to use `Integer.parseInt()` to convert String to integer before you can multiply. – Pradeep Simha Apr 29 '14 at 17:07
  • @PradeepSimha - thank you – user3561829 Apr 29 '14 at 17:11
  • Could you like elaborate a little on your code? Like what are the types of banana and apple, how where they initiated etc.. This question really has no answer because if you did declare banana as a String and apple as an int, then you cannot multiply them – ThaBomb Apr 29 '14 at 17:11
  • Are you actually doing anything with `banana * apple`? Even if they were both `int`'s, if you multiply them but don't assign the result to anything or do anything useful with it, you'll get `not a statement`. – ajb Apr 29 '14 at 17:12
  • @ThaBomb - I know I can't multiply them, that's why I needed to know what to do to change that. like Pradeep ^ said, I need to change my String with an Integer.parseIntr() – user3561829 Apr 29 '14 at 17:13
  • @ajb - yes I'm using them. the "banana" is a string that parses the user's input, and the "apple" is an int which represents a random generated number. – user3561829 Apr 29 '14 at 17:22
  • @user3561829 No, the question was: are you using the **result** of the multiplication `banana * apple`? Are you storing the result in a variable, or using the result when calling a method, or something? If your _entire_ statement is `(banana * apple);` you will get `not a statement`. – ajb Apr 29 '14 at 17:25
  • @ajb - oh, sorry. no, im using it in a while loop. – user3561829 Apr 29 '14 at 17:28

2 Answers2

3
String banana = "2";
int apple = 1;

int result = (Integer.valueOf(banana))*apple;

System.out.println("Result= " + result);

Try that ;)

DavideBar
  • 75
  • 10
  • I didn't use parseInt because this way you have 2 answers, and with parseInt you need to encapsulate with a try and catch – DavideBar Apr 29 '14 at 17:19
  • Because the valueOf() will get the int value of a String, and the parseInt will try to convert a string to int if your string is like this "123bla" parseInt will give you an error, thats why you need to use try and catch – DavideBar Apr 29 '14 at 17:23
  • ok I see, makes things simpler. – user3561829 Apr 29 '14 at 17:24
  • [valueOf internally uses parseInt](http://stackoverflow.com/questions/7355024/integer-valueof-vs-integer-parseint) .. just for you to know that you are **parsing** – Srinath Ganesh Apr 29 '14 at 17:25
  • Never thought of that – DavideBar Apr 29 '14 at 17:27
0

Probably you did something like:

String banana = "3";
int apple = 2;

int result = banana*apple;

Just use Integer.parseInt():

int result = Integer.parseInt(banana)*apple;
pfernandom
  • 831
  • 9
  • 21
  • yess thanks. actually I did String banana = (userInput.getText()); and apple = (userInteger + userInteger2); but yeah – user3561829 Apr 29 '14 at 17:15