7 Replies Latest reply: Feb 3, 2008 7:14 PM by DrClap RSS

    1 error -each class has 0 errors when run as separate file

    807601
      This application compiled the first time I ran it. and restarted it. I opened the application and got 11 errors. I removed the "import
      java.util.Scanner; // program uses class Scanner" statement from the "Comparison" and "Division" sections and I'm still getting "1" error as indicated here:

      // Calculation Project
      // Addition, Multiplication, Difference And Division.java
      /* Calculation program that displays the sum, product,
      difference and quotient of two numbers */
      // Created: Clarence Winfrey on January 31, 2008

      import java.util.Scanner; // program uses class Scanner

      public class Addition
      {
      // main method begins execution of Java application
      public static void main( String args[] )
      {
      // create Scanner to obtain input from command window
      Scanner input = new Scanner( System.in );

      int number1; // first number to add
      int number2; // second number to add
      int sum; // sum of number1 and number2

      System.out.print( "Enter first integer: " ); // prompt
      number1 = input.nextInt(); // read first number from user

      System.out.print( "Enter second integer: " ); // prompt
      number2 = input.nextInt(); // read second number from user

      sum = number1 + number2; // add numbers

      System.out.printf( "Sum is %d\n", sum ); // display sum

      } // end method main

      } // end class Addition

      // Calculate the product of three integers.



      public class Product
      {
      public static void main( String args[] )
      {
      // create Scanner to obtain input from command window
      Scanner input = new Scanner( System.in );

      int x; // first number input by user
      int y; // second number input by user
      int z; // third number input by user
      int result; // product of numbers

      System.out.print( "Enter first integer: " ); // prompt for input
      x = input.nextInt(); // read first integer

      System.out.print( "Enter second integer: " ); // prompt for input
      y = input.nextInt(); // read second integer

      System.out.print( "Enter third integer: " ); // prompt for input
      z = input.nextInt(); // read third integer

      result = x * y * z; // calculate product of numbers

      System.out.printf( "Product is %d\n", result );

      } // end method main

      } // end class Product

      // Compare integers using if statements, relational operators
      // and equality operators.


      public class Comparison
      {
      // main method begins execution of Java application
      public static void main( String args[] )
      {
      // create Scanner to obtain input from command window
      Scanner input = new Scanner( System.in );

      int number1; // first number to compare
      int number2; // second number to compare

      System.out.print( "Enter first integer: " ); // prompt
      number1 = input.nextInt(); // read first number from user

      System.out.print( "Enter second integer: " ); // prompt
      number2 = input.nextInt(); // read second number from user

      if ( number1 == number2 )
      System.out.printf( "%d == %d\n", number1, number2 );

      if ( number1 != number2 )
      System.out.printf( "%d != %d\n", number1, number2 );

      if ( number1 < number2 )
      System.out.printf( "%d < %d\n", number1, number2 );

      if ( number1 > number2 )
      System.out.printf( "%d > %d\n", number1, number2 );

      if ( number1 <= number2 )
      System.out.printf( "%d <= %d\n", number1, number2 );

      if ( number1 >= number2 )
      System.out.printf( "%d >= %d\n", number1, number2 );

      } // end method main

      } // end class Comparison

      // Calculate the quotient of two integers.


      import java.util.Scanner; // program uses class Scanner

      public class Division
      {
      // main method begins execution of Java application
      public static void main( String args[] )
      {
      // create Scanner to obtain input from command window
      Scanner input = new Scanner( System.in );

      int x; // first number input by user
      int y; // second number input by user

      int result; // quotient of numbers

      System.out.print( "Enter first integer: " ); // prompt for input
      x = input.nextInt(); // read first integer

      System.out.print( "Enter second integer: " ); // prompt for input
      y = input.nextInt(); // read second integer

      result = x / y ; // calculate quotient of numbers

      System.out.printf( "Quotient is %d\n", result );

      } // end method main

      } // end class Division

      **Build Output:**Configuration: Calculation Project - JDK version 1.6.0 <Default> - <Default>------------------
      E:\JAVA\Calculation Project\Calculation.java:114: class, interface, or enum expected
      import java.util.Scanner; // program uses class Scanner
      ^
      1 error

      Process completed.
        • 1. Re: 1 error -each class has 0 errors when run as separate file
          800282
          Are all classes in separate files?
          class Addition in Addition.java
          class Product in Product.java
          etc.
          • 2. Re: 1 error -each class has 0 errors when run as separate file
            807601
            Classes are in separate files when they run correctly but as indicated in the post. they are in the same file when I get the error and interestingly I get onlty one error that is apparantly occuring at the end of the addition section or beginning of the multiplication section. There seem to be no problems with the Comparison or Division classes. as indicated here
            // Calculation Project
            // Addition, Multiplication, Difference And Division.java
            /* Calculation program that displays the sum, product, 
               difference and quotient of two numbers */
            // Created: Clarence Winfrey on January 31, 2008
            
            import java.util.Scanner; // program uses class Scanner
            
            public class Addition 
            {
               // main method begins execution of Java application
               public static void main( String args[] )
               {
                  // create Scanner to obtain input from command window
                  Scanner input = new Scanner( System.in );
            
                  int number1; // first number to add
                  int number2; // second number to add
                  int sum; // sum of number1 and number2
            
                  System.out.print( "Enter first integer: " ); // prompt 
                  number1 = input.nextInt(); // read first number from user
            
                  System.out.print( "Enter second integer: " ); // prompt 
                  number2 = input.nextInt(); // read second number from user
            
                  sum = number1 + number2; // add numbers
            
                  System.out.printf( "Sum is %d\n", sum ); // display sum
            
               } // end method main
            
            } // end class Addition
            
            // Calculate the product of three integers.
            
            
            
            public class Product 
            {
               public static void main( String args[] )
               {
                  // create Scanner to obtain input from command window
                  Scanner input = new Scanner( System.in );
            
                  int x; // first number input by user
                  int y; // second number input by user
                  int z; // third number input by user
                  int result; // product of numbers
            
                  System.out.print( "Enter first integer: " ); // prompt for input
                  x = input.nextInt(); // read first integer
            
                  System.out.print( "Enter second integer: " ); // prompt for input
                  y = input.nextInt(); // read second integer
                  
                  System.out.print( "Enter third integer: " ); // prompt for input
                  z = input.nextInt(); // read third integer
            
                  result = x * y * z; // calculate product of numbers
            
                  System.out.printf( "Product is %d\n", result );
            
               } // end method main
            
            } // end class Product
            
            // Compare integers using if statements, relational operators 
            // and equality operators.
            
            
            public class Comparison 
            {
               // main method begins execution of Java application
               public static void main( String args[] )
               {
                  // create Scanner to obtain input from command window
                  Scanner input = new Scanner( System.in );
            
                  int number1; // first number to compare
                  int number2; // second number to compare
            
                  System.out.print( "Enter first integer: " ); // prompt 
                  number1 = input.nextInt(); // read first number from user 
            
                  System.out.print( "Enter second integer: " ); // prompt 
                  number2 = input.nextInt(); // read second number from user 
                  
                  if ( number1 == number2 ) 
                     System.out.printf( "%d == %d\n", number1, number2 );
            
                  if ( number1 != number2 )
                     System.out.printf( "%d != %d\n", number1, number2 );
            
                  if ( number1 < number2 )
                     System.out.printf( "%d < %d\n", number1, number2 );
            
                  if ( number1 > number2 )
                     System.out.printf( "%d > %d\n", number1, number2 );
            
                  if ( number1 <= number2 )
                     System.out.printf( "%d <= %d\n", number1, number2 );
            
                  if ( number1 >= number2 )
                     System.out.printf( "%d >= %d\n", number1, number2 );
             
               } // end method main
            
            } // end class Comparison 
            
             // Calculate the quotient of two integers.
             
             
             import java.util.Scanner; // program uses class Scanner
                 
             public class Division 
            {
               // main method begins execution of Java application
               public static void main( String args[] )
               {
                  // create Scanner to obtain input from command window
                  Scanner input = new Scanner( System.in );
            
                  int x; // first number input by user
                  int y; // second number input by user
                 
                  int result; // quotient of numbers
                  
                  System.out.print( "Enter first integer: " ); // prompt for input
                  x = input.nextInt(); // read first integer
            
                  System.out.print( "Enter second integer: " ); // prompt for input
                  y = input.nextInt(); // read second integer
            
                  result = x / y ; // calculate quotient of numbers
                  
                  System.out.printf( "Quotient is %d\n", result );
                  
                   } // end method main
            
            } // end class Division
            • 3. Re: 1 error -each class has 0 errors when run as separate file
              800282
              tenec wrote:
              Classes are in separate files when they run correctly but as indicated in the post. they are in the same file when I get the error and interestingly I get onlty one error that is apparantly occuring at the end of the addition section or beginning of the multiplication section. There seem to be no problems with the Comparison or Division classes. as indicated here
              What seems to be the problem then? Just don't stuff more than one class in one source file.
              • 4. Re: 1 error -each class has 0 errors when run as separate file
                807601
                sorry if I seem slow--- but after trying your suggestion I got this error when I tried to compile:

                "E:\JAVA\Product Comparison Division.java:114: class, interface, or enum expected
                import java.util.Scanner; // program uses class Scanner"

                Any thoughts or suggestions??
                • 5. Re: 1 error -each class has 0 errors when run as separate file
                  807601
                  It's not a complicated rule:

                  You can't put more than a single public, top-level class into a .java-File.

                  The standard says that each public top-level class needs to be in a .java file with a file name that corresponds to the class name (without the package).

                  So the class foo.Bar needs to be defined in a file called "Bar.java" (placed in a directory called "foo")
                  and the class foo.Quux needs to be defined in a file called "Quux.java" (placed in a directory called "foo").

                  Now that rule effectively makes it impossible to put foo.Bar and foo.Quux into the same .java file (as long as they are public top-level classes).

                  Why do you want to put them into the same file?
                  • 6. Re: 1 error -each class has 0 errors when run as separate file
                    807601
                    I took your advice and made some progress. I now have some other errors:

                    E:\JAVA\Calculation Project\src\Calculation Project.java:7: '{' expected
                    public class Calculation Project {
                    ^
                    E:\JAVA\Calculation Project\src\Calculation Project.java:11: ';' expected
                    Calculation ProjectFrame frame = new Calculation ProjectFrame();
                    ^
                    E:\JAVA\Calculation Project\src\Calculation Project.java:11: '(' or '[' expected
                    Calculation ProjectFrame frame = new Calculation ProjectFrame();
                    ^
                    E:\JAVA\Calculation Project\src\Calculation ProjectFrame.java:10: '{' expected
                    public class Calculation ProjectFrame extends Frame {
                    ^
                    E:\JAVA\Calculation Project\src\Calculation ProjectFrame.java:29: ';' expected
                    Calculation ProjectFrame.this.windowClosed();
                    ^
                    E:\JAVA\Calculation Project\src\Calculation ProjectFrame.java:45: ';' expected
                    Calculation ProjectFrame.this.windowClosed();
                    ^
                    6 errors
                    // Calculation Project
                    // Addition, Multiplication, Difference And Division.java
                    /* Calculation program that displays the sum, product, 
                       difference and quotient of two numbers */
                    // Created: Clarence Winfrey on January 31, 2008
                    
                    import java.util.Scanner; // program uses class Scanner
                    
                    public class Addition 
                    
                    {
                       // main method begins execution of Java application
                       public static void main( String args[] )
                       {
                          // create Scanner to obtain input from command window
                          Scanner input = new Scanner( System.in );
                    
                          int number1; // first number to add
                          int number2; // second number to add
                          int sum; // sum of number1 and number2
                    
                          System.out.print( "Enter first integer: " ); // prompt 
                          number1 = input.nextInt(); // read first number from user
                    
                          System.out.print( "Enter second integer: " ); // prompt 
                          number2 = input.nextInt(); // read second number from user
                    
                          sum = number1 + number2; // add numbers
                    
                          System.out.printf( "Sum is %d\n", sum ); // display sum
                    
                       } // end method main
                    
                    } // end class Addition
                    I'm not sure what the error messages mean.
                    • 7. Re: 1 error -each class has 0 errors when run as separate file
                      DrClap
                      If you look closely, you will see that the code in those error messages is nowhere to be seen in the code that you posted. So your first step would be to find the right code to look at.