12 Replies - 3590 Views - Last Post: 03 March 2011 - 01:42 PM Rate Topic: -----

#1 Menotomy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-December 09

Java code even or odd

Posted 02 March 2011 - 09:17 AM

I have this code and I am receiving an error of class, interface, or enum expected and would like some advice in which direction to proceed to remove the the errors.
/*
 * Stephen Boudreau
 * March 1,2011
 * print out even + odd numbers
 */

package even_odd;

/**
 *
 * @author Stephen Boudreau
 */
// <editor-fold defaultstate="collapsed" desc="find even odd">
public static void main(String[],boolean "if even else odd"  args){
{
    // </print out even or odd numbers>

  
    // find the even + odd numbers
    public static void main(String args[], boolean  "if even else odd"){
        // variable declarations
        int num1;           //  even number
        int num2;           //  odd number
        

        // create Scanner to capture input from console
        Scanner input = new Scanner(System.in);

        // get user input, num1 and num2 and num3
        System.out.print("enen number ");
        num1 = input.nextInt();
        System.out.print("odd number: ");
        num2 = input.nextInt();
       
        // Compare and determine the smallest number
        if (num1 > num2)
                even  = num2;
        if (num1  < num2)
                odd = num1;
        
        // find the even  + odd number
       even = (num1 + num2 ) /2;
       odd = (num1 + num2 ) /2;

        // Display average and the samllest
        System.out.printf("even");
        System.out.printf("odd");
       // run:

    }
}


This post has been edited by macosxnerd101: 02 March 2011 - 09:18 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Java code even or odd

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Java code even or odd

Posted 02 March 2011 - 09:22 AM

You cannot define a method in a method. This is illegal. Also, the main() method accepts one param: a String[]. You cannot name method params using String literals (anything between double quotes).
public static void main(String[] args){
      //code
}



Also, everything goes into a class, interface, or enum in Java. I'm guessing you haven't covered interfaces or enums at the moment, so for your purposes, everything goes into a class.
class MyClass{

     public static void main(String[] args){
           //code
     }
}


Was This Post Helpful? 0
  • +
  • -

#3 smattysamz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-February 11

Re: Java code even or odd

Posted 02 March 2011 - 11:55 AM

@ Menotomy: Well the programming structure is totally wrong and also the logic is wrong.no class definition and multiple main method.please share the question we will probably try to help you out. :)

This post has been edited by smattysamz: 02 March 2011 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Java code even or odd

Posted 02 March 2011 - 02:39 PM

Also have a look at the Java Tutorials section if you haven't already. There are some good beginner tutorials :)
Was This Post Helpful? 0
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 199
  • View blog
  • Posts: 1,683
  • Joined: 13-March 10

Re: Java code even or odd

Posted 02 March 2011 - 03:51 PM

Dude, get some good book and start from the beginning.
Was This Post Helpful? 0
  • +
  • -

#6 Menotomy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-December 09

Re: Java code even or odd

Posted 02 March 2011 - 05:58 PM

View Postsmattysamz, on 02 March 2011 - 11:55 AM, said:

@ Menotomy: Well the programming structure is totally wrong and also the logic is wrong.no class definition and multiple main method.please share the question we will probably try to help you out. :)

Can some one please describe in detail each word in this line of code. public static void main(String[] args)}
This will help me understand what everyone is talking about.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Java code even or odd

Posted 02 March 2011 - 05:59 PM

Programmist has a tutorial on the main() method you may want to check out.
Was This Post Helpful? 0
  • +
  • -

#8 Menotomy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-December 09

Re: Java code even or odd

Posted 02 March 2011 - 07:36 PM

View Postdarek9576, on 02 March 2011 - 03:51 PM, said:

Dude, get some good book and start from the beginning.

The book I have is a big problem and got lost trying to read the book.

View Postmacosxnerd101, on 02 March 2011 - 05:59 PM, said:

Programmist has a tutorial on the main() method you may want to check out.

This is the first time I have used this web site and have problems in understanding the structure.

View Postsmattysamz, on 02 March 2011 - 11:55 AM, said:

@ Menotomy: Well the programming structure is totally wrong and also the logic is wrong.no class definition and multiple main method.please share the question we will probably try to help you out. :)

Ok I have big problems and I could use some direction in how to solve the problem?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java code even or odd

Posted 02 March 2011 - 07:48 PM

Don't really understand your math (if they are understandable) but this should compile and give you a start

import java.util.*;
class MyClass {

    public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);

       System.out.print("Enter first num: ");
       int num1 = scan.nextInt();
       System.out.print("Enter second num: ");
       int num2 = scan.nextInt();

       System.out.println("Number1 is: " + num1 + " number2: " + num2);
   }
}


Put all that in a file named MyClass.java and at the console prompt:
> javac MyClass.java
> java MyClass

Happy coding
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Java code even or odd

Posted 02 March 2011 - 10:20 PM

View PostMenotomy, on 02 March 2011 - 09:36 PM, said:

View Postmacosxnerd101, on 02 March 2011 - 05:59 PM, said:

Programmist has a tutorial on the main() method you may want to check out.

This is the first time I have used this web site and have problems in understanding the structure.

Welcome to DIC! :)

What is so confusing??? I linked to the tutorial.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java code even or odd

Posted 02 March 2011 - 10:23 PM

Quote

The book I have is a big problem and got lost trying to read the book.

I guess that navigating in a forum is more complicated than reading a book without getting lost :)
Was This Post Helpful? 0
  • +
  • -

#12 smattysamz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-February 11

Re: Java code even or odd

Posted 03 March 2011 - 01:37 PM

View PostMenotomy, on 02 March 2011 - 05:58 PM, said:

View Postsmattysamz, on 02 March 2011 - 11:55 AM, said:

@ Menotomy: Well the programming structure is totally wrong and also the logic is wrong.no class definition and multiple main method.please share the question we will probably try to help you out. :)

Can some one please describe in detail each word in this line of code. public static void main(String[] args)}
This will help me understand what everyone is talking about.

public - The "public" keyword is an access specifier, which control the visibility of class members. private and default are the two other access specifier.

static - This keyword allows main( ) to be called without having to instantiate a particular instance of the class.static has some more logic. find it in any book. cant explain in 2 lines

void - this is the "return type" of the method.it informs that, that method will not return any value.

main - this specifies that this method will be the first method to call when the class executes. any integrated development environment (IDE) will first try to find this method.

(String[] args)- this declares a parameter named args, which is an array of instances of the class String.Objects of type
String store character strings. In this case, args receives any command-line arguments present when the program is executed. :)
Was This Post Helpful? 0
  • +
  • -

#13 smattysamz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-February 11

Re: Java code even or odd

Posted 03 March 2011 - 01:42 PM

View PostMenotomy, on 02 March 2011 - 07:36 PM, said:

View Postdarek9576, on 02 March 2011 - 03:51 PM, said:

Dude, get some good book and start from the beginning.

The book I have is a big problem and got lost trying to read the book.

View Postmacosxnerd101, on 02 March 2011 - 05:59 PM, said:

Programmist has a tutorial on the main() method you may want to check out.

This is the first time I have used this web site and have problems in understanding the structure.

View Postsmattysamz, on 02 March 2011 - 11:55 AM, said:

@ Menotomy: Well the programming structure is totally wrong and also the logic is wrong.no class definition and multiple main method.please share the question we will probably try to help you out. :)

Ok I have big problems and I could use some direction in how to solve the problem?

well start with reading a book and get some concept.you can refer complete reference by "Herbert Schildt", i have the e-book, if you want tell me i will mail you.that is the best book. n never underestimate the internet. n else we all are here to help you. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1