Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

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

Robert0201Robert0201 Member Posts: 7
I downloaded j2sdk1.4.2_05 for Java 2 and JCreator and for some programs it runs but for all other it says Exception in thread "main" java.lang.NoSuchMethodError: main. Here is a program that does work:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Muffins{

public static void main(String[] args) throws IOException
{
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any integer:");
String podString = keyboard.readLine();
int number = Integer.parseInt(podString);

System.out.println("Enter power raised to:");
String peaString = keyboard.readLine();
int power = Integer.parseInt(peaString);

int answer = (int)Math.pow(number,power);
double root = Math.sqrt(number);
int randomNumber = (int) (Math.random() * (number + 1));
double sine = Math.sin(number * Math.PI /180);
double cosine = Math.cos(number * Math.PI /180);
double tangent = Math.tan(number * Math.PI /180);

System.out.println();
System.out.println(number + " raised to power " + power + "= " + answer);
System.out.println("Square root of " + number + "= " + root);
System.out.println("Random integer between 1 and " + number + "= " + randomNumber);
System.out.println("Sine of " + number + " degrees= " + sine);
System.out.println("Cosine of " + number + " degrees= " + cosine);
System.out.println("Tangent of " + number + " degrees= " + tangent);
System.out.println();

System.out.println("Guess how many windows the White House has:");
String windowsString = keyboard.readLine();
int glass = Integer.parseInt(windowsString);

int winsoff = Math.abs(147 - glass);

System.out.println("The White House has 147 windows.");
System.out.println("You are " + winsoff + " off.");
System.out.println();
}

}
----------------------------------------------------------------------

Here is one that doesn't work:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class DateSecondTry {


private String month;
private int day;
private int year;

public void writeOutput() {

System.out.println(month + " " + day);
}

public void readInput() throws IOException {

BufferedReader keyboard;
keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter month, day, and year on three lines:");
month = keyboard.readLine();
day = Integer.parseInt(keyboard.readLine());
year = Integer.parseInt(keyboard.readLine());
}

public int getDay() {

return day;
}

public int getYear() {

return year;
}
public int getMonth() {

if (month.equals("Jan"))
return 1;
else if (month.equals("Feb"))
return 2;
else if (month.equals("Mar"))
return 3;
else if (month.equals("Apr"))
return 4;
else if (month.equals("May"))
return 5;
else if (month.equals("Jun"))
return 6;
else if (month.equals("Jul"))
return 7;
else if (month.equals("Aug"))
return 8;
else if (month.equals("Sep"))
return 9;
else if (month.equals("Oct"))
return 10;
else if (month.equals("Nov"))
return 11;
else if (month.equals("Dec"))
return 12;
else {

System.out.println("Fatal Error");
System.exit(0);
return 0;
}


}

}
-----------------------------------------------------------------------
Can someone please help me, I have no idea what to do.

