Check the following (in order of priority):
Strange things do happen sometimes, so make sure you see someone during office hours early in the week (i.e. not on Thursday morning) if you have problems.
Your program has a run-time, or logical, error in it. When this happens, the program terminates prematurely and it never gets to that Console.in.readLine(); statement at the end.
Your error is probably due to a substring index out of bounds. The numbers that you pass to the substring method must be legal indices into the string (or one beyond the last index.) For example, all of the following calls to substring will cause run-time errors:
String capital; capital = "Paris"; System.out.println(capital.substring(-1, 3)); System.out.println(capital.substring(3, 6)); System.out.println(capital.substring(6));
Yes. here is a brief installation and use instruction. This instruction is for Windows (win32) platforms only. You may need to make changes according to your platform.
In the README file, there is a section on PATH and CLASSPATH, I am including that here verbatim other than comments within [brackets]:
-----------------------------------------------------------------------
MICROSOFT WINDOWS PATH and CLASSPATH
-----------------------------------------------------------------------
[snip]
Windows NT only - If you are using Windows NT, it is
preferable to make the following environment variable
changes in the Control Panel. Start the Control Panel,
select System, then edit the environment variables.
1. PATH - Add the absolute path of the "jdk1.1.7B\bin"
directory to your PATH statement as follows.
The PATH statement enables Windows to find the executables
(javac, java, javadoc, etc.) from any current directory.
To find out the current value of your PATH, at the
DOS prompt type:
C:\%gt; path
To change the PATH, open the AUTOEXEC.BAT file and make the
change to the PATH statement. To edit the AUTOEXEC.BAT
file in Windows 95:
i. Start a text editor by choosing "Start", "Programs",
"Accessories", and choosing WordPad or NotePad.
ii. Choose Open from the File menu and type "c:\autoexec.bat"
for the filename This will open the file for editing.
iii. Look for the PATH statement. Notice that the PATH statement
is a series of directories separated by semi-colons (;).
Windows looks for programs in the PATH directories in order,
from left to right. Look for other versions in the PATH.
There should only be one path to a classes.zip file.
When in doubt, put the java directory at the end of
the path statement. For example, in the following PATH
statement, we have added the java directory at the end:
PATH C:\WINDOWS;C:\WINDOWS\COMMAND;C:\;C:\DOS;C:\JDK1.1.7B\BIN
To make the path take effect, execute the following:
C:\%gt; autoexec.bat
2. CLASSPATH Environment Variable - If you follow the default
installation, you do not need to set CLASSPATH, because the
tools automatically set it for you. If your CLASSPATH has
not previously been set, you can skip this step.
[Jit's comments: You can skip this step if this is the first time you
installed Java in your system.]
C:\> java -version
You should see a message saying the version of Java on your system,
which should be 1.1.7b. You can now compile a program by typing
javac filename.java and run a program by typing java
filename. (Note that you don't need to give an extension while
running your proram.)
Do not use the readString() method. You must use Console.in.readLine() or even better, Console.in.readWord() to read the input from the user into a String variable.
Are you using Console.in.readLine()? If so, it seems to include the return that the user hits after she imputs the word. Instead of readLine(), use readWord() - this should work as expected.
The textbook suggests that you set up a String variable containing the month names, separated by one or more blanks so that all "fields" are of the same width. Just to make this absolutely clear, I'll write this initial value as an expression joining the twelve different month names. This way, I can write all the names in a column and I can easily verify that each "field" is, indeed, of the same width.
"January " + // month 1 "February " + // month 2 "March " + // month 3 "April " + // month 4 "May " + // month 5 "June " + // month 6 "July " + // month 7 "August " + // month 8 "September " + // month 9 "October " + // month 10 "November " + // month 11 "December " // month 12
How wide is each field? Let's count:
// 1234567890 "January "
In my case, each month takes up 10 characters in the String. (Note that it could also be done with a field width of 9.) Of course, we must keep in mind, that the J in January is at index zero.
// 0123456789 "January "
So, how does this help? Well, there is a direct correlation between the starting index of the month name and the month number. Consider the following table:
| Month number | Start index |
| 1 | 0 |
| 2 | 10 |
| 3 | 20 |
| ... | ... |
You try to fill in entries for months 4, 5, 6, etc. Now, looking at the table, you need to try to come up with a formula relating the month number to the starting index of the month name in your string. If you can do this, then you can use the substring method to extract the actual month name and print it.
To find out the current time, create a Time object by calling the default constructor (i.e. the constructor with no arguments.) Here's an example:
Time thisVeryInstant; // set up a label for the object
thisVeryInstant = new Time(); // create the actual object by
// calling the default constructor
// ... lots of deleted code ... time rightNow; rightNow = new time(); // ... lots of deleted code ...
First of all, please provide more information about what is wrong with your program when you mail it to someone for help. If it fails to build because of a syntax error, then provide us with the error message. If it runs, but then produces the wrong results or bombs altogether, then let us know the details. This way we'll be able to identify your problem faster and easier.
In this particular program, the problem is that time is given in all lowercase. It should be Time, not time. Here's the corrected code fragment:
Time rightNow; rightNow = new Time();
No, you do not have to check to see if the user types the required sentence properly. This means that he can very easily fool your program into thinking he types really fast. But, right now, we are not concerned with typing accuracy, just speed.