Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: New to java, over 100 errors please help

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default New to java, over 100 errors please help

    Hi, I'm new to java and I just started getting into what I feel is more complicated stuff and I spent all morning making a cheesy fighting game involving people I know (so don't mind the cheese), but when I try to compile I get 100 errors, hopefully due to a few small mistakes shifting the entire code. I'm sorry for the length and amount of probably unnecessary code, but I'm new.
    Also, Don't mind anything involving the class GameHelper or .getUserInput, these are in a separate class file that I got from my book's website to enable user input.
    Thanks for your help
    What I try to do in this code:
    • Have each person choose which character by inputting a number from 0 to 3, and assign a name and an array of attack styles
    • Have each player choose a type of attack (0=weak, 1=strong, 2=guard, 3=heal) and have damage done scaled to that
    • Tell the players what attack was used and for how much damage/heal for how much
    • When a character is at 0 or lower health, end the game and say the winner


    If you do take the time to help, thank you, and please try to explain in a way that a newbie can understand.

    class Character {
    	String[] AttackStyle;
    	int Health=100;
    	String name;
    	public void getAttackStyle(String charNum) {
    		int Selection=Integer.parseInt(charNum);
    		if (Selection==0) {
    			name="Garrett";
    			AttackStyle={"throws a game disc at", "punches", "kicks", "throws a grand piano at", "smashes a coke bottle over the head of", "bores to death", "guards against", "sleeps and doesn't fight"};
    		} else if (Selection==1) {
    			name="Kathy";
    			AttackStyle={"throws an apple at", "punches", "knees", "takes a paint brush and stabs", "takes a ball of ribbon and strangles", "radiates cuteness, setting on fire", "guards against", "naps and doesn't fight"};
    		} else if (Selection==2) {
    			name="Kevin";
    			AttackStyle={"throws a chess piece at", "closes a chess tutorial book on", "swings homework at", "throws a grand piano at", "lights college apps on fire, setting fire to", "unleashes k-pop star powers on", "guards against", "naps and doesn't fight"};
    		} else {
    			name="Cheyenne";
    			AttackStyle={"licks, transferring tapeworm to", "stratches", "whimpers loudly at", "slams her massive body into", "snarls and bites", "falls down the stairs onto", "guards against", "sleeps and doesn't fight"};
    		}
    	}
    	public int loseHealth(int att) {
    		Health=Health-att;
    		return Health;
    	}
    	public int gainHealth(int heal) {
    		Health=Health+heal;
    		return Health;
    	}
    }
     
     
     
    public class Fighting {
    	public static void main(String[] args) {
    		Character[] ch=new Character[2];
    		ch[0]=new Character();
    		ch[1]=new Character();
    		GameHelper g1=new GameHelper();
    		GameHelper g2=new GameHelper();
    		GameHelper g3=new GameHelper();
    		GameHelper g4=new GameHelper();
    		System.out.println();
    		System.out.println("0=Garrett");
    		System.out.println("1=Kathy");
    		System.out.println("2=Kevin");
    		System.out.println("3=Cheyenne");
    		String a=g1.getUserInput("Player 1, Choose your Character!");
    		ch[0].getAttackStyle(a);
    		System.out.println("Player 1 chooses "+ch[0].name+".");
    		String b=g2.getUserInput("Player 2, choose your Character!");
    		ch[1].getAttackStyle(b);
    		System.out.println("Player 2 chooses "+ch[1].name+".");
    		String n1=ch[0].name;
    		String n2=ch[1].name;
    		System.out.println();
    		int attackStrength1=0;
    		int attackStrength2=0;
    		int h1=100;
    		int h2=100;
    		int d1=0;
    		int d2=0;
    		int a1=0;
    		int a2=0;
    		int heal1=0;
    		int heal2=0;
    		String v1s=n1+" is charging a strong attack! They are vulnerable!");
    		String v1h=n1+" is healing! They are vulnerable!");
    		String v2s=n2+" is charging a strong attack! They are vulnerable!");
    		String v2h=n2+" is healing! They are vulnerable!");
    		boolean GameGoing=true;
    		System.out.println("0=weak attack (weak but no vulnerability)");
    		System.out.println("1=Strong attack (Strong but more vulnerable)");
    		System.out.println("2=guard (Lessens opponent's damage)");
    		System.out.println("3=sleep (regains health but leaves vulnerable)");
    		System.out.println();
    		System.out.println("Both players have 100 Health");
    		try {
    			Thread.sleep(500);
    		}
    		catch (InterruptedException) {
    		}
    		System.out.print(".");
    		try {
    			Thread.sleep(500);
    		}
    		catch (InterruptedException) {
    		}
    		System.out.print(".");
    		try {
    			Thread.sleep(500);
    		}
    		catch (InterruptedException) {
    		}
    		System.out.print(".");
    				try {
    			Thread.sleep(500);
    		}
    		catch (InterruptedException) {
    		}
    		System.out.print("Fight!");
    		String c=" ";
    		String d=" ";
    		String p1=n1+" "+ch[0].AttackStyle[a1]+" "+n2+" who now has "+h2+" health left!";
    		String p2=n2+" "+ch[1].AttackStyle[a2]+" "+n1+" who now has "+h1+" health left!";
    		String bw="But wait!";
    		while (GameGoing==true) {
    			c=g3.getUserInput(n1+", attack style?");
    			d=g4.getUserInput(n2+", attack style?");
    			attackStrength1=Integer.parseInt(c);
    			attackStrength2=Integer.parseInt(d);
    			if (attackStrength1==0) {
    				a1=(int)(Math.random()*3);
    				if (attackStrength2==0) {
    					a2=(int)(Math.random()*3);
    					d1=(int)(Math.random()*10);
    					d2=(int)(Math.random()*10);
    					h1=ch[0].loseHealth(d2);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(p2);
    				}
    				if (attackStrength2==1) {
    					System.out.println(v2s);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (InterruptedException) {
    					}
    					a2=(int)((Math.random()*3)+3);
    					d1=(int)(Math.random()*18);
    					d2=(int)(Math.random()*28);
    					h1=ch[0].loseHealth(d2);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(bw);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(p2);
    				}
    				if (attackStrength2==2) {
    					a2=7;
    					d1=(int)(Math.random()*5);
    					d2=0
    					h1=ch[0].loseHealth(d2);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(p2);
    				}
    				if (attackStrength2==3) {
    					System.out.println(v2h);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (InterruptedException ie) {
    					}
    					a2=8;
    					d1=(int)(Math.random()*20);
    					d2=0;
    					heal2=(int)(Math.random()*14);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					h2=ch[1].gainHealth(heal2);
    					System.out.println(n2+" "+ch[1].AttackStyle[a2]+"to heal and now has "+h2+" health!");
    				}
    			} else if (attackStrength1==1) {
    				a1=(int)((Math.random()*3)+3);
    				System.out.println(v1s);
    				try {
    					Thread.sleep(1000);
    				}
    				catch (InterruptedException ie) {
    				}
    				if (attackStrength2==0) {
    					a2=(int)(Math.random()*3);
    					d1=(int)(Math.random()*28);
    					d2=(int)(Math.random()*18);
    					h1=ch[0].loseHealth(d2);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(p2);
    				}
    				if (attackStrength2==1) {
    					System.out.println(v2s);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (InterruptedException) {
    					}
    					a2=(int)((Math.random()*3)+3);
    					d1=(int)(Math.random()*38);
    					d2=(int)(Math.random()*38);
    					h1=ch[0].loseHealth(d2);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(bw);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(p2);
    				}
    				if (attackStrength2==2) {
    					a2=7;
    					d1=(int)(Math.random()*10);
    					d2=0
    					h1=ch[0].loseHealth(d2);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(p2);
    				}
    				if (attackStrength2==3) {
    					System.out.println(v2h);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (InterruptedException ie) {
    					}
    					a2=8;
    					d1=(int)(Math.random()*30);
    					d2=0;
    					heal2=(int)(Math.random()*20);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					h2=ch[1].gainHealth(heal2);
    					System.out.println(n2+" "+ch[1].AttackStyle[a2]+" to heal and now has "+h2+" health!");
    				}
    			} else if (attackStrength1==2) {
    				a1=7;
    				if (attackStrength2==0) {
    					a2=(int)(Math.random()*3);
    					d1=0;
    					d2=(int)(Math.random()*5);
    					h1=ch[0].loseHealth(d2);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(p2);
    				}
    				if (attackStrength2==1) {
    					System.out.println(v2s);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (InterruptedException) {
    					}
    					a2=(int)((Math.random()*3)+3);
    					d1=0;
    					d2=(int)(Math.random()*10);
    					h1=ch[0].loseHealth(d2);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(bw);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(p2);
    				}
    				if (attackStrength2==2) {
    					a2=7;
    					d1=0;
    					d2=0;
    					h1=ch[0].loseHealth(d2);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println(p2);
    				}
    				if (attackStrength2==3) {
    					System.out.println(v2h);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (InterruptedException ie) {
    					}
    					a2=8;
    					d1=0;
    					d2=0;
    					heal2=(int)(Math.random()*20);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p1);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					h2=ch[1].gainHealth(heal2);
    					System.out.println(n2+" "+ch[1].AttackStyle[a2]+" to heal and now has "+h2+" health!");
    				}
    			} else {
    				System.out.println(v1h);
    				try {
    					Thread.sleep(1000);
    				}
    				catch (InterruptedException ie) {
    				}
    				a1=8;
    				if (attackStrength2==0) {
    					a2=(int)(Math.random()*3);
    					d1=0;
    					d2=(int)(Math.random()*20);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p2);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					heal1=(int)(Math.random()*20);
    					h1=ch[0].gainHealth(heal1);
    					System.out.println(n1+" "+ch[0].AttackStyle[a1]+"to heal and how has "+h1+" health!");
    				}
    				if (attackStrength2==1) {
    					System.out.println(v2s);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (InterruptedException ie) {
    					}
    					a2=(int)((Math.random()*3)+3);
    					d1=0;
    					d2=(int)(Math.random()*30);
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p2);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					heal1=(int)(Math.random()*20);
    					h1=ch[0].gainHealth(heal1);
    					System.out.println(n1+" "+ch[0].AttackStyle[a1]+" to heal and how has "+h1+" health!");
    				}
    				if (attackStrength2==2) {
    					a2=7;
    					d1=0;
    					d2=0;
    					h2=ch[1].loseHealth(d1);
    					System.out.println(p2);
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					heal1=(int)(Math.random()*20);
    					h1=ch[0].gainHealth(heal1);
    					System.out.println(n1+" "+ch[0].AttackStyle[a1]+"to heal and how has "+h1+" health!");
    				}
    				if (attackStrength2==3) {
    					a2=8;
    					d1=0;
    					d2=0;
    					heal1=(int)(Math.random()*20);
    					h1=ch[0].gainHealth(heal1);
    					System.out.println(n1+" "+ch[0].AttackStyle[a1]+"to heal and how has "+h1+" health!");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					System.out.println("bw");
    					try {
    						Thread.sleep(1000);
    					}
    					catch (Interrupted Exception ie) {
    					}
    					heal2=(int)(Math.random()*20);
    					h2=ch[0].gainHealth(heal2);
    					System.out.println(n2+" "+ch[1].AttackStyle[a2]+"to heal and how has "+h2+" health!");
    				}
    			}
    			if (h1<1) {
    				GameGoing=false;
    				try {
    					Thread.sleep.(1500);
    				}
    				catch (InterruptedException ie) {
    				}
    				System.out.println(n2+" wins with "+h2+" health left!!");
    			} else if (h2<1) {
    				GameGoing=false;
    				try {
    					Thread.sleep.(1500);
    				}
    				catch (InterruptedException ie) {
    				}
    				System.out.println(n1+" wins with "+h1+" health left!!");
    			}
    		}
    	}
    }
    Last edited by Renhik; July 6th, 2012 at 01:32 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,608
    Thanks
    24
    Thanked 316 Times in 295 Posts

    Default Re: New to java, over 100 errors please help

    What errors are you getting? Can you provide the stack trace? And please leave references to line numbers (if it says there is an exception on line 5, please tell us what line 5 is).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: New to java, over 100 errors please help

    Quote Originally Posted by Renhik View Post
    ... but when I try to compile I get 100 errors, ...
    Whenever I see a statement like this, it concerns me, not so much about the errors, but rather that the poster is going about coding wrong. If you're not using an IDE, you must be sure to compile your code very often, perhaps after each line or 3 of code added, and (here's the important part) make sure to fix any and all compiler errors before adding any more code. If you don't do this, you risk adding good code to bad, kind of like trying to build a bridge with a bad foundation -- it's guaranteed to fail. So I hope you fix your current errors, but more importantly, I strongly urge you to change your coding habits; you won't be sorry you did.

    Also when you get a bit more familiar with Java, start using an IDE like NetBeans or Eclipse since these guys will point out your compiler errors almost immediately as you are typing in your code, and this will help alert you to make changes before adding more code.

    Best of luck!

  4. The Following User Says Thank You to Fubarable For This Useful Post:

    Renhik (July 8th, 2012)

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: New to java, over 100 errors please help

    I'll be blunter than Fubarable: start again.

    This isn't meant to be discouraging - quite the opposite. I genuinely believe you'll make faster progress by addressing each compiler message as it occurs. As aussie says: post the message(s) here and say what line(s) are being referred to. Much of the code is repetition of the same logic (multiple characters, multiple health levels etc) so get one to compile and the rest will (very likely) follow. As far as starting again is concerned we know how it feels because we've all been there.

    -----

    A couple of other things which are intended, like frequent compiles, to become unthinking, effortless, habit:

    * Use standard Java coding conventions. Variables begin with a lower case letter and class with an upper case one. Variables should be descriptive of the thing they represent. Imagine reading your code as a stranger might or as you might in 6 months: avoid nondescriptive variables like d2.

    * Compile every few lines. And listen to the compiler. But also test (by running the application) each time you finish a method. Ensure that the method does just what you expect (and consider documenting what that is in the code!)

Similar Threads

  1. Replies: 3
    Last Post: March 6th, 2012, 02:50 AM
  2. Strange errors with java.nio.file
    By jnewb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 4th, 2012, 01:37 PM
  3. hi - Java swing errors
    By genlastudio in forum AWT / Java Swing
    Replies: 1
    Last Post: March 22nd, 2011, 05:49 PM
  4. Replies: 3
    Last Post: February 23rd, 2011, 12:27 AM
  5. miniBuilder on Linux- java errors
    By nwtjv in forum Exceptions
    Replies: 0
    Last Post: March 25th, 2010, 06:54 AM

Tags for this Thread