9 Replies - 576 Views - Last Post: 30 May 2013 - 09:22 PM Rate Topic: -----

#1 ryan4509  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-May 13

I need help saving user input in a single array

Posted 29 May 2013 - 02:25 PM

This is my program I have for keeping track of an inventory. What I'm trying to figure out is how I can store all of the information about that item that the user enters into an array called game. I thought of somehow using a for loop for incrementing through each index in the array and saving the user input from that iteration into the first index, but I can't figure out how to get that to work or even if that would work. What I would like this program to be able to do is save all of the user input from the first time through the loop into the first spot in the array, then on the second iteration save all of that user input about the item into spot 2 in the array, and so on.

I got my program to store input regarding a single item, I just want that input and all of the input from each iteration after that to be stored in a single array.




import java.util.Scanner; //Program uses utility Scanner

public class InventoryProgram02{

	public static void main(String args[]){ //Main method begins java application
	
	InventoryResources game = new InventoryResources(); //Creates new game object of class InventoryResources
	Scanner input = new Scanner(System.in); //Creates new Scanner to gather user input
	InventoryResources[] game = new InventoryResources[5]
	
	//Lines 18-24, variable declaration
	String cleanInputBuffer;
	double gameNumber;
	String gameName;
	double gameQuantity;
	double gamePrice;
	double valueOfInventory;
	boolean end = false;
	int count;
	
	
	
	//Start of while loop
	while (end == false){ 
		System.out.print("\nEnter in the name of the video game, \n"); //Prompt
		System.out.print("or enter in stop, if you're finished: "); //Prompt
		game.setgameName(input.nextLine()); //Saves user input to variable gameName
		System.out.print("\n");
		
	//Start of if else statement	
	if (game.getgameName().toLowerCase().equals("stop")){ //If sentinal value has been entered,
		end = true;                               		  //while loop stops, and program ends
		System.out.print("Program Terminated."); //Output displayed upon program completion
		}
		
	else{
	
		System.out.printf("Enter in the number of %s games in stock,\n", game.getgameName()); //Prompt
		System.out.print("positive numbers only: "); //Prompt
		game.setgameQuantity(input.nextDouble()); //Saves user input to gameQuantity variable
		System.out.print("\n"); // Outputs blank line
		
		System.out.printf("Enter in the price of each %s game,\n", game.getgameName()); //Prompt
		System.out.print("positive numbers only: $"); //Prompt
		game.setgamePrice(input.nextDouble()); //Saves user input to gamePrice variable
		System.out.print("\n"); //Outputs blank line
		
		System.out.printf("Enter in the item number associated with %s: ", game.getgameName()); //Prompt
		game.setgameNumber(input.nextDouble()); //Saves user input to gameNumber variable
		System.out.print("\n\n"); //Outputs 2 blank lines
		
		System.out.printf("The name of the video game in stock is %s.\n", game.getgameName()); //Outputs item name
		System.out.printf("\nThe item number associated with %s is %.2f\n", game.getgameName(), game.getgameNumber()); //Outputs item number
		System.out.printf("\nThe number of the %s games in stock is %.2f.\n", game.getgameName(), game.getgameQuantity()); //Outputs item quantity
		System.out.printf("\nThe price for each of the %s games is $%.2f.\n", game.getgameName(), game.getgamePrice()); //Outputs item price
		System.out.printf("\nThe total value of the %s stock is $%.2f.\n\n", game.getgameName(), game.getvalueOfInventory()); //Outputs value 
	                                                                                                                        //of inventory
		cleanInputBuffer = input.nextLine(); //Cleans input buffer
	
	}
	
} //End while loop
	
	
} //End main method


} //End InventoryProgram01 class






public class InventoryResources{

	//lines 10-14, variables declaration
	public String itemName;
	public double itemNumber;
	public double itemQuantity;
	public double itemPrice;
	public double valueOfInventory;
	
	
	//Constructor with 6 arguments
	public InventoryResources(){
	
	itemName = ""; 
	itemNumber = 0;
	itemQuantity = 0;
	itemPrice = 0;
	valueOfInventory = 0;
	
	
	}
	
	public void setgameName(String gameName){
	
	this.itemName = gameName;
	
	}
	
	public String getgameName(){
	
	return itemName;
	
	}
	
	public void setgameQuantity(double gameQuantity){
	
	this.itemQuantity = gameQuantity;
	
	}
	
	public double getgameQuantity(){
	
	return itemQuantity;
	
	}
	
	public void setgameNumber(double gameNumber){
	
	this.itemNumber = gameNumber;
	
	}
	
	public double getgameNumber(){
	
	return itemNumber;
	
	}
	
	public void setgamePrice(double gamePrice){
	
	this.itemPrice = gamePrice;
	
	}
	
	public double getgamePrice(){
	
	return itemPrice;
	
	}
	
	public double getvalueOfInventory(){
	
	return itemQuantity * itemPrice;
	
	}
	
	
	
} //Ends InventoryResources class




