7 Replies - 411 Views - Last Post: 04 March 2013 - 11:42 PM Rate Topic: -----

#1 Gu7g34r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-February 13

error '.class' expected

Posted 28 February 2013 - 11:12 PM

Hi I am having trouble with a Rock, paper, scissors program.

Just so there is no doubt, I am doing this for a course. I just want help with the error message.

.\Input>javac RPScissors2.java
RPScissors2.java:29: error: '.class' expected
                Game Rock = new Game(int comp, int other, int jj);
                                         ^
RPScissors2.java:29: error: <identifier> expected
                Game Rock = new Game(int comp, int other, int jj);
                                              ^
RPScissors2.java:29: error: not a statement
                Game Rock = new Game(int comp, int other, int jj);
                                                   ^
RPScissors2.java:29: error: ';' expected
                Game Rock = new Game(int comp, int other, int jj);
                                                        ^
RPScissors2.java:29: error: ';' expected
                Game Rock = new Game(int comp, int other, int jj);
                                                                ^
5 errors

The code I have got is rather simple. Though I am still clueless why i get errors.

// File:	SystemDetailsMenu.java
// Purpose:	Rock, Paper, scissors game.
// Author: Gu7g34r
import java.util.Scanner;

class RPScissors2 {
    int comp, other, jj;    // computer's answer, other's answer.
	String prson;           // person's answer.
	Scanner Sc = new Scanner(System.in);
	
	public void main(String[] args) {
	    
		System.out.println( "This is a Rock, Paper, Scissors Game." );    // Just a little intro.
		System.out.println( "Type R for Rock, P for Paper, S for Scissors." );
		
		prson = Sc.next();     //get the players move.
		if ( "R" == prson || "r" == prson ) {
	        other = 1;
		}
		else if( "P" == prson || "p" == prson ) {
	        other = 2;
	    	}
	    else if( "S" == prson || "s" == prson ) {
			other = 3;
		}
		else {
			System.out.println( "That is not a valid option." );
		}
		Game Rock = new Game(int comp, int other, int jj);
		
	}
	
	public int Game( int comp, int other, int jj ) {
    	String R = "Rock";      // Rock = 1.
        String P = "Paper";     // Paper = 2.
    	String S = "Scissors";  // Scissor = 3.
		
	    jj = (int)(Math.random() * 100 );
		comp = jj % 3;
		if ( Comp == 0 ) {          //get the computers move.
		    comp = 3;
		}
		
		
		switch( comp ) {                                  //print what the Computer is.
		    case 1: 
			    System.out.println( "Computer1 is " + R );
				break;
		    case 2:
			    System.out.println( "Computer1 is " + P );
				break;
		    case 3:
			    System.out.println( "Computer1 is " + S );
				break;
			default:
			    System.out.println( "Error" );
				break;
		}
		switch( other ) {                                 //print what the users is.
		    case 1: 
			    System.out.println( "Computer2 is " + R );
				break;
		    case 2:
			    System.out.println( "Computer2 is " + P );
				break;
		    case 3:
			    System.out.println( "Computer2 is " + S );
				break;
			default:
			    System.out.println( "Error" );
				break;
		}
		
		
		if( comp == other ) {                  // compute and print who wins.
		    System.out.println( "It is a tie, want to play again?" );
		}
		else if( comp == other - 1 || ( 1 == other && 3 == comp )) {
		    if( CompVSprson ) {
			    System.out.println( "Player Wins! Wsnt to play again?" );
			}
			else {
			    System.out.println( "Computer2 Wins! Wsnt to play again?" );
			}
		}
		else if( other == comp - 1 || ( 1 == comp && 3 == other )) {
		    if( CompVSprson ) {
			    System.out.println( "Computer Wins! Wsnt to play again?" );
			}
			else {
			    System.out.println( "Computer1 Wins! Wsnt to play again?" );
			}
		}
		else {
		    System.out.println( "Error" );
		}
		return 1;
	}
	
}        // Done.


any help is greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: error '.class' expected

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: error '.class' expected

Posted 28 February 2013 - 11:50 PM

This is a method and not a class (methods start with a lowercase letter usually)
public int game( int comp, int other, int jj ) {


You call the method by doing
//Notice how we are not writing int in front of the variables
int value = game(comp, other, jj);


It is unclear to me what the game method should do, maybe change the name to something better :) ? Like what is the int it's returning?

