4 Replies - 547 Views - Last Post: 14 January 2010 - 08:18 PM Rate Topic: -----

#1 Jana.Newbould  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-December 09

ConversionApplication

Post icon  Posted 14 January 2010 - 05:53 PM

I am having an issue with my code at this point:

// displays the converted value in Euros
System.out.printf( "The equivalent value is %.2f Euros.", euroAmt );

I get the error of: variable euroAmt might not have been initialized

Here is my code, any help would be appreciated:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Administrator
*/
/*
Currency Converter Application
IT171 DB2
*/
import java.util.Scanner;

public class ConversionApplication
{
public static void main (String args[] )
{

// variable declarations
double convRate;
double dollarAmt = -1;
double euroAmt;


// helper object declarations
EuroConverter myConverter;
Scanner input = new Scanner(System.in);

// Get user input for conversion rate and construct EroConverter
System.out.print( "Please enter conversion rate (x.xx): ");
convRate = input.nextDouble();

while (convRate <= 0)
{
System.out.print( "Rate cannot be less than zero. Try again: ");
convRate = input.nextDouble();
}

// construct EuroConverter with conversion rate from user
myConverter = new EuroConverter(convRate);
System.out.println("Rate: " + myConverter.getConversionRate());

// begin converting dollars yntil the user enters 0.
while (dollarAmt != 0)
{

// get dollar amount from user (error trap for bad amounts)
System.out.print( "\nPlease enter the dollar amount to convert ");
System.out.print("or 0 to quit:");
dollarAmt = input.nextDouble();

while (dollarAmt < 0)
{
System.out.print( "Dollar amount cannot be negative: ");
dollarAmt = input.nextDouble();
}

// convert if dollar is not zero.
if (dollarAmt != 0)
{
euroAmt = myConverter.convertToEuros(dollarAmt);
}

// displays the converted value in Euros
System.out.printf( "The equivalent value is %.2f Euros.", euroAmt );

}

System.out.println("Thank you for using our Euro converter.");
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: ConversionApplication

#2 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: ConversionApplication

Posted 14 January 2010 - 06:03 PM

Firstly, paste your code in tags like this :code:

This will make it easier to read your code.

Secondly..you have not initialized the variable "euroAmt" that is where the problem lies.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,467
  • Joined: 27-December 08

Re: ConversionApplication

Posted 14 January 2010 - 06:05 PM

In the future, please :code:. It makes it a lot easier for us to read and therefore debug your code. :)

Your error lies at this line double euroAmt;. While you declare euroAmt, it is a good idea to go ahead and initialize it (or assign it an initial value). So if you say double euroAmt = 0;, this should fix the error. The reason you initialize variables before using them is to assure that you can use them. As you have seen, there are cases where the compiler will throw a fit if the variable isn't guaranteed to be initialized before it is used. In your code, I believe your first attempt at using the variable was in an assignment in a conditional. Because it may or may not receive the assignment (dependent on the initialization), it may not have a value when you go to display it in the printf() statement.
Was This Post Helpful? 1
  • +
  • -

#4 Jana.Newbould  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-December 09

Re: ConversionApplication

Posted 14 January 2010 - 07:58 PM

View Postmacosxnerd101, on 14 Jan, 2010 - 05:05 PM, said:

In the future, please :code:. It makes it a lot easier for us to read and therefore debug your code. :)

Your error lies at this line double euroAmt;. While you declare euroAmt, it is a good idea to go ahead and initialize it (or assign it an initial value). So if you say double euroAmt = 0;, this should fix the error. The reason you initialize variables before using them is to assure that you can use them. As you have seen, there are cases where the compiler will throw a fit if the variable isn't guaranteed to be initialized before it is used. In your code, I believe your first attempt at using the variable was in an assignment in a conditional. Because it may or may not receive the assignment (dependent on the initialization), it may not have a value when you go to display it in the printf() statement.



Thank you, Thank you, Thank you!!
Was This Post Helpful? 0
  • +
  • -

#5 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: ConversionApplication

Posted 14 January 2010 - 08:18 PM

Instead of:
		// convert if dollar is not zero.
		if (dollarAmt != 0)
		{
			euroAmt = myConverter.convertToEuros(dollarAmt);
		}

		// displays the converted value in Euros
		System.out.printf( "The equivalent value is %.2f Euros.", euroAmt );


Do:
		// convert if dollar is not zero.
		if (dollarAmt != 0)
		{
			euroAmt = myConverter.convertToEuros(dollarAmt);

			// displays the converted value in Euros
			System.out.printf( "The equivalent value is %.2f Euros.", euroAmt );
		}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1