Results 1 to 4 of 4
  1. #1
    ryan4509 is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Angry How to get user input stored in an array

    I am just starting out learning Java and I created this inventory program to keep track of some information about video games. The user enters in the required info when prompted about the game and that info is supposed to be getting stored in the mygames array. Everything entered on the first time through the loop gets stored in the first element in the array, and so on. Then when stop is entered for the name of the game, the program outputs the contents of the array. The problem I'm having is that when the array is printed, nothing is in it, the information that the user enters is not getting stored in the mygames array. So the only thing printed out is what each variable was initialized to. I'm not sure what I'm doing wrong here.


    Java Code:
    import java.util.Scanner; //Program uses utility Scanner
    
    public class InventoryProgram03{
    
    	public static void main(String args[])
    	{ 
    	
    
    	Scanner input = new Scanner(System.in); //Creates new Scanner to gather user input
    	VideoGameRating[] mygames = new VideoGameRating[10];
    	
    
    	
    	//Lines 18-24, variable declaration
    	String cleanInputBuffer;
    	double gameNumber = 0.0;
    	String gameRating = "";
    	String gameName = "";
    	double gameQuantity = 0.0;
    	double gamePrice = 0.0;
    	double valueOfInventory = 0.0;
    	int count;
    	boolean end = false;
    	
    	
    	
    	for (count = 0; count <=10; count++)
    	{ //I'm using count to represent the current index in the array,
    	 //and to store the information with the particular index.
    		
    		VideoGameRating myGame = new VideoGameRating(gameName, gameNumber, gameQuantity, gamePrice, valueOfInventory, gameRating);
    		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: ");
    		mygames[count].setgameName(input.nextLine()); //Saves user input to variable gameName
    		System.out.print("\n");
    		
    	if ( mygames[count].getgameName().toLowerCase().equals( "stop" ) )
    	{ //If sentinal value has been entered, program should stop looping
    		end = true;
    		
    		for (int a = 0; a <=count; a++)
    		{
    		
    		//outputs contents of mygames array
    		System.out.println("Title of game: " + mygames[count].getgameName());
    		System.out.println("Game Rating: " + mygames[count].getgameRating());
    		System.out.println("Total in stock: " + mygames[count].getgameQuantity());
    		System.out.println("Price for each: " + mygames[count].getgamePrice());
    		System.out.println("Item number: " + mygames[count].getgameNumber());
    		System.out.println("Value of stock: " + mygames[count].valueOfInventory());
    		System.out.print("\n");
    		}//end inner for
    		}//end if
    	
    	else{
    		System.out.printf("Enter in %s rating: ", mygames[count].getgameName());
    		mygames[count].setgameRating(input.nextLine());
    		System.out.print("\n");
    		
    		System.out.printf("Enter in the number of %s games in stock,\n", mygames[count].getgameName()); //Prompt
    		System.out.print("positive numbers only: "); //Prompt
    		mygames[count].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", mygames[count].getgameName()); //Prompt
    		System.out.print("positive numbers only: $"); //Prompt
    		mygames[count].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: ", mygames[count].getgameName()); //Prompt
    		mygames[count].setgameNumber(input.nextDouble()); //Saves user input to gameNumber variable
    		System.out.print("\n\n"); //Outputs 2 blank lines
    		
    		
    		cleanInputBuffer = input.nextLine(); //Cleans input buffer
    		}//end else
    		
    		}//End outer for
    		
    		
    		
    	
    	} //End main
    	
    }//End class
    Java Code:
    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(String itemName, double itemNumber, double itemQuantity, double itemPrice,
    	double valueOfInventory)
    	{
    	
    	
    	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 valueOfInventory()
    	{
    	
    		return itemQuantity * itemPrice;
    	
    	}
    } //Ends VideoGame class
    Java Code:
    class VideoGameRating extends VideoGame 
    {
    
    	public String itemRating;
    
    	public VideoGameRating( String itemName, double itemNumber, double itemQuantity, double itemPrice, double valueOfInventory, String itemRating)
    	{
    		super (itemName, itemNumber, itemQuantity, itemPrice, valueOfInventory);
    		setgameRating (itemRating);
    
    	}//end constructor
    
    	public void setgameRating(String gameRating)
    	{
    	
    		this.itemRating = gameRating;
    	
    	}
    	
    	public String getgameRating()
    	{
    	
    		return itemRating;
    	
    	}
    	
    	public double valueOfInventory()
    	{
    	
    		return super.valueOfInventory() * .05;
    	
    	}
    	
    	
    	
    }//end class

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: How to get user input stored in an array

    I do not believe your method of iterating over an array within the same loop in which you are filling it is sound design. I recommend you use stop to exit thefirst loop and then use a second to print out the contents. However, that is not your problem. When you print out the contents you need to use "a" to index the array. You are still using count.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    ryan4509 is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default Re: How to get user input stored in an array

    Thanks for your help. I can't believe I didn't see that I needed to use a instead of count to print out the array. Now there are 2 problems I'm coming across. The first one is when the valueOfInventory is printed out, for some reason it's not printing out the correct value. I thought I did that method correctly for calculating the value of the inventory, but I must not have, and I can't see where I made the mistake.

    The other problem I'm having is after the array is printed out, the program should stop, it's not, it prints out the array once the sentinel is entered, but then proceeds to gathering info on the next game. I know you said that I should use stop to exit the first loop, I'm not sure what you mean by that. Do you mean that I should use stop to exit the first for loop? The way that I am currently using to exit the loop, in the if statement, am I doing that correct. That's one part of the program that has always given me trouble. Ideally, I would like to have that statement, as well as printing out the contents of the array at the bottom of my class, but I could never figure out how to do that or if that would even work.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: How to get user input stored in an array

    The reason that your not printing out information correctly is twofold. First, after the sentinel is entered you print out the information. But once that loop is done, the outer loop then resumes, filling up mygames from where it left off. Here is some skeletal code to fix the problem. You will need to adapt it to your code.
    Java Code:
          int count;
          String input = "";
          for (count = 0; count < 10; count++) {
             if (input.equals("stop"))
                break;
             
          }
          
          for (int a = 0; a < count; a++) {
             // print out stuff.
          }
    Also, your loop goes from 0 to 10 inclusive (i.e. <= 10). That means 11 entries not 10. So either make it < 10 (not <=10) or increase your array size to 11.

    You never really use your constructor. You pass values but they aren't assigned and even if they were they are always the same. Then you assign the real values with your setter methods. Since the constructor is not doing anything of value you may as well eliminate it.

    Regarding your ValueOfInventory. Since you override the method in your subclass the value should be the super class' value * .05. I suggest you put some print statements in the super classes method and the sub classes method to ensure the values are what you expect.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. How to print array from user input?
    By sims34 in forum New To Java
    Replies: 1
    Last Post: 01-29-2013, 04:46 AM
  2. How to store user input into an array
    By codyjava in forum New To Java
    Replies: 11
    Last Post: 12-02-2012, 04:08 AM
  3. Replies: 3
    Last Post: 09-15-2012, 04:29 PM
  4. Creating 2D array from all user input
    By peek_a_boo in forum New To Java
    Replies: 1
    Last Post: 12-08-2011, 08:16 PM
  5. user input array
    By localhost in forum New To Java
    Replies: 5
    Last Post: 12-30-2010, 04:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •