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

    Default Newbie Loop Question

    Im attempting to make a simple attack program using a loop. It should attack and loop until the health reaches zero. It gives me an error and I think the code looks correct but im not sure. Here is the code:

    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;
    			
    		
    	while ( health > 0 )
    	{ 
    		System.out.println("Attack? Y/N ");
    		reply = myScanner.findInLine(".").charAt(0);
    		
    		attack = myRandom.nextInt(10) + 15;
    		
    		if (reply == 'Y' || reply == 'y') {
    			health -= attack;
    		}
    		
    		System.out.print(health);
    		System.out.println(" Health points left");
    		
    		{
    		
    	}
    }
    This is the error code it gives me and it points at the last brace?

    "reached end of file while parsing" location line 33

    Any help is greatly appreciated :)

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

    Default

    lol sorry my third brace from the bottom was turned the wrong way woops...

  3. #3
    funnyboy's Avatar
    funnyboy is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    Just a little tip:
    except for

    System.out.print(health);
    System.out.println(" Health points left");

    You could simply write

    System.out.println(health + " Health points left");

    or

    System.out.printf("%d Health points left", health);

    (%d shows the computer, that he should output the given variable of type integer)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Seems to me you've formatted the code incorrectly. At the end few of closing braces are missing. Because the error reached end of file while parsing is a compile time error notifying that the class is not properly added into blocks using open and close curly braces.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    As a good practice when you are coding once you add a open brace just type the close bracket as well, before even write to a single line of code in the block. In that way you never end up with such an error.

Similar Threads

  1. Newbie question
    By MrNiceGuy in forum New To Java
    Replies: 8
    Last Post: 11-12-2010, 05:29 AM
  2. newbie question
    By ronguilmet in forum New To Java
    Replies: 2
    Last Post: 11-16-2009, 02:37 AM
  3. Replies: 4
    Last Post: 05-18-2009, 02:23 PM
  4. Newbie having problems with for loop
    By Dannii in forum New To Java
    Replies: 4
    Last Post: 04-13-2009, 11:52 PM
  5. Newbie question; Vectors
    By Kern in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 06:59 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
  •