Also, you need to use the .equals method when comparing Strings
if ("R".equals(input)) {


Was This Post Helpful? 0
  • +
  • -

#3 Gu7g34r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-February 13

Re: error '.class' expected

Posted 01 March 2013 - 10:08 PM

Sorry "Game" is meant to be a class not a method.

I confused classes with functions. The idea of the lesson is input not actually classes or methods, I was trying to make it more modular.

The int is only there because the compiler complains if it does not have a return type, I really did not care what was returned.

Thanks for the info on Strings. I am not very good at initializing Char from input and I did not know how to compare it.
With the code
("R".equals(input)) {
the "input" is what i am comparing it to?

I have decided to go full blast and make a proper class for Game. I have to put the method in a loop. I have to thank you, just trying to answer your questions and the small things you say I think I have got the answer.
Was This Post Helpful? 0
  • +
  • -

#4 Gu7g34r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-February 13

Re: error '.class' expected

Posted 01 March 2013 - 11:02 PM

Here is my updated code, now in a proper class (hopefully).

With yet more Errors.
RPScissors2.java:31: error: constructor Game2 in class Game2 cannot be applied t
o given types;
                Rock = new Game2( other );
                       ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
.\Game2.java:18: error: cannot find symbol
                if ( Comp == 0 ) {          //get the computers move.
                     ^
  symbol:   variable Comp
  location: class Game2
2 errors



My amended main.

// File:	SystemDetailsMenu.java
// Purpose:	Rock, Paper, scissors game.
// Author: Gu7g34r
import java.util.Scanner;

class RPScissors2 {
    int other;              // computer's answer, other's answer.
	String prson;           // person's answer.
	Scanner Sc = new Scanner(System.in);
	
	public void main(String[] args) {
	    
		Game2 Rock;
		
		System.out.println( "This is a Rock, Paper, Scissors Game." );    // Just a little intro.
		System.out.println( "Type R for Rock, P for Paper, S for Scissors." );
		
		prson = Sc.next();     //get the players move.
		if ( "R" == prson || "r" == prson ) {
	        other = 1;
		}
		else if( "P" == prson || "p" == prson ) {
	        other = 2;
	    	}
	    else if( "S" == prson || "s" == prson ) {
			other = 3;
		}
		else {
			System.out.println( "That is not a valid option." );
		}
		Rock = new Game2( other );
		
	}
}


And Game.
// File:	Counter.java
// Purpose:	make a counter class.
// Date: 2/03/2013

public class Game2  {
    
	int jj, other, comp;
    String R = "Rock";      // Rock = 1.
    String P = "Paper";     // Paper = 2.
    String S = "Scissors";  // Scissor = 3.
	
	public void Game2( int other ) {
		
		this.other = other;
		
	    jj = (int)(Math.random() * 100 );
		comp = jj % 3;
		if ( comp == 0 ) {          //get the computers move.
		    comp = 3;
		}
		
		switch( comp ) {                          //print what the Computer is.
		    case 1: 
			    System.out.println( "Computer1 is " + R );
				break;
		    case 2:
			    System.out.println( "Computer1 is " + P );
				break;
		    case 3:
			    System.out.println( "Computer1 is " + S );
				break;
			default:
			    System.out.println( "Error" );
				break;
		}
		switch( other ) {                      //print what the users is.
		    case 1: 
			    System.out.println( "Computer2 is " + R );
				break;
		    case 2:
			    System.out.println( "Computer2 is " + P );
				break;
		    case 3:
			    System.out.println( "Computer2 is " + S );
				break;
			default:
			    System.out.println( "Error" );
				break;
		}
		
		if( comp == other ) {                  // compute and print who wins.
		    System.out.println( "It is a tie, want to play again?" );
		}
		else if( comp == other - 1 || ( 1 == other && 3 == comp )) {
			System.out.println( "Computer2 Wins! Wsnt to play again?" );
		}
		else if( other == comp - 1 || ( 1 == comp && 3 == other )) {
		    System.out.println( "Computer1 Wins! Wsnt to play again?" );
		}
		else {
		    System.out.println( "Error" );
		}
		
	}
	
}        // Done.


Sorry I still have not fixed the input.
Was This Post Helpful? 0
  • +
  • -

#5 flareback  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-February 13

Re: error '.class' expected

Posted 01 March 2013 - 11:14 PM

You don't have a constructor for your Game2 class. When you don't specify a constructor, java creates a zero arg constructor for you but you're trying to call a constructor with 1 arg. That's why you're getting the error about expecting no argument but it found an int.
public class Game {
   public Game (){
      // constructor code here
   }

   public int doSomething(int arg1) {
      // do something
      return 0;
   }
}


then to use it
Game game = new Game();
game.doSomething(2);


Was This Post Helpful? 0
  • +
  • -

#6 Gu7g34r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-February 13

Re: error '.class' expected

Posted 03 March 2013 - 06:53 PM

	public Game2( int other ) {


Exception in thread "main" java.lang.NoSuchMethodError: main


It does not matter if I do it verbose your way or just remove void, both get the same error.
Was This Post Helpful? 0
  • +
  • -

#7 flareback  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-February 13

Re: error '.class' expected

Posted 03 March 2013 - 07:02 PM

Oh, I missed that one. The main method has to be static. so make your main method:
Was This Post Helpful? 0
  • +
  • -

#8 Gu7g34r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-February 13

Re: error '.class' expected

Posted 04 March 2013 - 11:42 PM

Got it. finally a working program.

// File:	SystemDetailsMenu.java
// Purpose:	Rock, Paper, scissors game.
// Author: Gu7g34r
import java.util.Scanner;

class RPScissors2 {
	
	public static void main(String[] args) {
        
	    String prson;               // person's answer.
	    Scanner Sc = new Scanner(System.in);
		int other = 0;              // computer's answer, other's answer.
	    
		Game2 Rock;
		
		System.out.println( "\nThis is a Rock, Paper, Scissors Game." );    // Just a little intro.
		System.out.println( "Type R for Rock, P for Paper, S for Scissors." );
		
		
		while ( other == 0 ) {
			prson = Sc.next();                                //get the players move.
			if ( "R".equals(prson) || "r".equals(prson) ) {
				other = 1;
			}
			else if( "P".equals(prson) || "p".equals(prson) ) {
				other = 2;
			}
			else if( "S".equals(prson) || "s".equals(prson) ) {
				other = 3;
			}
			else {
				System.out.println( "That is not a valid option.\nTry again." );
				other = 0;
			}
		}
		
		
		
		
		
		if ( other != 4 ) {
			Rock = new Game2( other );
		}
	}
}



Game2 class

public class Game2  {
    
	int jj, other, comp;
    String R = "Rock";      // Rock = 1.
    String P = "Paper";     // Paper = 2.
    String S = "Scissors";  // Scissor = 3.
	
	public Game2( int other ) {
		
		
	    boolean CompVSprson = true;
		jj = (int)(Math.random() * 100 );
		comp = jj % 3;
		if ( comp == 0 ) {          //get the computers move.
		    comp = 3;
		}
		
		switch( comp ) {                                  //print what the Computer is.
		    case 1: 
			    System.out.println( "\nComputer1 is " + R );
				break;
		    case 2:
			    System.out.println( "\nComputer1 is " + P );
				break;
		    case 3:
			    System.out.println( "\nComputer1 is " + S );
				break;
			default:
			    System.out.println( "Error" );
				break;
		}
		switch( other ) {                                 //print what the users is.
		    case 1: 
			    System.out.println( "\nYou are " + R );
				break;
		    case 2:
			    System.out.println( "\nYou are " + P );
				break;
		    case 3:
			    System.out.println( "\nYou are " + S );
				break;
			default:
			    System.out.println( "Error" );
				break;
		}
		
		
		if( comp == other ) {                  // compute and print who wins.
		    System.out.println( "It is a tie, want to play again?" );
		}
		else if( comp == other - 1 || ( 1 == other && 3 == comp )) {
		    if( CompVSprson ) {
			    System.out.println( "Player Wins! Want to play again?" );
			}
			else {
			    System.out.println( "Computer2 Wins! Want to play again?" );
			}
		}
		else if( other == comp - 1 || ( 1 == comp && 3 == other )) {
		    if( CompVSprson ) {
			    System.out.println( "Computer Wins! Want to play again?" );
			}
			else {
			    System.out.println( "Computer1 Wins! Want to play again?" );
			}
		}
		else {
		    System.out.println( "Error" );
		}
		
		this.other = other;
		
	}
	
}        // Done.


There is my working game. Yay!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1