Hi,
Thank you very much for replying.
1. I am try to execute this command
// Command
c:\temp\java> javac Retirement.java
//Result
Retirement.java:1: package corejava does not exist
import corejava.*;
^
Retirement.java:16: cannot access Console
bad class file: C:\temp\java\book\corejava\Console.class
class file contains wrong class: corejava.Console
Please remove or make sure it appears in the correct subdirectory of the classpath
goal = Console.readDouble("How much money do you need to retire?");
^
2 errors
// End of command and result
2. The file Console.class is located in c:\temp\java\book\corejava
3. Console is a public class. Method readDouble is in Console.java
4. CLASSPATH variable set as .;c:\temp\java\book
5. PATH variable set to
%path%;c:\j2sdk1.4.0_03\lib;C:\j2sdk1.4.0_03\bin;c:\temp\java
How do I find where corejava package is located? I am presuming it's in corejava directory in my case which is c:\temp\java\book\corejava. I copied this from Core Java CD that comes with CORE JAVE book. What should I do to remove those 2 errors.
Thank you Ernest. Your suggestion will be very helpful.
Raghav.
// *************************************** File Retirement.java**********
import corejava.*;
public class Retirement
{
public static void main(
String[] args)
{
double goal;
double interest;
double payment;
int years=0;
double balance = 0;
goal = Console.readDouble("How much money do you need to retire?");
payment = Console.readDouble(" How much money will you contribute every year?");
interest = Console.readDouble("Interest rate in %(ex: use 7.5 for 7.5%):");
while (balance<goal)
{
balance = (balance +payment) * (1+interest);
years ++;
}
System.out.println(" You can retire in "+years+" years.");
}
}
//*********************** End of the file Retirement.java***************
//************************** File Console.java *************************
package corejava;
/**
An easy interface to read numbers and strings from
standard input
@version 1.10 10 Mar 1997
@author Cay Horstmann
*/
public class Console
{ /**
print a prompt on the console but don't print a newline
@param prompt the prompt string to display
*/
public static void printPrompt(String prompt)
{ System.out.print(prompt + " ");
System.out.flush();
}
/**
read a string from the console. The string is
terminated by a newline
@return the input string (without the newline)
*/
public static String readLine()
{ int ch;
String r = "";
boolean done = false;
while (!done)
{ try
{ ch = System.in.read();
if (ch < 0 || (char)ch == '\n')
done = true;
else if ((char)ch != '\r') // weird--it used to do \r\n translation
r = r + (char) ch;
}
catch(java.io.IOException e)
{ done = true;
}
}
return r;
}
/**
read a string from the console. The string is
terminated by a newline
@param prompt the prompt string to display
@return the input string (without the newline)
*/
public static String readLine(String prompt)
{ printPrompt(prompt);
return readLine();
}
/**
read an integer from the console. The input is
terminated by a newline
@param prompt the prompt string to display
@return the input value as an int
@exception NumberFormatException if bad input
*/
public static int readInt(String prompt)
{ while(true)
{ printPrompt(prompt);
try
{ return Integer.valueOf
(readLine().trim()).intValue();
} catch(NumberFormatException e)
{ System.out.println
("Not an integer. Please try again!");
}
}
}
/**
read a floating point number from the console.
The input is terminated by a newline
@param prompt the prompt string to display
@return the input value as a double
@exception NumberFormatException if bad input
*/
public static double readDouble(String prompt)
{ while(true)
{ printPrompt(prompt);
try
{ return Double.parseDouble(readLine().trim());
} catch(NumberFormatException e)
{ System.out.println
("Not a floating point number. Please try again!");
}
}
}
}
// **************** End of file Console.java ********************