3 Replies - 596 Views - Last Post: 19 November 2009 - 05:14 PM Rate Topic: -----

#1 eyeore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-September 09

help java problem

Posted 19 November 2009 - 04:31 AM

hi guys im really confuse about this one my compiler keep telling me variable tcost might not have been initialized is there something i miss? please do tell me im soo confused right now and NO THIS NOT A HOMEWORK im just practicing around and messing with some of my code :)





import java.util.Scanner

public class ping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);

int pball;
double fprice = 5.25;
double sprice = 3.75;
double tcost;

System.out.print("Number of PingPong Balls Purchased: ");
pball = input.nextInt();


if(pball>10)
tcost = pball *fprice;
System.out.print("The total cost is " + tcost);
if (pball <= 11){
tcost = pball *sprice;
System.out.print("The total cost is " + tcost);
; }

} public double compute(int pball, double fprice)
{
return pball * fprice;

}
public double compute1(int pball, double sprice)
{
return pball * sprice;
}
}

This post has been edited by eyeore: 19 November 2009 - 04:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: help java problem

#2 facebookuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 18-November 09

Re: help java problem

Posted 19 November 2009 - 04:53 AM

set

double tcost =0;

that should do the trick.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help java problem

Posted 19 November 2009 - 05:05 PM

After 9 posts you might start to know the rules
Two of them are not respected in this post

rule #4 :code:
rule #5 Give your topic a descriptive title, NOT "Help" or "Plz Help Me" or "I'm a noob"
"Java Help problem" is kind of useless in a forum devoted to help people with Java problem
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: help java problem

Posted 19 November 2009 - 05:14 PM

Hey, facebookuser is right, but let me explain why. At the beginning, you don't have it initialized which is Ok for a while, until you hit here:
if(pball>10)
	tcost = pball *fprice;
System.out.print("The total cost is " + tcost);
if (pball <= 11)
	tcost = pball *sprice;
System.out.print("The total cost is " + tcost);
	
}



What if pball IS less than 10? Then it never gets initialized, so that, when you call it in the next line in the print call, it won't exist... You can either do it acebookuser's way (I recommend it) or you can make sure it's set.

if(pball>10)
	tcost = pball *fprice;
else if (pball <= 10)
	 tcost = pball *sprice;
System.out.print("The total cost is " + tcost);



This insures that it is always initialized.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1