Comments

  • VilanyeVilanye Member Posts: 684
    [b][red]This message was edited by Vilanye at 2004-8-27 1:40:6[/red][/b][hr]
    The first thing I see missing is a constructor.

    A constructor sets up all the class level variables/objects and does not have a return value. It should look something like this, depending if you want all the variable set to a value, otherwise pass nothing, and set them to null.

    public DateSecondTry(String month, int day, int year)
    {
    this.month = month;
    this.day = day;
    this.year = year;
    }

    You can have more then one constructor, but you have to have at least one.

    Another thing, every program you write(except applets and maybe a few other things) needs a main method. To get DateSecondTry.class, make a short driver file that creates an instance of DateSecondTry. Like:

    public class Whatever
    {

    public static void main(String[] args)
    {
    String name;
    int day, year;

    //read in the input, validate it
    ...
    DateSecondTry date = new DateSecondTry(name, day, year);
    ...//add code to test the class
    }
    }

    You will get 2 .class files after compiling. Or did you mean to put a main method in DateSecondTry? The code in method public void readInput()looks like it should be in the driver file or the constructor method in DateSecondTry. Personally I would put that code in main, then write a private static method to validate the input, then create an instance of DateSecondTry passing in the three values to the constructor.

    There may be a way to seperately compile classes into bytecode without a main method, through something like Eclipse, but I don't know if that is true, or how to do it.

    You might also want to consider using String.equalsIgnoreCase("Jan") method instead of String.equals(String), if there is no guarantee of good user data, otherwise jan will return as not equal. I don't know if you know exceptions but the Integer.parseInt() definately needs to catch NumberFormatException, unless you know the input will always be valid, otherwise your program can crash and burn during runtime if those methods try to process anything but a string of valid integers. This may not be something you need to worry about right now for this program but it is a good thing to remember.

    This piece of code is not needed and the compiler might just scream at you for it:

    System.exit(0);
    return 0;

    The return statement will never execute because the previous line cut your programs throat. For a more robust program, return the 0 and handle the problem.

    This link contains all the Java API classes and its methods and is invaluable for finding out what classes like String and Integer offer, there are also tutorials, but I found them to be lacking when I first started learning java.

    http://java.sun.com/j2se/1.4.2/docs/api/


    : I downloaded j2sdk1.4.2_05 for Java 2 and JCreator and for some programs it runs but for all other it says Exception in thread "main" java.lang.NoSuchMethodError: main. Here is a program that does work:
    :
    : import java.io.BufferedReader;
    : import java.io.InputStreamReader;
    : import java.io.IOException;
    :
    : public class Muffins{
    :
    : public static void main(String[] args) throws IOException
    : {
    : BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
    : System.out.println("Enter any integer:");
    : String podString = keyboard.readLine();
    : int number = Integer.parseInt(podString);
    :
    : System.out.println("Enter power raised to:");
    : String peaString = keyboard.readLine();
    : int power = Integer.parseInt(peaString);
    :
    : int answer = (int)Math.pow(number,power);
    : double root = Math.sqrt(number);
    : int randomNumber = (int) (Math.random() * (number + 1));
    : double sine = Math.sin(number * Math.PI /180);
    : double cosine = Math.cos(number * Math.PI /180);
    : double tangent = Math.tan(number * Math.PI /180);
    :
    : System.out.println();
    : System.out.println(number + " raised to power " + power + "= " + answer);
    : System.out.println("Square root of " + number + "= " + root);
    : System.out.println("Random integer between 1 and " + number + "= " + randomNumber);
    : System.out.println("Sine of " + number + " degrees= " + sine);
    : System.out.println("Cosine of " + number + " degrees= " + cosine);
    : System.out.println("Tangent of " + number + " degrees= " + tangent);
    : System.out.println();
    :
    : System.out.println("Guess how many windows the White House has:");
    : String windowsString = keyboard.readLine();
    : int glass = Integer.parseInt(windowsString);
    :
    : int winsoff = Math.abs(147 - glass);
    :
    : System.out.println("The White House has 147 windows.");
    : System.out.println("You are " + winsoff + " off.");
    : System.out.println();
    : }
    :
    : }
    : ----------------------------------------------------------------------
    :
    : Here is one that doesn't work:
    :
    : import java.io.BufferedReader;
    : import java.io.InputStreamReader;
    : import java.io.IOException;
    :
    :
    : public class DateSecondTry {
    :
    :
    : private String month;
    : private int day;
    : private int year;
    :
    : public void writeOutput() {
    :
    : System.out.println(month + " " + day);
    : }
    :
    : public void readInput() throws IOException {
    :
    : BufferedReader keyboard;
    : keyboard = new BufferedReader(new InputStreamReader(System.in));
    : System.out.println("Enter month, day, and year on three lines:");
    : month = keyboard.readLine();
    : day = Integer.parseInt(keyboard.readLine());
    : year = Integer.parseInt(keyboard.readLine());
    : }
    :
    : public int getDay() {
    :
    : return day;
    : }
    :
    : public int getYear() {
    :
    : return year;
    : }
    : public int getMonth() {
    :
    : if (month.equals("Jan"))
    : return 1;
    : else if (month.equals("Feb"))
    : return 2;
    : else if (month.equals("Mar"))
    : return 3;
    : else if (month.equals("Apr"))
    : return 4;
    : else if (month.equals("May"))
    : return 5;
    : else if (month.equals("Jun"))
    : return 6;
    : else if (month.equals("Jul"))
    : return 7;
    : else if (month.equals("Aug"))
    : return 8;
    : else if (month.equals("Sep"))
    : return 9;
    : else if (month.equals("Oct"))
    : return 10;
    : else if (month.equals("Nov"))
    : return 11;
    : else if (month.equals("Dec"))
    : return 12;
    : else {
    :
    : System.out.println("Fatal Error");
    : System.exit(0);
    : return 0;
    : }
    :
    :
    : }
    :
    : }
    : -----------------------------------------------------------------------
    : Can someone please help me, I have no idea what to do.
    :



  • Robert0201Robert0201 Member Posts: 7
    All of the code was straight out of a book.
  • VilanyeVilanye Member Posts: 684
    If the code really came from a book, I am speechless. But you still need a main method regardless. The code for it might be on a nearby page.

    : All of the code was straight out of a book.
    :

  • Robert0201Robert0201 Member Posts: 7
    How bout this. I got it straight from sparknotes.com and its giving me the same error.

    import java.applet.Applet;
    import java.awt.*;
    // this is required for JOptionPane
    import javax.swing.JOptionPane;

    public class tictactoetemplate extends Applet
    {
    // defines a simple 2d array to store which player has gone where
    private int[][] board;
    // defines the size of each square for the applet
    private int sizesquare = 50;

    // you can put code you want to run when the applet first starts in this method
    public void start()
    {
    // allow drawing...
    setVisible(true);
    // initialize the board...
    board = new int[3][3];
    // play the game
    playgame();
    }

    // return true if the position on the board has been claimed by a player
    // (it would have a non-zero value stored there if it is)
    // Make sure to NOT allow invalid coordinates (return true if x and y
    // are invalid values)
    private boolean istaken(int x, int y) {
    // *** PART 2
    // Replace the following line with code that works as specified in the comments above
    // ***
    return true;
    }

    // return true if all of the given coordinates are owned by the player
    // (ie: if the values at those array positions are the player's number)
    // note: haswon(1,0,0,1,1,2,2) is asking if player 1 has the positions (0,0) (1,1) and (2,2)
    // in the format where the first integer in each pair represents the X value, and the 2nd
    // represents the Y value
    private boolean haswon(int player, int x1, int y1, int x2, int y2, int x3, int y3) {
    // *** PART 3
    // Replace the following line with code that works as specified in the comments above
    // ***
    return false;
    }

    // return true if anyone has won the game yet, false otherwise.
    // You need to check each possible way to win at tic tac toe to see if either player has
    // won. (ie: check if they have a full row or column, or the diagonals).
    // Hint: there are a total of 8 ways a player can win... Also, you are expected to
    // use the above method (the other haswon) to help you here.
    private boolean haswon(int player) {
    // *** PART 11
    // Replace the following line with code that works as specified in the comments above
    // ***
    return false;
    }

    // return true if the board is full (ie: every single position is taken by either player1
    // or player2
    private boolean boardfull() {
    // *** PART 4
    // Replace the following line with code that works as specified in the comments above
    // ***
    return false;
    }

    // This is where the action is...
    private void playgame() {
    // variables you can use for the sake of reading in the coordinates from the players
    int xpos=-1;
    int ypos=-1;

    // *** PART 5
    // replace the condition of the while loop (what now says xpos > 0) with one which basically says that the while
    // loop will run as long as neither player has won the game, and as long as the board is not totally full
    // ***
    while (xpos > 0) {

    // *** PART 6
    // Write code to keep on prompting player 1 for an x and y coordinate as long as
    // the coordinates the player enters are invalid (less than 0 or greater than 2)
    // and as long as the spot they are asking for is already taken
    // Hint: use another method to help figure out if something is taken, and use JOptionPane
    // to read in the string from the user, and covert it over to an integer
    // Also, if you want the user to enter coordinates in the format where the upper left
    // is 1,1 you need to subtract 1 off from each number the player enters
    // ***

    // *** PART 7
    // At this point, we are out of that loop, and thus it should be safe to assign the
    // board position the player asked for to player1 (assign it 1)
    // Write the code to do this, and then write more code to force Java to redraw the screen
    // so we can see the update on the new spot that has been grabbed
    // ***

    // *** PART 8
    // If player 1 did not just win and there is still space on the board, you need to do
    // the exact same thing as you just did above for player 2. So figure out of player1
    // just won, if player1 did not, copy the above 2 sections modifying them so they
    // work for player 2
    // ***
    }

    // *** PART 9
    // At this point, someone either won the game or the board is full. Find out if player 1 won the
    // game, if so display some sort of message saying as much, and then figure out if player 2 won
    // and if so display some sort of message saying as much, and otherwise, the board must be full
    // and put up another message in that case
    // ***
    }

    // draw a single box (including if it's taken or not)
    public void drawbox(int x, int y, Graphics2D g) {
    g.setColor(Color.black);
    // draw the box for the square
    g.drawRect(x*sizesquare,y*sizesquare,sizesquare,sizesquare);

    // *** PART 10
    // You need to fill in code here to figure out if either player has this box
    // and if so, to fill in the box in a way to distinguish the box as being
    // owned by one of the two players. You can decide to use whatever
    // design you wish to to represent player 1 & which to represent player2
    // ***
    }

    // this is called by Java every time the window needs to be drawn or redrawn
    public void paint(Graphics g)
    {
    Graphics2D page = (Graphics2D) g;
    // draw the board
    page.setColor(Color.white);
    page.fillRect(0, 0, sizesquare*3, sizesquare*3);
    // now draw each individual box by calling drawbox for each possible position
    for (int a = 0; a < 3; a++) {
    for (int b = 0; b < 3; b++) {
    drawbox(a,b,page);
    }
    }
    // *** PART 1
    // Include any other messages you want too
    // ***
    page.drawString("Tic Tac Toe Game",0,sizesquare*3+20);
    }
    }
  • Robert0201Robert0201 Member Posts: 7
    maybe i didn't download everything i need. what all should i have?
  • VilanyeVilanye Member Posts: 684
    Did that applet you post compile, or did you try to run it on the command line? Applets have to be run in a web browser or in the applet viewer.

    If it didn't compile go to the command line and type javac. If it doesn't recognize that command, then you need to make sure you have set your classpath properly. How you do that depends on what OS. Go to the developer section at www.java.com and you can get information on how to set up your java enviroment, along with instructions on how to compile and run applications and applets.

    If you get the following, then it is set up correctly and you are good to go and your issue is what I have mentioned previously. Whether you installed J2SE or J2EE, it doesn't matter.


    C:ProjectsNetwork>javac
    Usage: javac
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are us
    ed
    -classpath Specify where to find user class files
    -sourcepath Specify where to find input source files
    -bootclasspath Override location of bootstrap class files
    -extdirs Override location of installed extensions
    -d Specify where to place generated class files
    -encoding Specify character encoding used by source files
    -source Provide source compatibility with specified release
    -target Generate class files for specific VM version
    -help Print a synopsis of standard options



    : maybe i didn't download everything i need. what all should i have?
    :

  • Robert0201Robert0201 Member Posts: 7
    yeh thats what i got so javac works
  • Robert0201Robert0201 Member Posts: 7
    this java sample program gives the same error

    /*
    * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
    *
    * -Redistributions of source code must retain the above copyright
    * notice, this list of conditions and the following disclaimer.
    *
    * -Redistribution in binary form must reproduct the above copyright
    * notice, this list of conditions and the following disclaimer in
    * the documentation and/or other materials provided with the distribution.
    *
    * Neither the name of Sun Microsystems, Inc. or the names of contributors
    * may be used to endorse or promote products derived from this software
    * without specific prior written permission.
    *
    * This software is provided "AS IS," without a warranty of any kind. ALL
    * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
    * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
    * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
    * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
    * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
    * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
    * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
    * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
    * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
    * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
    *
    * You acknowledge that Software is not designed, licensed or intended for
    * use in the design, construction, operation or maintenance of any nuclear
    * facility.
    */

    /*
    * @(#)AquaTheme.java 1.7 03/01/23
    */


    import javax.swing.plaf.*;
    import javax.swing.plaf.metal.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;

    /**
    * This class describes a theme using "blue-green" colors.
    *
    * 1.7 01/23/03
    * @author Steve Wilson
    */
    public class AquaTheme extends DefaultMetalTheme {

    public String getName() { return "Aqua"; }

    private final ColorUIResource primary1 = new ColorUIResource(102, 153, 153);
    private final ColorUIResource primary2 = new ColorUIResource(128, 192, 192);
    private final ColorUIResource primary3 = new ColorUIResource(159, 235, 235);

    protected ColorUIResource getPrimary1() { return primary1; }
    protected ColorUIResource getPrimary2() { return primary2; }
    protected ColorUIResource getPrimary3() { return primary3; }

    }

  • VilanyeVilanye Member Posts: 684
    [b][red]This message was edited by Vilanye at 2004-8-28 16:20:21[/red][/b][hr]
    Well of course it doesn't compile, it has no main method. That is a class you might want add to a GUI program, it is not a standalone program.

    All java applications with a few exceptions(applets, servlets, ect) require a class with a main method. The reason applets and servlets don't is that they inherit from another class so main is already predefined for those types of applications, IIRC.

    I did make an error in a previous response, classes(other then main) do not have to have an explicit constructor, it you fail to include all class level variable will be set to their default values( int =0 char = '0' object = null).




    : this java sample program gives the same error
    :
    : /*
    : * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
    : *
    : * Redistribution and use in source and binary forms, with or without
    : * modification, are permitted provided that the following conditions
    : * are met:
    : *
    : * -Redistributions of source code must retain the above copyright
    : * notice, this list of conditions and the following disclaimer.
    : *
    : * -Redistribution in binary form must reproduct the above copyright
    : * notice, this list of conditions and the following disclaimer in
    : * the documentation and/or other materials provided with the distribution.
    : *
    : * Neither the name of Sun Microsystems, Inc. or the names of contributors
    : * may be used to endorse or promote products derived from this software
    : * without specific prior written permission.
    : *
    : * This software is provided "AS IS," without a warranty of any kind. ALL
    : * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
    : * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
    : * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
    : * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
    : * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
    : * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
    : * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
    : * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
    : * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
    : * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
    : *
    : * You acknowledge that Software is not designed, licensed or intended for
    : * use in the design, construction, operation or maintenance of any nuclear
    : * facility.
    : */
    :
    : /*
    : * @(#)AquaTheme.java 1.7 03/01/23
    : */
    :
    :
    : import javax.swing.plaf.*;
    : import javax.swing.plaf.metal.*;
    : import javax.swing.*;
    : import javax.swing.border.*;
    : import java.awt.*;
    :
    : /**
    : * This class describes a theme using "blue-green" colors.
    : *
    : * 1.7 01/23/03
    : * @author Steve Wilson
    : */
    : public class AquaTheme extends DefaultMetalTheme {
    :
    : public String getName() { return "Aqua"; }
    :
    : private final ColorUIResource primary1 = new ColorUIResource(102, 153, 153);
    : private final ColorUIResource primary2 = new ColorUIResource(128, 192, 192);
    : private final ColorUIResource primary3 = new ColorUIResource(159, 235, 235);
    :
    : protected ColorUIResource getPrimary1() { return primary1; }
    : protected ColorUIResource getPrimary2() { return primary2; }
    : protected ColorUIResource getPrimary3() { return primary3; }
    :
    : }
    :
    :



  • Robert0201Robert0201 Member Posts: 7
    i have no clue what youre talkin about, i dont know much java. what do i have to do to get these programs workin. they work at school but not at my home computer.
Sign In or Register to comment.