Adblock breaks this site

Java question!

Discussion in 'Programming General' started by Fate1, Nov 19, 2007.

  1. Fate1

    Fate1 Apprentice
    Banned

    Joined:
    Apr 21, 2005
    Posts:
    773
    Referrals:
    2
    Sythe Gold:
    5
    Java question!

    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
     
  2. Bori

    Bori Newcomer

    Joined:
    Mar 4, 2006
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java question!

    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';
    }
    
     
  3. _3x6

    _3x6 Active Member
    Banned

    Joined:
    Dec 23, 2007
    Posts:
    232
    Referrals:
    0
    Sythe Gold:
    0
    Java question!

    ^^ thats harder thou man
     
< Someone help me please =( | RS2 Java Max-Hit Stat Calc? >


 
 
Adblock breaks this site