Ayumiz
J-10 whore
+103|6703|Singapore
I got a couple of java homeworks i need help with, like helping me spot the error with the code.


Code:

public class SphereVolume 
{
    public static void main (String[] args);
            
        System.out.println ("Volume: " + volume + " cubic cm");
        
        radius = scn.nextDouble();     
        
        final double PI = 3.14159;
        
        volume = PI * radius * radius * radius ;

        System.out.print ("Enter radius(cm) of circle: ");
        
          Scanner scn = new Scanner(System.in);
          
            int radius;  
 
        int volume;    
 
    }
Whats the error here?


Code:

import java.util.Scanner;

public class Face
{
  public static void main(String[] args)
  {
     // declare variables
      String Peter;
      
          
     Scanner scn = new Scanner(System.in);
      System.out.print("Enter your name : ");
      Peter = scn.nextString();
         
          
     System.out.println("  //////////");
      System.out.println(" |  o   o  |");
      System.out.println("(|    ^    |)");
      System.out.println(" |  \\_/  |");
      System.out.println("  --------- ");
      System.out.println("Hello, I am Peter! How are you?");
  }
}
Any error here?

Last edited by Ayumiz (2007-05-10 06:26:21)

jsnipy
...
+3,276|6492|...

int radius; 

        int volume; 

are declared after you are referencing them
Ayumiz
J-10 whore
+103|6703|Singapore
uhh so i must declare them before system println right?
jsnipy
...
+3,276|6492|...

Ayumiz wrote:

uhh so i must declare them before system println right?
yes
Ayumiz
J-10 whore
+103|6703|Singapore
Ok edit: Whats wrong with the second code?
jsnipy
...
+3,276|6492|...

Assuming u are trying to take the name form the user ... you variable name is Peter ... which is taking the value from the user input.

Code:

// What you have
System.out.println("Hello, I am Peter! How are you?");

// Try this
System.out.println("Hello, I am " + Peter + " ! How are you?");
Perhaps use a different variable name to avoid confusion

Last edited by jsnipy (2007-05-10 06:49:17)

Ayumiz
J-10 whore
+103|6703|Singapore
uh okay, i'll try that out, theres some error with the first code,


I have edited it countless times

Code:

import java.util.Scanner;
public class Sphere 
{
    public static void main (String[] args);
     {
     
     
         //declare variables
          int radius;
          int volume;
          final double PI = 3.14159;  
 
         
          
          // read input data
          Scanner scn = new Scanner(System.in);
          volume = PI * radius * radius * radius;

          
            
 
            
        System.out.println ("Volume: " + volume +  "cubic cm");
                    radius = scn.nextDouble();
          System.out.print ("Enter radius(cm) of circle: ");
          }
          
          
}
The error message shown on my compiler is this,

Sphere.java:51: 'class' or 'interface' expected
    }
    ^
Sphere.java:52: 'class' or 'interface' expected
^
2 errors
vpyroman
Aeon Supreme commander
+16|6587|UCF
Its been a semester, but 

public static void main (String[] args);

seems to be highly illegal.

No ";" on the end of that line.

System.out.println ("Volume: " + volume +  "cubic cm");
                    radius = scn.nextDouble();

You're also reading in a double value into an int
its just easier to recode everything

import java.util.Scanner;
public class Sphere
{
    public static final double PI = 3.14159; 
    public static void main (String[] args) //edited here
     {
         
         //declare variables
          double radius;  //Edited here
          double volume; //edited here
          Scanner scn = new Scanner(System.in);

         
          // read input data
          System.out.print ("Enter radius(cm) of circle: ");
          radius = scn.nextDouble(); //Edited here

          volume = PI * radius * radius * radius;
          System.out.println ("Volume: " + volume +  "cubic cm");


          }
         
         
}
Also, constants shouldn't be declared in a method
:Example:

public class bmi {
    public static final double FOOT_TO_METER  = 0.3048;
    public static final double POUND_To_KILO  = 0.45359237;
    public static void main(String[] args) {}
}
And for the 2nd method:

Peter = scn.nextString();
should be

Peter = scn.readLine();

Last edited by vpyroman (2007-05-10 07:09:13)

Ayumiz
J-10 whore
+103|6703|Singapore
hmm done editing but still 2 errors though.
vpyroman
Aeon Supreme commander
+16|6587|UCF
Also, you're sig should be changed because its missing a  ]

[img]http://img112.imageshack.us/img112/6987/ellokh9.gif[/img  <---- Right here, a missing ]
Ayumiz
J-10 whore
+103|6703|Singapore
heya thanks. my code worked out

Board footer

Privacy Policy - © 2024 Jeff Minard