Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Very Basic problem for complete beginner

7 views
Skip to first unread message

Robert Webber

unread,
Apr 18, 2012, 7:18:48 AM4/18/12
to
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);

int whichRoom, numGuests, Otra, A;
int guestsIn[];


guestsIn = new int[11];

for (int roomNum = 0; roomNum < 10; roomNum++){
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');

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
Do another? at ProgramUnits.HotelRooms.main(HotelRooms.java:39)
Java Result: 1
BUILD SUCCESSFUL (total time: 13 seconds)


Thanks for the help in advance.

Roedy Green

unread,
Apr 18, 2012, 8:55:56 AM4/18/12
to
On Wed, 18 Apr 2012 04:18:48 -0700 (PDT), Robert Webber
<robertjo...@googlemail.com> wrote, quoted or indirectly quoted
someone who said :

>NullPointerException

Rather than holding your hand...
see
http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION

it helps people help you if you point out which line is 39
> at ProgramUnits.HotelRooms.main(HotelRooms.java:39)

Why do you have two scanners?

see http://mindprod.com/jgloss/codingconventions.html
Otra and A should be otra and a.

Hint: read up on Scanner.findInLine. What possible things can it
return?
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..

rossum

unread,
Apr 18, 2012, 9:14:29 AM4/18/12
to
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
0 new messages