I understand arrays pretty well but I am so stuck on where to put it. Everything I enter into the inventory program seems to be in the wrong place or written the wrong way. I am so confused can someone help me? This is the program I have so far, I know how to use arrays but where do I put it? The array has to display the information 1 item at a time including it's number, name, price, inventory and amount in stock. My dad says I have a lot wrong with what I've got anyways.

//Fig.2.7:Inventory.java
//Inventory program

public class Inventory
{
	//main method begins execution of Java application
public static void main(String[] args) {
	
		//create User to obtain input
		Scanner input = new Scanner; java.util; ( System.in );
public class DataVideoDisk
{ 

	name1 String productName;
	int number1 String productNumber;
	int number2 String productStockAmount;
	int number3 String productPrice;
	int amount String stockValue;

} //end class Data video disk

	name1 // product name
	int  number1; // product number
	int number2; // product stock amount
	int number3; // product price
	int amount; // stock value; amount of number2 multiplied by number3
public class While
{
	System.out.print( “Enter product name: ” ); //prompt
	( name1 = input.nextName1(); read name from user)

	System.out.print( “Product number: ###-##-###: “ ); //prompt
	number1 = outputNextInt(); display number1)

	System.out.print( “Product stock amount: “ ); //prompt
	number2 = outputNextInt(); display product stock amount)

	System.out.print( “Product price: $##.##: “ ); //prompt
	number3 = outputNextInt(); display product price)

	amount = number2 * number3; // multiply numbers
	System.out.print( “Stock value is $d/n “ , amount); display amount)
}
 // end class Inventory

Recommended Answers

All 2 Replies

//Fig.2.7:Inventory.java
//Inventory program

public class Inventory
{
	//main method begins execution of Java application
public static void main(String[] args) {
	
 //You need to create a Scanner object. Below, we create a Scanner object called 'input'.
 //We use System.in because we want to read input from the keyboard.
		Scanner input = new Scanner(System.in);

       //To declare the variables below, we do NOT need a new class. 
       //We can just declare them in this method.
       //Each declaration should only have one type, as I edited below.
	String productName;
	int productNumber;
	int productStockAmount;
	int productPrice;
	int stockValue;

        while(true){
	System.out.print( “Enter product name: ” ); //prompt
	productName = input.nextLine(); // gets the line of input the user typed

        //Note: this code is wrong: the format ###-##-### is NOT an integer
        //therefore, you can't save it into an integer variable! number1 is an 
        // integer variable, and this code will not work. You can, however,
        //declare number1 as a 'String' and it will work. 
	System.out.print( “Product number: ###-##-###: “ ); //prompt
	number1 = input.nextLine(); 

	System.out.print( “Product stock amount: “ ); //prompt
	number2 = input.nextLine(); 

        //Again, this will not work because ##.## is not an integer. 
        //It is a decimal number, so use float or double. 
        // Go back to where number3 is declared in your code and change it from int
        // to either float or double.
	System.out.print( “Product price: $##.##: “ ); //prompt
	number3 = input.nextLine(); 

       //Amount has to be a float or a double for the same reason listed above.
	amount = number2 * number3; // multiply numbers
       // The code you previously had for printing out the amount was close to 
       // working, but don't use that form. Instead use this...
       //the way I did it works since in Java, Strings support then '+' operator.
       //which means that in most cases, if you do String + any other type, it results in String 
          In this case, you are doing String + double = String. 
	System.out.println( “Stock value is $“ + amount); 
        }
}

I have edited your code to what it should look like. I don't guarantee that I changed everything that was wrong, but pay attention to the things that I changed. You shouldn't be declaring a class inside of a method in this case. Do you want the choices to be displayed to the screen until the user quits the program? If so, you should provide menu choices inside of a while loop. For example, you could pop up a menu that asks the user whether they want to add a product to the stock or quit the program. Not that the code I posted above will NEVER end. To give the user the option of ending the program, you could add another System.out.println("Enter 0 to quit the program or 1 to enter more stock"); then you could say int choice = input.nextInt(); and then you could say if (choice == 0){ System.exit(0) // this will quit the program}. So if choice isn't 0, the program will go past that if statement, and go back to the top of the while loop.

I also edited some of your comments.


Also, please respond whether or not you understand this, because its extremely annoying to spend half an hour helping someone only to learn that they never came back to their thread. Lol. If you still don't get it I'll be happy to help more.

I knew this was not anything near correct thank you for helping me. I would love to give an option to end the program I just don't know where to put it in the program. Java to me is like learning computer language or spanish, I just don't catch on fast at all. I have to modify the program with an array and I think I know how to use it (how to word it) but I wouldn't guarentee it. I don't know where to put the array in either. I think that is mostly my problem maybe I need to take a class or something.

java-clueless

Does this look better; it looks real to me now and not so confusing.

//Fig.2.7:Inventory.java
//Inventory program

public class Inventory
{
	//main method begins execution of Java application
public static void main(String[] args) {
	
Scanner input = new Scanner( System.in );
 
String productName;
	Int  productNumber;
	int  productStockAmount;
	int  productPrice;
	int stockValue;

	while(true) {
	System.out.print( “Enter product name: ” ); //prompt
	productName = input.nextLine();

	System.out.print( “Product number: “ ); //prompt
	productNumber = input.nextLine();

	System.out.print( “Product stock amount: “ ); //prompt
	ProductStockAmount = input.nextLine();

	System.out.print( “Product price: “ ); //prompt
	productPrice = input.nextLine();

	System.out.printIn( “Stock value is $” + amount);
	
	}
}
 // end class Inventory
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.