• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with this code.

 
Rush Art
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I just can't figure it out what is wrong with this code of mine. Especially at System.out.println . The intention is, the code reads three whole numbers from the user. And it write the lowest number entered, then the higest, the sum of all three, and then the product of all those three. The answer is shown in each line.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

You are using + before you have turned the entries into numbers. So entry 1 = 12, entry 2 = 34 and entry 3 = 56. Then you will (successfully) try to convert the String "123456" into a number.

Use int not Integer if you are doing arithmetic. Remember an int cannot be null.

Go through your two if statements with a pencil and paper, and see whether any of the conditions is true, and follow the execution. Don't write an if statement as a single line: format it like this, noting the indentation spaces and {} braces:There is more . . .

But good luck with that ( ). Correct tiny bits of code, then you can probably see what is happening with the rest of it.
 
Rush Art
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha,I see it now. But how can I make it read "en and to and tre "separately??
I can't just:



Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't say int tall three times. You would have to give three different names to the different numbers.
 
Rush Art
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Managed it. But it became a long code.... Is there any way to shorten it?

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done so far

Yes, you can shorten it. The numbers min and max should be ints not Integers.

Write down on a piece of paper what the smallest number is when you get to the first number. Then see how much you can see beyond that point.

Tiny little bits, one step at a time. That's how you do it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic