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

Thread: errors in java

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question errors in java

    My superclass
    public class NewComputer
    {
    // display the users new computer
    private String NewComputer; // computer of this NewComputer

    // constructor initializes myNewComputer with String argument
    public NewComputer( String name ) // constructor name is class name
    {
    myNewComputer = name; // initializes myNewComputer
    } // end constructor

    // method to set newComputer
    public void setNewComputer( String name )
    {
    myNewComputer = name; // store user computer
    } // end method setNewComputer

    // method to retrieve user computer
    public String getNewComputer()

    {
    return myNewComputer;
    } // end method getNewComputer

    // display computer to end user
    public void displayMessage()
    {
    // this statement calls getNewComputer to get the
    // new computer this NewComputer represents
    System.out.printf( " User new computer for\n%s!\n",
    getNewComputer() );
    } //end method displayMessage

    My main class
    public class GaylesComputer
    {
    //main method begins execution of Java application
    public GaylesComputer( String[] args )
    {
    // create Scanner to obtain input from command window
    Scanner input = new Scanner ( System.in );
    {
    // create a NewComputer object and assign it to myNewComputer
    NewComputer myNewComputer = new myNewComputer();
    myNewComputer.displayMessage( nameOfComputer ); // user input

    //description of current computer
    System.out.println( "Acer" );
    System.out.println("Windows 7 Home Premium 64-bit" );
    System.out.println();//outputs a blank line
    System.out.println( "20 inch HD" );
    System.out.println( "1TB Hard Drive" );
    System.out.println();//outputs a blank line
    System.out.println( "Intel Core i3 Processor 2120" );
    //calling myNewComputer displayMessage method
    //and pass nameOfComputer as an argument

    }
    }//end main method
    } //end class GaylesComputer

    I have errors on this my main class has 10 errors but my superclass has only one and therefore confused. Thanks for you help


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: errors in java

    When posting code, please use the highlight tags to preserve formatting.

    What exact errors are you getting? What exact line is each error on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,310
    My Mood
    Mellow
    Thanks
    253
    Thanked 780 Times in 765 Posts

    Default Re: errors in java

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    Too much unformatted code, posted improperly. Please review the link above and edit your post so that the code appears between code tags and is properly formatted. Thanks.

  4. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: errors in java

    I apologize and hope I'm following the instructions.

    //GaylesComputer.java
    //input computer 
    //creating a NewComputer object and invoke its myNewComputer method.
    //calling a Scanner.
     
    import java.util.Scanner; //program uses Scanner
     
    //declaring class for GaylesComputer
    public class GaylesComputer
    {
     //main method begins execution of Java application
     public  GaylesComputer( String[] args ) 
     {
       // create Scanner to obtain input from command window
       Scanner input = new Scanner ( System.in );
      {  
       // create a NewComputer object and assign it to myNewComputer
       //calling myNewComputer displayMessage method 
       NewComputer myNewComputer = new myNewComputer();
       //and pass nameOfComputer as an argument 
        myNewComputer.displayMessage( nameOfComputer ); // user input
     
       //description of current computer
       System.out.println( "Acer" );
       System.out.println( "Windows 7 Home Premium 64-bit" );
       System.out.println();//outputs a blank line
       System.out.println( "20 inch HD" );
       System.out.println( "1TB Hard Drive" );
       System.out.println();//outputs a blank line
       System.out.println( "Intel Core i3 Processor 2120" );
     
     
          } 
         } 
        }//end main method 
       } //end class GaylesComputer  -->line 36
      } // end of superclass

    and the line is 36 where the error is class, interface or enum expected

    my superclass with the main class
    // NewComputer.java
    // Class declaration with constructor.
    // class declaration for NewComputer
     
    public class NewComputer
    {
        // display the users new computer
    	private String NewComputer; // computer of this NewComputer
     
    	// constructor initializes myNewComputer with String argument
    	public NewComputer( String name ) // constructor name is class name
    	 {
    	   myNewComputer = name; // initializes myNewComputer
    	   } // end constructor
     
    	   // method to set newComputer
    	   public void setNewComputer( String name ) 
    	   { 
    	      myNewComputer = name; // store user computer
    	   } // end method setNewComputer
     
    		 // method to retrieve user computer
    		 public String getNewComputer()
     
    		 {
    		   return myNewComputer;
    		  } // end method getNewComputer
     
    		  // display computer to end user 
    		  public void displayMessage() 
    		  { 
    		    // this statement calls getNewComputer to get the
    			// new computer this NewComputer represents
    			System.out.printf( " User new computer for\n%s!\n", 
    			   getNewComputer() ); 
            } //end method displayMessage
     
      //declaring class for GaylesComputer
     public class GaylesComputer
    {
     //main method begins execution of Java application
     public GaylesComputer( String[] args )
     {
       // create Scanner to obtain input from command window
       Scanner input = new Scanner ( System.in );
      {  
       // create a NewComputer object and assign it to myNewComputer
       //calling myNewComputer displayMessage method 
       NewComputer myNewComputer = new myNewComputer();
       //and pass nameOfComputer as an argument 
       myNewComputer.displayMessage( nameOfComputer ); // user input 
     
       //description of current computer
       System.out.println( "Acer" ); 
       System.out.println("Windows 7 Home Premium 64-bit" );
       System.out.println();//outputs a blank line
       System.out.println( "20 inch HD" ); 
       System.out.println( "1TB Hard Drive" );
       System.out.println();//outputs a blank line
       System.out.println( "Intel Core i3 Processor 2120" );
     
     
     
    	   } 
          } 
         }//end main method 
        } //end class GaylesComputer  
       } // end of superclass

    Again the error is the same but on line 68 which is //end of superclass also says the same as the previous code submitted.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: errors in java

    Please properly line up the {}s and the indentation of the code so that it is easy to see how the { and } are paired.
    The error messages you are getting are often given when the {}s are incorrectly placed.

    NOTE: The name of the main method is: main. Your comments about the main method are next to what looks like contructors for the class.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: errors in java

    You've got a few problems here.

    First off, you need to fix your formatting, as it's hiding some extra curly brackets that you have.

    Then you need to get rid of the extra curly brackets. Use indentation to make sure the closing brackets match up with the opening brackets you think they do.

    When you get that fixed, you'll need to fix your naming problems. Is the name of the class NewComputer or is it myNewComputer? Is the name of the variable NewComputer or is it myNewComputer? You seem to switch between the two interchangably, which is causing a ton of errors.

    Then you'll have to fix errors caused by not passing in the correct arguments into your constructors and methods.

    Finally, you'll have to add some import statements.

    All of these errors makes it look like you've simply typed up your whole program and hoped for the best. That isn't how you should write code. You need to get in the habit of writing only a couple lines at a time, recompiling, and testing what you just wrote. Writing a hundred lines and THEN trying to fix your compiler errors is a recipe for a headache.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    GregBrannon (March 25th, 2014)

  8. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: errors in java

    The class is NewComputer and the "myNewComputer" is the method which incidentally is the constructor as one of you mentioned. Plus, I was following my textbook Deitel.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: errors in java

    I was following my textbook
    Perhaps you should look at another textbook. There is some confusion in the code you've posted between the main() method and a constructor.
    The posted code doesn't have a main() method which is needed to start its execution.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: errors in java

    Thank you Norm. I will definitely do so. Do you have any recommendations?

Similar Threads

  1. Errors In Java
    By SecTech in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 27th, 2013, 05:28 AM
  2. Java Program Help! Cant find errors
    By frenchsasha in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2013, 11:30 AM
  3. New to java, over 100 errors please help
    By Renhik in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 6th, 2012, 11:50 PM
  4. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  5. hi - Java swing errors
    By genlastudio in forum AWT / Java Swing
    Replies: 1
    Last Post: March 22nd, 2011, 06:49 PM

Tags for this Thread