Click to See Complete Forum and Search --> : Compiling error


Ronen1313
03-02-2007, 11:49 PM
I am taking a Beginners Java course and I am trying to complile a program I wrote but I keep getting errors that my

char code variable is not initiating. The problem is that I have to use input from a command line prompt to compare values in an if else statement. I have been looking at this thing for hours. Please keep in mind that I am extremely new at Java. It may be a simple fix but I am not sure what it is. Here is the source code for my java file:

//Discount.java

import java.io.*;
import java.text.DecimalFormat;

public class Discount {

public static void main(String args[]) {
// Introduction
System.out.println("\n Class: Java Beginning Programming"
+ "\n Lab/Assignments: A03 Discount"
+ "\n Student: Nakijma Nelson"
+ "\n Date: 03/01/2007");

//declare varibles
float price = 0,
discount = 0,
disAmount= 0,
disPrice = 0;

String value = "";

//declare buffered reader
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));

DecimalFormat twoDigits = new DecimalFormat("0.00");

//Get and covert user input from command line
try {
System.out.print("\n\tEnter Price ........... ");
value = reader.readLine();
// convert string to float
price = Float.parseFloat(value);

System.out.print("\n\tEnter Price Code...... ");
value = reader.readLine();
char code = value.charAt(0);
//convert string to char


} // end try block

catch (IOException ioe) {
System.out.println("\n I O Exception Occurered");
} // end catch


//define conditions
if(code == 'H'){
discount = .5F ;
}
else if(code == 'F'){
discount = .4F ;
}
else if(code == 'T'){
discount = .35F ;
}
else if(code == 'Q'){
discount = .25F ;
}
else if(code == 'Z'){
System.out.println("\n No Discount.");
System.exit(1) ;
}
else {
System.out.println("\n Incorrect Value Code.");
System.exit(2);
}

// evalute expressions
disAmount = price * discount;
disPrice = price - disAmount;

//display results

System.out.println("\n Price Code = > \t" + code);
System.out.println("\n Original Price = > \t $" + twoDigits.format(price));
System.out.println("\n Discount Amount = > \t $" + twoDigits.format(disAmount));
System.out.println("\n Disconnted Price = > \t $" + twoDigits.format(disPrice));

} //end main

} //end Class

These are the area a few of the error messages I get when I try to compile the file.

Discount.java :50: can not find symbol
symbol: variable code
location: class Discount
if(code=="H") {
/\ -------> (arrow is under the code variable)

......etc. for the rest of the conditions in the if..else statement.


Any help would be greatly appreciated. Thanks

mickscool
03-03-2007, 08:07 AM
Hi,
The error code tells you that its not able to find symbol "code",that means you need to define that symbol globally,instead of you have defined it locally in the try-catch block.Anyways,I have increased the try-catch block span,just to make it simple and working for you.
Check this out:

//Discount.java

import java.io.*;
import java.text.DecimalFormat;

public class Discount {

public static void main(String args[]) {
// Introduction
System.out.println("\n Class: Java Beginning Programming"
+ "\n Lab/Assignments: A03 Discount"
+ "\n Student: Nakijma Nelson"
+ "\n Date: 03/01/2007");

//declare varibles
float price = 0,
discount = 0,
disAmount= 0,
disPrice = 0;

String value = "";

//declare buffered reader
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));

DecimalFormat twoDigits = new DecimalFormat("0.00");

//Get and covert user input from command line
try {
System.out.print("\n\tEnter Price ........... ");
value = reader.readLine();
// convert string to float
price = Float.parseFloat(value);

System.out.print("\n\tEnter Price Code...... ");
value = reader.readLine();
char code = value.charAt(0);
//convert string to char




//define conditions
if(code == 'H'){
discount = .5F ;
}