CSci A201/A597
Frequently Asked Questions on Lab #2


General Questions

  1. Cafe gives me wierd errors while I'm building my program. What can I do?

    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.

  2. When I run my program, the DOS window appears, but then suddenly disappears even though I've got the Console.in.readLine(); statement at the end of the program. What is wrong?

    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));
    

  3. You said you would provide instructions on using the JDK from Sun

    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.


Questions About Comma.java

  1. How do I read a string from the user? When I use Console.in.readString(), I get an error.

    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.

  2. When I try to get the length of the string that the user inputted, the length seems to be one more than what I would expect

    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.


Questions About Months.java

  1. I have no idea how to transform numbers into month names! Can you give me a hint to get me started?

    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.


Questions About Typing.java

  1. How do I get the time?

    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
    

  2. Here's my program. It won't work. What is wrong?

    // ... 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();  
    

  3. Do I have to check if the user types what he is supposed to? How do I do that?

    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.


Last modified: Mon Feb 1 14:11:22 EST 1999 by asengupt@cs.indiana.edu