Is This A Good Question/Topic? 0
  • +

Replies To: I need help saving user input in a single array

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: I need help saving user input in a single array

Posted 29 May 2013 - 02:56 PM

If you're getting errors, you should post them, copied and pasted exactly as they appear to you. If you're not seeing errors, then you're not paying attention.

There are two variables of different types in the same scope (local to method main()) with the same name, 'game'. That's not allowed. Delete one of them.

Is InventoryResources a class that will contain a game's details? If so, why not call it Game or VideoGame?

The user's input will be entered into an instance of InventoryResources (with a better name), that is collected in the array games:

InventoryResources[] games = new InventoryResources[5];

Then an instance has to be created and filled with the user's input:

games[0] = new InventoryResources();

// get the video game's name and add it to the games[0]
// instance
games[0].setGameName( videoGameName );

etc . . .

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: I need help saving user input in a single array

Posted 29 May 2013 - 03:54 PM

That is an horror !!! Programming wise

The main() method should have only 3 to 5 lines
the rest, in a OO approach (and Java is a OO language) should be within the class instance
Was This Post Helpful? 1
  • +
  • -

#4 ryan4509  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-May 13

Re: I need help saving user input in a single array

Posted 29 May 2013 - 09:42 PM

So what I'm trying to do here is keep track of an inventory of games which are stored in an array called mygames. The user enters in data about the games, which should be getting stored in the mygames array. Then when the user enters in stop for the name of the game, the program prints off the contents of the array.

The problem that I'm having here is that the information is not being stored in the mygames array, so when the program goes to output the contents of the array, it just prints off the data for the game from the most recent iteration of the loop. I'm not sure what I'm doing wrong, and why the information is not being stored in the mygames array.

My goal for the program is to have it continue looping through the for loop gathering user input and storing it in the mygames array, until the sentinel value ("stop") is entered. Then it should just print off all of the contents of the array that were entered up to that point.

I'm just starting off learning Java, so any help regarding this would be appreciated.



import java.util.Scanner; //Program uses utility Scanner

public class InventoryProgram02{

	public static void main(String args[]){ //Main method begins java application
	
	VideoGame game = new VideoGame();
	Scanner input = new Scanner(System.in); //Creates new Scanner to gather user input
	VideoGame[] mygames = new VideoGame[10];
	
	//Lines 18-24, variable declaration
	String cleanInputBuffer;
	double gameNumber;
	String gameName;
	double gameQuantity;
	double gamePrice;
	double valueOfInventory;
	boolean end = false;
	int count;
	
	
	for (count = 0; count <=10; count++){
	 
		VideoGame myGame = new VideoGame();
		mygames[count] = myGame;
		System.out.print("\nEnter in the name of the video game,\n"); //Prompt
		System.out.print("or enter in stop if your finished: ");
		game.setgameName(input.nextLine()); //Saves user input to variable gameName
		System.out.print("\n");
		
	if (game.getgameName().toLowerCase().equals("stop")){ //If sentinal value has been entered,
		end = true;                               		  //while loop stops, and program ends
		for (int a = 0; a <=count; a++){
			
		System.out.println("Title of game: " + game.getgameName());
		System.out.println("Total in stock: " + game.getgameQuantity());
		System.out.println("Price for each: " + game.getgamePrice());
		System.out.println("Item number: " + game.getgameNumber());
		System.out.println("Value of stock: " + game.getvalueOfInventory());
		System.out.print("\n");
			
			}//ends inner for
                }//ends if
	
	else{	
		System.out.printf("Enter in the number of %s games in stock,\n", game.getgameName()); //Prompt
		System.out.print("positive numbers only: "); //Prompt
		game.setgameQuantity(input.nextDouble()); //Saves user input to gameQuantity variable
		System.out.print("\n"); // Outputs blank line
		
		System.out.printf("Enter in the price of each %s game,\n", game.getgameName()); //Prompt
		System.out.print("positive numbers only: $"); //Prompt
		game.setgamePrice(input.nextDouble()); //Saves user input to gamePrice variable
		System.out.print("\n"); //Outputs blank line
		
		System.out.printf("Enter in the item number associated with %s: ", game.getgameName()); //Prompt
		game.setgameNumber(input.nextDouble()); //Saves user input to gameNumber variable
		System.out.print("\n\n"); //Outputs 2 blank lines
		
		
		cleanInputBuffer = input.nextLine(); //Cleans input buffer
		}//ends else
		}//ends outer for
		
		
		
	
	} //End main
	
}//End class
	






public class VideoGame{

	//lines 10-14, variables declaration
	public String itemName;
	public double itemNumber;
	public double itemQuantity;
	public double itemPrice;
	public double valueOfInventory;
	
	
	//Constructor with 5 arguments
	public VideoGame(){
	
	itemName = ""; 
	itemNumber = 0;
	itemQuantity = 0;
	itemPrice = 0;
	valueOfInventory = 0;
	
	
	}
	
