• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variable might not have been initialized.  RSS feed

 
John Ellsworth
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have just started a Java class (which is my first programming class) and just received my first assignment. "Write a program that prompts the user to input a decimal number and ouputs the number rounded to the nearest integer." Here is what I have so far...



//This is my first Java program for IT 145.
//It is designed to prompt a user to input a decimal number and produce an integer.

I am getting the error "variable answer might not have been initialized." I have tried to initialize the variable by giving it a value of 0. That gets rid of the error, but always returns an answer of 0 no matter what decimal is entered. How do initialize a variable when its value depends on what is entered? Thanks.

John
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ellsworth wrote:First of all, sorry for double post. I had originally posted this question in the wrong area and couldn't delete it.


I deleted your duplicate post -- and moved the original post for you. Next time, just make a request to a moderator to move the post for you.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops... it looks like your other post had a response as it was being deleted...


K. Tsang wrote:Both of your variables input and answer are local variables which needs to be declared or have some value assigned to it before output or using it.

Now your code actually assigned a value for variable input. Given your requirement, if you just declared it as 0 (zero) and output it you surely got 0 all the time. Since you need to round the decimal/double input to integer, what methods or API can you use?

 
John Ellsworth
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Oops... it looks like your other post had a response as it was being deleted...


K. Tsang wrote:Both of your variables input and answer are local variables which needs to be declared or have some value assigned to it before output or using it.

Now your code actually assigned a value for variable input. Given your requirement, if you just declared it as 0 (zero) and output it you surely got 0 all the time. Since you need to round the decimal/double input to integer, what methods or API can you use?




Thanks. Here's what I came up with. I tried the predefined mathematical method for rounding. However, it would not allow to use "answer" if it was declared as "int". I changed it to "long" instead. It produced the desired result. What do you think?


//This is my first Java program for IT 145.
//It is designed to prompt a user to input a decimal number.
//The program will then round to the nearest integer.



 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you looks at the documentation for the method I think you are using for rounding, you find that it returns a long. Change the type of answer to long.
 
John Ellsworth
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did just that and it seemed to work just fine. Thanks for the input.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!