6 Replies - 282 Views - Last Post: 26 August 2012 - 11:27 PM Rate Topic: -----

#1 Porter51  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-August 12

I can't get this number guessing game to run. I get Not main metho

Posted 26 August 2012 - 10:16 PM

<Removed>

This post has been edited by macosxnerd101: 27 August 2012 - 06:52 AM
Reason for edit:: Removed code since it was not in code tags and the OP posted it down further in code tags

Is This A Good Question/Topic? 0
  • +

Replies To: I can't get this number guessing game to run. I get Not main metho

#2 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: I can't get this number guessing game to run. I get Not main metho

Posted 26 August 2012 - 10:19 PM

1) Put your code inside of code tags, because we can't read what you're trying to say.
2) Tell us the error you're actually getting. Don't just post it in your title, show us the error's text.

Looking over it, you need to add a main() function. Otherwise, this isn't going to work. Your best bet is to ask whoever gave you this code to help you fix it.
Was This Post Helpful? 0
  • +
  • -

#3 Porter51  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-August 12

Re: I can't get this number guessing game to run. I get Not main metho

Posted 26 August 2012 - 10:28 PM

import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;


//create class GuessingGame that extends to JFrame
public class Program6 extends JFrame
{
	//define variables
	   private JLabel instruction1JLabel;
	   private JLabel instruction2JLabel;
	   private JLabel instruction3JLabel;
	   private JLabel outputJLabel;
	   private JTextField inputJTextField;
	   private JButton playAgainJButton;
  
	   private int myGuess = 0;
	   private int previousDistance = 0;
	   private int currentDistance = 0;
	   private int randomNumber = ((int)((Math.random() * 1000) + 1));

	   //Constructor
	public Program6()
	   {
		super( "Guessing Game" );
		 setLayout( new FlowLayout() );
	
		System.out.println(myGuess);
		System.out.println(currentDistance);
		  System.out.println(previousDistance);
		System.out.println("random number is " + randomNumber);	

		  JLabel instruction1JLabel = new JLabel( "I have a number between 1 and 1000." );
		  add( instruction1JLabel );
	  
		  JLabel instruction2JLabel = new JLabel( "Can you guess my number?" );
		  add ( instruction2JLabel);
	  
		  JLabel instruction3JLabel = new JLabel( "Please enter your first guess\n");
		  add( instruction3JLabel );
	  
		  inputJTextField = new JTextField( 12 );
		  add( inputJTextField );

	outputJLabel = new JLabel( "	" );
		  add( outputJLabel );
	  
		  JButton playAgainJButton = new JButton( "Play Again" );
		  add ( playAgainJButton );
	  
		  InputTextFieldHandler inputTextFieldHandler = new InputTextFieldHandler();
		  inputJTextField.addActionListener( inputTextFieldHandler );
	  
		  PlayAgainButtonHandler playAgainButtonHandler = new PlayAgainButtonHandler();
		  playAgainJButton.addActionListener( playAgainButtonHandler );
	  
		  
	  
   }