	public void setgameName(String gameName){
	
	this.itemName = gameName;
	
	}
	
	public String getgameName(){
	
	return itemName;
	
	}
	
	public void setgameQuantity(double gameQuantity){
	
	this.itemQuantity = gameQuantity;
	
	}
	
	public double getgameQuantity(){
	
	return itemQuantity;
	
	}
	
	public void setgameNumber(double gameNumber){
	
	this.itemNumber = gameNumber;
	
	}
	
	public double getgameNumber(){
	
	return itemNumber;
	
	}
	
	public void setgamePrice(double gamePrice){
	
	this.itemPrice = gamePrice;
	
	}
	
	public double getgamePrice(){
	
	return itemPrice;
	
	}
	
	public double getvalueOfInventory(){
	
	return itemQuantity * itemPrice;
	
	}
	

	
	
} //Ends VideoGame class







Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: I need help saving user input in a single array

Posted 29 May 2013 - 09:48 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#6 ryan4509  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-May 13

Re: I need help saving user input in a single array

Posted 29 May 2013 - 10:13 PM

As you can see I've made some modifications to my program from my original post, but I still can't get the program to accomplish what I'm trying to accomplish with it. The information is still not being stored in the my games array.

View Postmacosxnerd101, on 29 May 2013 - 09:48 PM, said:

Duplicate threads merged. Please avoid duplicate posting.

Sorry about that, I couldn't figure out how to delete my original post, so that I could just have the one.

This post has been edited by macosxnerd101: 29 May 2013 - 10:14 PM
Reason for edit:: Removed large quote. It is unnecessary.

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: I need help saving user input in a single array

Posted 29 May 2013 - 10:14 PM

You can't delete posts.
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: I need help saving user input in a single array

Posted 30 May 2013 - 02:09 PM

What's the point of the count variable and the for ( count ) loop if the user can abort entry with the word "stop?"

After that, program steps are a bit out of order and you're still confused about how to use an array of objects. I provided more examples of how to do it correctly and then reordered your code to correct the logic flow. I also rewrote the main() method to honor pbl's comment, but you can cram it all back into a main() method if you want.

There are holes for you to fill in, mostly with code you've already written. Let me know if you have questions.
public class ArrayOfObjectsDemo
{
    VideoGame[] videoGames;
    String gameName;
    Scanner input;
    int arrayIndex;
    
    // default constructor - the constructor plus new methods do all of the
    // work that the main() method used to do
    ArrayOfObjectsDemo()
    {
        // initialize instance variables
        videoGames = new VideoGame[10];
        gameName = "";
        input = new Scanner( System.in );
        
        // fill the videoGames[] array with user input
        getUserInput();
        
        // output the video games entered by the user
        displayInventory( arrayIndex-- );
        
    } // end default constructor
    
    private void getUserInput()
    {
        System.out.print("\nEnter in the name of the video game,\n"); //Prompt
        System.out.print("or enter in stop if your finished: ");
        gameName = input.nextLine();

        // leave user entry if done
        if ( gameName.equalsIgnoreCase( "stop" ) )
        {
            return;
        }

        // create an object in the array at the next element
        videoGames[arrayIndex] = new VideoGame();

        // fill the new array object with values
        videoGames[arrayIndex].setgameName( gameName );
        System.out.print("\n");
        System.out.printf("Enter in the number of %s games in stock,\n",
                gameName );
        System.out.print("positive numbers only: ");
        videoGames[arrayIndex].setgameQuantity(input.nextDouble());

        // . . . you do the rest
        
        // clear the buffer
        input.nextLine();
        
        // get the next input
        getUserInput();
        
    } // end method getUserInput()
    
    private void displayInventory( int numberOfVideos )
    {
        arrayIndex = 0;

        for ( int i = 0 ; i < numberOfVideos ; i++ )
        {
            System.out.println("Title of game: " + videoGames[i].
                    getgameName());

            // . . . you do the rest

            System.out.print("\n");
        }

    } // end method displayInventory()
    
    
    // to pbl's point, this is an example of a proper main() method
    // for this kind of program
    public static void main( String[] args )
    {
        new ArrayOfObjectsDemo();
        
    } // end method main()
    
}//End class

Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: I need help saving user input in a single array

Posted 30 May 2013 - 08:16 PM

all the game reference like

game.setgameQuantity(input.nextDouble()); //

should be reference to myGame which is your array element

myGame.setgameQuantity(input.nextDouble()); //
Was This Post Helpful? 1
  • +
  • -

#10 ryan4509  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-May 13

Re: I need help saving user input in a single array

Posted 30 May 2013 - 09:22 PM

Thank you pbl and GregBrannon for your help. This was my first attempt at using an array of objects, or an array for that matter. I'm beginning to get the hang of how an array works, it's just going to take some more time and practice using them before I really get the hang of it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1