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 6 of 6

Thread: Class issues not allowing me to compile

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Class issues not allowing me to compile

    I cant get my code to compile keeps having issues with the methods/classes...... im trying to build a roulette table

    A bet is: The Player picks a particular number and commits one to as many chips as they have to that number.
    The player is allowed to place a bet on as many particular numbers as they wish.
    The rules of the game - described above - must be adhered to strictly.
    All user input must be validated to be in its appropriate range.
    A spin report must be displayed after each spin, stating the result of the spin (how much won, how much lost)
    You must keep track of how many spins the Player plays.
    You must keep track of how many bets the Player won on.
    You must keep track of how many bets the Player lost on.
    You must keep track of the number of times each number occurs as the result of a spin.
    You must write and use a statistics report method which displays:
    How many spins the Player played.
    How many bets the Player won.
    How many bets the Player lost.
    For each number that was the result of a spin, how many times that number was the result of a spin.
    The net winnings (or net losings) of the Player - Based off of starting with 500 chips.

    public class Gameroulette {
     
     
     
     
     
     
        public static int main (String[] args)
        {static  player theplayer;
        	int theodds;
            Scanner scan = new Scanner(System.in);
            int num = (int)(Math.random()*theodds); //wheel modified to input       
     
          int winnings = 0;
    	   int bet = 0;
            int x = 0, yesorno, counter = 1, p = 1;      
            int willyouplay; 
     
     
            int iamount = 500; //Initial Amount
     
                System.out.println("test");
                 	            System.out.print("Please enter wheel size : ");
                 	            theodds = scan.nextInt();
     
     
     
     
     
                 	           theplayer = new player(theodds,iamount);
     
                    //loop for the whole game
                    while (counter == 1) {
     
                        //Initial bet 
                        System.out.println("Would you like to place a Bet");
                        System.out.println("1. For bet");
    	                    System.out.println("2. to quit");
     
    	                    x = 0;//random variable for while looping
     
     
                        while (x == 0){ //looping of menu
                            System.out.print("Please enter choice : ");
                            willyouplay = scan.nextInt();	                   
    	                        if(willyouplay == 1 || willyouplay == 2) 
                        {
    	                        x = 1;  //increment of x so the loop stops	              
     
                          thewheel(theodds, iamount, bet);
     
                        }
                        else if(willyouplay == 2){ 
                            System.out.println("You have quit");
                            break;
     
                            } }
     
                    }
     
                        System.out.println("Would you like to play again?");
                        System.out.println("1. Yes");
                      System.out.println("2. No");
                        System.out.print("Enter choice : ");
                            yesorno = scan.nextInt();
     
                    if (yesorno == 2)
    	                    counter ++;
     
     
    	            System.out.println("test");   
        }  
     
     
    	        static  thewheel(int theodds,int iamount, int bet)  
    	        {                           
     
                int num = (int)(Math.random()*theodds);
                Scanner scan1 = new Scanner(System.in);
                int numInnumber;
                int counterw; 
                int counterl;
                int x=0;
                char c;
                System.out.print("How much will you bet? ");// bet placing
                bet = scan1.nextInt();
     
     
                System.out.println();
     
     
             x=0;
                while(x==0);// were i ask for multiple bets
                System.out.print("\tPlease enter your number(S) to bet on : ");
               int []number = new int[5];
            		for(int i =0; i <number.length;i++)
                         {
            			number[i]=scan1.nextInt();
     
     
     
            		System.out.print("If you want another number type y,if you want to spin s");
     
            		 c = scan1.next().charAt(0);
     
            		theplayer.setCurrentAmount(theplayer.getCurrentAmount()-bet);
     
     
                if(c =='y');{                // sees if you need to add another number to the array or spin the wheel
                number[i] = scan1.nextInt();}
                 if (c== 's' && theplayer.setCurrentAmount>0){    // checks for the spin command and that the current amount of bets is greater then 0
                	 x=1;
                 }
     
     
                    thewheel(theodds,iamount, bet);
    	            }
            		for(int i =0; i <number.length;i++)
                           {
    	            if (number[i] == num)                            // compares the random num to the bets
    {              
    	                System.out.println("You Won");
    	                bet = bet * theodds;
    	                 counterw = counterw ++;
                    System.out.println("You won " + bet );
                    theplayer.setCurrentAmount(theplayer.getCurrentAmount() + bet);
                    System.out.println("Your current money is " + theplayer.getCurrentAmount());
                    System.out.println("wins:"+counterw + "Loss:"+ counterl);
    	            }
    	            else {
    	            	counterl=counterl ++;//counts losses
    	            }
    	                System.out.println("You lost\n");
    	                System.out.println("Your have " + theplayer.getCurrentAmount());
    	                System.out.println("wins:"+counterw + "Loss:"+ counterl);
    } 				
     
            		theplayer.setCurrentAmount(theplayer.getCurrentAmount() - 500);
            		System.out.println("Net wins/losses " + theplayer.getCurrentAmount() );
     
    	        } }
     
     
     
     class player
    	   {
     
    	      private int theodds1;
     
    	      	private int initialAmount;
     
    	       private int currentAmount;
     
     
    	     public player(int theodds, int iamount)
    	        {
    	           theodds1 = theodds;
    	       setInitialAmount(iamount);
    	       setCurrentAmount(iamount);
    	     }
     
     
    	   public int gettheodds1()
    	     {        return theodds1;
    	      }
     
     
     
     
     
        public int getInitialAmount()
    	      {
            return initialAmount;
    	   	    }
     
     
     
    	     public int getCurrentAmount()
    	       {
    	         return currentAmount;
    	      }
     
     
    	          private void setInitialAmount(int amt)
    	     	    {
    	             if(amt >= 0)
    	                 initialAmount = amt;
    	    	        else
    	                  initialAmount = 0;
    	           }
     
    	   	    public void setCurrentAmount(int amt)
    	        	    {
    	        if(amt >= 0)
    		            currentAmount = amt;
    	        else
                currentAmount = 0;
    	    }
     
    	    public int calcDifference()
        {
            return currentAmount - initialAmount;
       }
    	   }}
    }
    any help would be appreciated and if you see something else thats wrong plz lemme know im sure its something simple that im overlooking
    Last edited by iamlost123; May 3rd, 2014 at 06:36 PM. Reason: some fixes


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class issues not allowing me to compile

    Please copy the full text of the compiler's error messages and paste it here so we can see the problems.

    One problem I see is misplaced { and }s. It's important that the code be properly formatted with }s being in the same column beneath the start of the statement with the {.

    There should NOT be anything on the same line with a }.
    Hiding }s makes the code very hard to read and understand.
    For example this:
    break;} }


    This code needs to be editted and the formatting of the {}s fixed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class issues not allowing me to compile

    Exception in thread "main" java.lang.NoClassDefFoundError: FileNotFoundException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)
    Caused by: java.lang.ClassNotFoundException: FileNotFoundException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 6 more




    i fixed it to be a class so that there was only one public but when i compiled i got this

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class issues not allowing me to compile

    The posted code is missing the import statements. Is there an import statement for the missing class?

    That exception is a runtime exception, not a compiler exception. It looks like there could be something with the java program.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Class issues not allowing me to compile

    No idea what's with the NoClassDefFoundError and ClassNotFoundException, but the code posted in post #1 definitely doesn't compile. There are multiple elementary problems with it.

    Important notes

    When you're new with programming:
    • Pay close attention to code indentation. Haphazard indentation is a source of errors, as in your code. See https://servicemix.apache.org/develo...standards.html for an example of how to do this properly.
    • Every time when you type an open brace ({), immediately type the close brace (}) before filling in the body. This helps ensure every open brace has a matching close brace.
    • Compile your code frequently. Don't wait until you have lots of code before compiling, as you'll end up with so many compile errors that you will not know where to start. Fix your compile errors before continuing with you coding (unless the compile errors are expected, e.g., you have yet to implement a class or a method that is being called.)

    Here are the errors that I've found:

    1. public static int main (String[] args)

    This is the main method - the first method that you learn in Java programming. And you got the method signature wrong. It should be: public static void main (String[] args)

    2. static player theplayer;

    The above line is located in the main method. You cannot use the static modifier for variables in a method. However that's not the problem. Since the theplayer variable is also needed in thewheel method, it would appear that theplayer is meant to be a class variable, so move this statement out and above the main method. E.g.,
    public class Gameroulette {
     
        static player theplayer;
     
        public static void main(String[] args) {
            ...
        }

    3. int theodds;

    This variable has not been initialised, and causes problem at "int num = (int) (Math.random() * theodds); // wheel modified to input)". Either initialise this variable during the declaration above, or move "int num = (int) (Math.random() * theodds); // wheel modified to input)" to after "theodds = scan.nextInt();".

    4. static thewheel(int theodds,int iamount, int bet)

    This method declaration is missing the return type. See Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects). Since thewheel appears to not need return a value, you must use "void" as the return type, e.g., "static void thewheel(int theodds,int iamount, int bet)"

    5. if (c == 's' && theplayer.setCurrentAmount > 0) {

    setCurrentAmount is a method in the player class: public void setCurrentAmount(int amt). Perhaps it is getCurrentAmount() that you want to call here?

    6. counterw = counterw++;

    counterw has not been initialised, and so causes a problem here. Also, with the ++ operator, "counterw++" loosely implies "counter = counter + 1", so there's no need for the initial "counterw =" in "counterw = counterw++".

    7. counterl = counterl++;// counts losses

    Same problem as counterw above.

    8. Incorrect number of braces at the end of code

    At the end of your posted code you have
    }}
    }
    There are 2 extraneous braces here - something that you could have avoided if you follow the 3 important notes above.

  6. The Following 2 Users Say Thank You to jashburn For This Useful Post:

    GregBrannon (May 4th, 2014), iamlost123 (May 5th, 2014)

  7. #6
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class issues not allowing me to compile

    bah i have given up and rewrote the code to simple if statements since the get commands are screwing with me but thanks

Similar Threads

  1. Replies: 2
    Last Post: January 11th, 2014, 06:58 AM
  2. Java Abstract class and Interface issues in a code
    By John234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 09:51 PM
  3. Static class that reads from file -> performance issues?
    By rbk in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: April 18th, 2011, 09:59 AM
  4. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 04:40 PM
  5. issues with Class
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 20th, 2010, 01:49 PM