   // inner class for input event handling
   private class InputTextFieldHandler implements ActionListener
   {
	  // handle input event
	  public void actionPerformed( ActionEvent event )
	  {
		   myGuess = Integer.parseInt(inputJTextField.getText());
		currentDistance = Math.abs(myGuess - randomNumber);
		  //previousDistance = currentDistance;
		
		inputJTextField.setBackground( Color.WHITE );
		 outputJLabel.setText( "	" );
		 inputJTextField.setText( "" );
		 //inputJTextField.setEditable( false );
		 inputJTextField.requestFocus();
			
			   
		  if ( previousDistance != 0 )
		{
			  if ( currentDistance == previousDistance )
				  inputJTextField.setBackground( Color.WHITE );
				  
			  else if ( currentDistance < previousDistance )
				  inputJTextField.setBackground( Color.RED );
				  
			  else
				  inputJTextField.setBackground( Color.BLUE );
		  }
		  
		  if ( myGuess == randomNumber )
		  {
				   outputJLabel.setText( "Correct!" );
				  inputJTextField.setBackground( Color.BLUE );
				  inputJTextField.setEditable( true );
		  }		
		  else if ( myGuess < randomNumber )
			  outputJLabel.setText( "Too Low" );
		 else  
		 outputJLabel.setText( "Too High" );
		 
		  inputJTextField.selectAll();
		  previousDistance = currentDistance;
		  
		
	  } // end method actionPerformed
   } // end private inner class InputTextFieldHandler

// inner class for playagainbutton event handling
private class PlayAgainButtonHandler implements ActionListener
   {
	  // handle button event
	  public void actionPerformed( ActionEvent event )
	  {
		myGuess = 0;
		previousDistance = 0;
		currentDistance = (myGuess - randomNumber);
		
		
		 inputJTextField.setBackground( Color.WHITE );
		 outputJLabel.setText( "	" );
		 inputJTextField.setText( "" );
		 inputJTextField.setEditable( true );
		 inputJTextField.requestFocus();
		
	  } //end method actionPerformed
   } // end private inner class PlayAgainButtonHandler

}// end class TextFieldFrame



How do I delete a post and start over? I'd like to clean it up.

This post has been edited by macosxnerd101: 27 August 2012 - 06:52 AM
Reason for edit:: Removed excess code

Was This Post Helpful? 0
  • +
  • -

#4 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: I can't get this number guessing game to run. I get Not main metho

Posted 26 August 2012 - 10:29 PM

Okay, now, look through your code. You don't have a main() function. You need a main() function, whether in this code or elsewhere, in order to do anything because that marks the entry point for your Java application.

Once again, I don't know who you got your code from, but you can't just copy/paste code and magically expect it to work. You need to learn the language and understand how programming works.

This is the function declaration for a proper Main method in Java. Create one, give it a proper body (I'll be leaving that as an exercise for you) to create and use your Program6 class.

public static void main(String[] args)


Edit:
As a side note, I've fixed up the program to run for me, and I'm seeing:
1) It's not obvious that you must press Enter, rather than clicking a button for your guess.
2) You've got three JLabels and one JButton that are declared as member variables but aren't actually being used anywhere in your code.
3) Clicking the "Play Again" button doesn't actually change the number the user is trying to guess.
4) After creating your UI elements in your frame, you aren't doing anything to resize the frame. So when you launch it, it's just a window with the minimum size, and I have to manually resize it.

This post has been edited by Mina-no-Hime: 26 August 2012 - 10:49 PM

Was This Post Helpful? 1
  • +
  • -

#5 Porter51  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-August 12

Re: I can't get this number guessing game to run. I get Not main metho

Posted 26 August 2012 - 10:50 PM

Okay, Thank you! How do I delete this post? I would like to repost it a bit cleaner.
Was This Post Helpful? 0
  • +
  • -

#6 Porter51  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-August 12

Re: I can't get this number guessing game to run. I get Not main metho

Posted 26 August 2012 - 11:22 PM

I'm trying to figure out the correct main() method to use and where to place it exactly to get the window to display. Would it be something to effect of:

public static void main(String[] args) {
      ActionEvent.showMessageDialogue (null, "GuessingGame" );


Was This Post Helpful? 0
  • +
  • -

#7 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: I can't get this number guessing game to run. I get Not main metho

Posted 26 August 2012 - 11:27 PM

You wouldn't want to use ActionEvent to start it up (as far as I know - I'm not a huge fan of Java programming, so I don't know what the best-practice convention is).

Basically, what you want to do in the main function is (based on this source):

Instantiate a Program6 object
Set the close operation of the object (would be best to do this in the constructor)
Resize the instance with the pack() command (would also do this in the constructor, after you've created all of your UI elements)
Make the window visible (again, could be done in constructor).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1