•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,759 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 490 | Replies: 4 | Solved
![]() |
•
•
Join Date: Jan 2007
Posts: 67
Reputation:
Rep Power: 2
Solved Threads: 0
Hi guys
I started to learn java and have a little problem\. When I try to compile my little program it says :
assgn2.java:40: variable overnight might not have been initialized
System.out.println(overnight);
thx
I started to learn java and have a little problem\. When I try to compile my little program it says :
assgn2.java:40: variable overnight might not have been initialized
System.out.println(overnight);
How is that?
The code:
import java.io.*;
public class assgn2
{
public static void main(String[] args) throws IOException
{
BufferedReader value=new BufferedReader(new InputStreamReader(System.in));
String item,itemPrice,ans;
double price,overnight;
int answer;
System.out.println("\n\t*********************************************");
System.out.println("\n\t\tSam and Ella's Delicatessen ");
System.out.println("\n\t*********************************************");
System.out.print(" What is the nome of the item: ");
item=value.readLine();
System.out.print(" What is the price of an item: ");
itemPrice=value.readLine();
price=Double.parseDouble(itemPrice);
do
{
System.out.print("Overnight delivery (0=no, 1=yes) : ");
ans=value.readLine();
answer=Integer.parseInt(ans);
if (answer==0)
{
overnight=5.00;
break;
}
else if (answer==1)
{
overnight=0.00;
break;
}
else
System.out.println(" You entered wrong value. Try again: \n");
}while((answer!=1)||(answer!=0));
System.out.println(overnight);
}
} thx
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,625
Reputation:
Rep Power: 12
Solved Threads: 311
Good, can you mark this as solved then?
There would be link bellow last post which read something like "Marks Solved"...
There would be link bellow last post which read something like "Marks Solved"...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
•
•
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation:
Rep Power: 1
Solved Threads: 6
Actually, that isn't the solution.
The way you say it makes it look like you have to do it right away.
However, the problem was that he declared a variable and wanted to print it out at the end.
His program had three different flows of which two did initialize the variable. The last flow option didn't. The idea is that between the declaration and it's use, it needs to be initialized, so not necessarilly at the same moment as the declaration.
In this case, it's a common error, because you know the loop won't end untill the variable gets its value. However, java doesn't know that, because your while loop checks other variables instead of the uninitialized one. Therefor, theoretically there still remains one path from declaration to first use which didn't initialize.
Ok, long answer for a fairly simple problem.
Black Box
The way you say it makes it look like you have to do it right away.
However, the problem was that he declared a variable and wanted to print it out at the end.
His program had three different flows of which two did initialize the variable. The last flow option didn't. The idea is that between the declaration and it's use, it needs to be initialized, so not necessarilly at the same moment as the declaration.
In this case, it's a common error, because you know the loop won't end untill the variable gets its value. However, java doesn't know that, because your while loop checks other variables instead of the uninitialized one. Therefor, theoretically there still remains one path from declaration to first use which didn't initialize.
Ok, long answer for a fairly simple problem.
Black Box
Last edited by Black Box : Dec 3rd, 2007 at 2:43 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access activation api blogger blogging blogs code com combo dani daniweb data debugging development dreamweaver dropdownlist gdata google gpl html innovation java key linux microsoft microsoft sdk for java microsystems module net news open openbsd platform product programming reuse rss serial software source sun tags vista web wysiwyg xml
- Need simple code for VB.NET (VB.NET)
- simple code (C++)
- CAn anyone provide me a very simple code for shopping cart? (PHP)
- help! I don't know what's wrong with my simple code (C)
- pointer/reference ? issues ... need help with simple code (C++)
- i need code for binary to decimal (it should be written in 'C') (C)
- Simple (I think) Code Question (PHP)
Other Threads in the Java Forum
- Previous Thread: How to 1include 100 jar files in classpath
- Next Thread: Random Salary



Linear Mode