Results 1 to 3 of 3
Thread: Simple attack newbie question
- 11-13-2010, 06:15 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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".
it gives me this errorJava 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"); } }
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! :)
- 11-13-2010, 06:25 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Nevermind I fixed it with a
after the firstJava Code:myScanner.nextline();
Java Code:System.out.print(Health); System.out.println(" Health points left");
- 11-13-2010, 06:27 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
-
java newbie..simple mail server implementation
By saurabh4dudes in forum NetworkingReplies: 1Last Post: 03-12-2010, 02:46 PM -
newbie question
By ronguilmet in forum New To JavaReplies: 2Last Post: 11-16-2009, 02:37 AM -
Simple newbie codes..
By kampong in forum New To JavaReplies: 9Last Post: 03-11-2009, 02:10 PM -
Need Help With VERY Simple Program -- Newbie!!!
By somethingfast in forum New To JavaReplies: 17Last Post: 11-25-2008, 07:38 AM -
Simple newbie problem: import 3rd party jar
By jodyflorian in forum EclipseReplies: 5Last Post: 05-30-2008, 04:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks