• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java program question

Zap Brannigan

Golden Member
So I'm prgramming for the first time and have hit a slight bump in the road. I have this error popping up,

Class product is public, should be declared a file named Product.java. It's on line 5. Here is what I have so far and am trying ot figure out how to fix this so that it will compile.

// Ex.2.5: Product.java
// Calculate the product of three integers.
import java.util.Scanner; // program uses Scanner

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

int x; // 10
int y; // 20
int z; // 30
int result; // product of numbers

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

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

System.out.print("30;"); // 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


 
by chance on your file system is it named Product.txt or something? make sure it's in fact named "Product.java"
 
Originally posted by: Zap Brannigan
So I'm prgramming for the first time and have hit a slight bump in the road. I have this error popping up,

Class product is public, should be declared a file named Product.java. It's on line 5. Here is what I have so far and am trying ot figure out how to fix this so that it will compile.

// Ex.2.5: Product.java
// Calculate the product of three integers.
import java.util.Scanner; // program uses Scanner

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

int x; // 10
int y; // 20
int z; // 30
int result; // product of numbers

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

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

System.out.print("30;"); // 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

it compiles fine on my system.
Make sure the file is named exactly "Product.java" the case matters, so "product.java" will fail to compile.

 
Ok, now what is supposed it happen when all goes well? Is'nt it supposed to = 6000?

Still having trouble getting it to do what it is supposed to.
 
Originally posted by: Zap Brannigan
Ok, now what is supposed it happen when all goes well? Is'nt it supposed to = 6000?

Still having trouble getting it to do what it is supposed to.

It should prompt you for three integers. You input the integers and it outputs the product of those numbers.
 
After you compile it you have execute the .class file. In the command prompt it would be C:\java Product.
 
Does on this machine. Since you're using the scanner, you need to physically type in 10 at the first prompt, 20 at the second, etc. Are you trying to shortcut it, by writing to system.out and hoping those get pulled in to system.in? It won't work quite as you expect.
 
If what you posted is *exactly* what your program is, remove the "<------this is line 5 " from the end of line five. Otherwise, what are you using to edit the file? Notepad? Maybe the file is Product.java.txt, make sure that you tell Windows NOT to hide suffixes of known file types (it's in one of the folder menus).
 
I went in and renamed my project to Product , then added in everything and compiled and it is showing zero errors.

Should I now execute my program and see what happens?
 
Originally posted by: Zap Brannigan
I went in and renamed my project to Product , then added in everything and compiled and it is showing zero errors.

Should I now execute my program and see what happens?

yes
 
if it is not letting you execute, at the command prompt enter this:

SET classpath=

then run ur program:

java Product
 
Well I did pretty well considering I've yet to do any substantial reading on Java. I had other homework to do so I decided not to spend to much time on it this time. Urge is slowly building too tear through the book and absorb as much as possbile though lol. Probably later in the week.

This message brought to you by Starbuck - hell ya!

 
For future reference, make your prompts more meaningful. Also, when doing the import to have access to the scanner class, you just need "import java.util.*;" You dont need the .Scanner to have access to that class, you just need to import the java.util library as a whole.
 
Originally posted by: pcnerd37
For future reference, make your prompts more meaningful. Also, when doing the import to have access to the scanner class, you just need "import java.util.*;" You dont need the .Scanner to have access to that class, you just need to import the java.util library as a whole.

well, there's nothing wrong w/ importing just java.util.Scaner, and for a beginner it's more important they they see exactly waht they are importing so that they don't think the 'Scanner' class is 'just' there.
 
Originally posted by: statik213
Originally posted by: pcnerd37
For future reference, make your prompts more meaningful. Also, when doing the import to have access to the scanner class, you just need "import java.util.*;" You dont need the .Scanner to have access to that class, you just need to import the java.util library as a whole.

well, there's nothing wrong w/ importing just java.util.Scaner, and for a beginner it's more important they they see exactly waht they are importing so that they don't think the 'Scanner' class is 'just' there.


QFT. Almost all of my basic computer science labs simply imported general libraires, and I had no clue as to WTH I was doing for a few weeks. However, I never even knew the Scanner class existed, and I got a 5 in AP Computer Science. Go figure.

Zap Brannigan: I know ur a java newb but learn the JOptionPane and SpringLayout classes as soon as you get to GUI programming. Most courses don't directly teach you either of these, and they are VERY useful classes to know.
 
Originally posted by: scottish144
Originally posted by: statik213
Originally posted by: pcnerd37
For future reference, make your prompts more meaningful. Also, when doing the import to have access to the scanner class, you just need "import java.util.*;" You dont need the .Scanner to have access to that class, you just need to import the java.util library as a whole.

well, there's nothing wrong w/ importing just java.util.Scaner, and for a beginner it's more important they they see exactly waht they are importing so that they don't think the 'Scanner' class is 'just' there.


QFT. Almost all of my basic computer science labs simply imported general libraires, and I had no clue as to WTH I was doing for a few weeks. However, I never even knew the Scanner class existed, and I got a 5 in AP Computer Science. Go figure.

Zap Brannigan: I know ur a java newb but learn the JOptionPane and SpringLayout classes as soon as you get to GUI programming. Most courses don't directly teach you either of these, and they are VERY useful classes to know.

That's generally the approach I take. The * import, to me, clutters up the namespace. When I specifically import a class, I can look at the import list and see exactly what I'm using within my application.

I started with Java 1.4.2, so the Scanner is new to me. I'd end up with some self-written classes of similar functionality, used in each new project.

Once you have the fundamentals down of using the command line to run and compile your programs, I'd suggest you [op] look into an IDE. Eclipse is great, and Netbeans has a fairly good GUI Builder.
 
Back
Top