Help, still trying to get the basics of Java
OK, I'm still trying to figure out the basics and on this program decided to write small amounts of code at a time and compile them so I could learn where I was making the mistakes. I keep getting stuckI am getting 5 cannot find symbols on the following code. I've made sure I have imported the Scanner class, checked my brackets, but can't figure out why it is still giving these errors. Can you please help? Why will it not recognize "input"?
import java.util.Scanner;
publicclass Inventory
{
publicstaticvoid main( String args[] )
{
String productNum ="";
String name ="";
int units = 0;
double amount = 0;
Scanner scan =new Scanner( System.in );
System.out.println ("Please enter product #" );
productNum = input.next();
System.out.println ("Please enter color name" );
name = input.next();
System.out.println ("Please enter units on hand" );
units = input.nextInt();
System.out.println ("Please enter dollar amount for each unit" );
amount = input.nextDouble();
System.out.println (productNum, name, units, amount);
}
}
You haven't declared a variable called input! You have declared one called scan though.
Also, the println method accepts a single parameter, not a comma separated list.
Instead of that non-working println, try [url= http://java.sun.com/javase/6/docs/api/java/io/PrintStream.html#format(java.lang.String,%20java.lang.Object...)]format[/url].
Da-ding. And the light comes on. Ok the input thing was it. You're the bomb!!!! Thanks.
Ok, I am just using this line to test my input, but I like this feature. How do I use it properly though? I put this in as System.out.format(String productNum, String name, int units, double amount); and it several errors...I first typed it in without the types and it simply printed off the first variable...where am I going wrong?
Denise
TryString outputStr = String.format(
"%s %s %d %.2f",
productNum, name, units, amount);
System.out.println(outputStr);
// or, the equivalent
System.out.printf("%s %s %d %.2f%n", productNum, name, units, amount);
Check the String API documentation for format() and the java.util.Formatter
documentation for how the format strings work. They take a bit of getting used to.
OK, I have 2 classes. In my second class, I have created an object. I want to use a method call to an object from my first class that will add productNum, name, units and amount) . Would this be the correct statement...(Sorry, I do not know how to test variables in second class when I can't get the main class to run).
AlbumInventory.addAlbum(productNum, name, units, amount);
I then want to print a list using the toString report from the second class. I am a little confused...how do I call the toString back into my first class(that has the main).
Denise Comer
Thanks
With the above, I am getting this error, can anyone tell me why?
Inventory.java:15: cannot find symbol
symbol : class Albuminventory
location: class Inventory
Albuminventory scrapbook = new AlbumInventory ();
I am trying to create this to send a report back to with
> Albuminventory scrapbook = new AlbumInventory ();
It looks like Albuminventory should be AlbumInventory.
The description of your problem is not all that clear. Some code would
help.
It's a good idea to do one small thing at a time, making sure the
program compiles at each step. What you are doing with each class,
and with each method of each class should be documented in the
code.
Doesn't it not mean that your AlbumInventory class does not define a constructor that takes zero arguments?
e.g
public class AlbumInventory {
public static void main(String [] args) {
AlbumInventory brokenInventory = new AlbumInventory(); // This will produce compile error
AlbumInventory workingInventory = new AlbumInventory(5098) // This will compile
}
private int id;
public AlbumInventory (int id) {
this.id = id;
}
}
> Doesn't it not mean that your AlbumInventory class does not define a > constructor that takes zero arguments?I would have thought it means that the symboladdinventory^|i not Icannot be found.