User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jan 2007
Posts: 67
Reputation: mauro21pl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

very simple code

  #1  
Dec 3rd, 2007
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);

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Posts: 67
Reputation: mauro21pl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

Re: very simple code

  #2  
Dec 3rd, 2007
never mind ,got it
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,625
Reputation: peter_budo is just really nice peter_budo is just really nice peter_budo is just really nice peter_budo is just really nice 
Rep Power: 12
Solved Threads: 311
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: very simple code

  #3  
Dec 3rd, 2007
Good, can you mark this as solved then?
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
Reply With Quote  
Join Date: Dec 2007
Posts: 6
Reputation: tommybobby is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
tommybobby tommybobby is offline Offline
Newbie Poster

Re: very simple code

  #4  
Dec 3rd, 2007
You should post the solution even if you solved it yourself to help others potentially having the same issues.


Solution: Local variables need to be initialized with a value when declared.
Reply With Quote  
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation: Black Box is on a distinguished road 
Rep Power: 1
Solved Threads: 6
Black Box Black Box is offline Offline
Junior Poster in Training

Re: very simple code

  #5  
Dec 3rd, 2007
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
Last edited by Black Box : Dec 3rd, 2007 at 2:43 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:28 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC