On Wed, 18 Apr 2012 04:18:48 -0700 (PDT), Robert Webber
<
robertjo...@googlemail.com> wrote:
>Hello,
>Ok I'm almost embarrased to ask this as I guess it will be so simple.
>I am going through the "Java for Dummies" Book.
>
>Its now explaining about Do and while loops.
>Basically I get an issue on when I scan in Char variables.
>
>I've tried many things including scanning the char variable then
>altering to an int using an IF statement, but still I get problems. It
>seems like the scanner is not looking at the current character I'm
>writing.
>
>Here is the code:
> package ProgramUnits;
>
>import java.util.Scanner;
>import static java.lang.System.out;
>
>/**
> *
> * @author ESWebbeR
> */
>public class HotelRooms {
>
> public static void main(String args[]){
> Scanner myScanner = new Scanner(System.in);
> Scanner myScanner2 = new Scanner(System.in);
Warning: Variable myScanner2 not used.
>
> int whichRoom, numGuests, Otra, A;
Warning: Variables Otra, A not used.
Style: Do not use capital letters for variable names.
Style: Ond declaration per line.
> int guestsIn[];
>
>
> guestsIn = new int[11];
Style: Better to combine: int guestsIn[] = new int[11];
>
> for (int roomNum = 0; roomNum < 10; roomNum++){
Query: why "< 10" rather than "< 11"?
> guestsIn[roomNum] = 0;
> }
>
> do{
> out.print("Room number: ");
> whichRoom = myScanner.nextInt();
> out.print("How many guests? ");
> numGuests = myScanner.nextInt();
> guestsIn[whichRoom] = numGuests;
> out.println();
> out.print("Do another? ");
>
> } while (myScanner.findInLine(".").charAt(0) == 'Y');
Question: What does findInLine() return if it cannot find a match?
Question: What happens if you try to call the charAt() method on what
findInLine() just returned in the case of no match?
>
> out.println();
> out.println("Room\tGuests");
>
> for(int roomNum = 0; roomNum < 10; roomNum++){
> out.print(roomNum);
> out.print("\t");
> out.println(guestsIn[roomNum]);
> }
> }
>
>}
>
>and here is the answer:
>run:
>Room number: 1
>How many guests? 2
>
>Do another? Y
>Room number: 2
>How many guests? 3
>
>Exception in thread "main" java.lang.NullPointerException
You have a NullPointerException. Look up what can cause that. I can
guarantee you will see that exceptin many times in your Java
programming.
>Do another? at ProgramUnits.HotelRooms.main(HotelRooms.java:39)
The "HotelRooms.java:39" part tells you that the error was noticed at
line 39 of your HotelRooms.java source file. The cause of the error
is likely to be there, or in some code that was executed immediately
before that line was reached.
rossum