Results 1 to 3 of 3
  1. #1
    MrNiceGuy is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Simple attack newbie question

    Im attempting to make a simple attack program for a game.

    Health points start out 100.00
    and Attack is a random number between 1-100.

    It works for one of the questions but then snags on the second "Attack".

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    class Attack {
    	
    	public static void main(String args[]) {
    		
    		Random myRandom = new Random();
    		Scanner myScanner = new Scanner(System.in);
    			
    			double Health = 100;
    			int Attack;
    			char reply;
    		
    		System.out.println("Attack? Y/N ");
    		reply = myScanner.findInLine(".").charAt(0);
    		
    		Attack = myRandom.nextInt(100) + 1;
    		
    		if (reply == 'Y' || reply == 'y') {
    			Health -= Attack;
    		}
    		
    		System.out.print(Health);
    		System.out.println(" Health points left");
    		
    		System.out.println("Attack Again? Y/N ");
    		reply = myScanner.findInLine(".").charAt(0);
    		
    		
    		if (reply == 'Y' || reply == 'y') {
    			Health -= Attack;
    		}
    		
    		System.out.print(Health);
    		System.out.println(" Health points left");
    		
    		
    		
    		
    		
    	}
    }
    it gives me this error

    Attack? Y/N
    y
    10.0 Health points left
    Attack Again? Y/N <---(It doesn't let me answer Y/N)
    Exception in thread "main" java.lang.NullPointerException
    at Attack.main(Attack.java:27)

    Process completed.

    Any Help greatly appreciated! :)

  2. #2
    MrNiceGuy is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Nevermind I fixed it with a

    Java Code:
    myScanner.nextline();
    after the first

    Java Code:
    System.out.print(Health);
    System.out.println(" Health points left");

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    try adding a nextLine call after reply = myScanner.findInLine(".").charAt(0); It looks like you're not reading in the newline, and that could mess with you're program and cause the NPE.

    Another note - variables should start with a lowercase letter, so attack instead of Attack, and health instead of Health.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. java newbie..simple mail server implementation
    By saurabh4dudes in forum Networking
    Replies: 1
    Last Post: 03-12-2010, 03:46 PM
  2. newbie question
    By ronguilmet in forum New To Java
    Replies: 2
    Last Post: 11-16-2009, 03:37 AM
  3. Simple newbie codes..
    By kampong in forum New To Java
    Replies: 9
    Last Post: 03-11-2009, 03:10 PM
  4. Need Help With VERY Simple Program -- Newbie!!!
    By somethingfast in forum New To Java
    Replies: 17
    Last Post: 11-25-2008, 08:38 AM
  5. Simple newbie problem: import 3rd party jar
    By jodyflorian in forum Eclipse
    Replies: 5
    Last Post: 05-30-2008, 05:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •