• Log in or sign up for an account here: https://www.sythe.org/login

Java question!

Fate1

Apprentice

Banned
Joined
Apr 21, 2005
Sythe Gold
5
Messages
773
Age
34
look below..
Code:
/**
 * @(#)JavaProgramming.java
 *
 *
 * @author 
 * @version 1.00 2007/11/18
 */
 import java.util.Scanner;
 import static java.lang.System.out;

public class JavaProgramming {

    public static void main(String[] args) {
    	Scanner myScanner = new Scanner(System.in);
    	int whichRoom, guestNum;
    	int guestIn[];
    	guestIn = new int[10];
    	
    	for (int roomNum = 0; roomNum < 10; roomNum++) {
    		guestIn[roomNum] =0;
    	}
		do {
			out.print("Room number: ");
			whichRoom = myScanner.nextInt();
			out.print("How many guests: ");
			guestNum = myScanner.nextInt();
			guestIn[whichRoom] = guestNum;
			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(guestIn[roomNum]);
		}
	}
 }

Hi. My question is the line reply = myScanner.findInLine(".").charAt(0); that works for me sometimes and sometimes doesn't. Yes I have declared reply and myScanner and such. It will compile and when I run it, it comes up with an error NullPointerException.. but sometimes in other programs I write it will work fine.. The one I wrote above it wont work and it should.. any help?

Thanks
 
Personally I wouldn't use findInLine at all, but I'd do it like this:
Code:
boolean cont = true;
while (cont)
{
   //code here
   out.println("Do you want to continue?");
   String answer = myScanner.next();
   cont = answer.length() > 0 && answer.charAt(0) == 'Y';
}
 
^^ thats harder thou man
 
Runescape Gamblingrunescape gambling
Back
Top