Thursday, May 30, 2013

I Need Help Saving User Input In A Single Array - Java | Dream.In ...


Example
#{example}
"); ipb.editor_values.get('templates')['togglesource'] = new Template("
??? Cancel Source Edit
"); ipb.editor_values.get('templates')['toolbar'] = new Template(""); ipb.editor_values.get('templates')['button'] = new Template("
  • Icon
  • "); ipb.editor_values.get('templates')['menu_item'] = new Template("
  • #{title}
  • "); ipb.editor_values.get('templates')['togglesource'] = new Template("
    ??? Cancel Source Edit
    "); ipb.editor_values.get('templates')['emoticons_showall'] = new Template(""); ipb.editor_values.get('templates')['emoticon_wrapper'] = new Template("

    Emoticons

    "); // Add smilies into the mix ipb.editor_values.set( 'show_emoticon_link', false ); ipb.editor_values.set( 'bbcodes', $H({"snapback":{"id":"1","title":"Post Snap Back","desc":"This tag displays a little linked image which links back to a post - used when quoting posts from the board. Opens in same window by default.","tag":"snapback","useoption":"0","example":"[snapback]100[/snapback]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"topic":{"id":"5","title":"Topic Link","desc":"This tag provides an easy way to link to a topic","tag":"topic","useoption":"1","example":"[topic=1]Click me![/topic]","switch_option":"0","menu_option_text":"Enter the topic ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"post":{"id":"6","title":"Post Link","desc":"This tag provides an easy way to link to a post.","tag":"post","useoption":"1","example":"[post=1]Click me![/post]","switch_option":"0","menu_option_text":"Enter the Post ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"spoiler":{"id":"7","title":"Spoiler","desc":"Spoiler tag","tag":"spoiler","useoption":"0","example":"[spoiler]Some hidden text[/spoiler]","switch_option":"0","menu_option_text":"","menu_content_text":"Enter the text to be masked","single_tag":"0","optional_option":"0","image":""},"acronym":{"id":"8","title":"Acronym","desc":"Allows you to make an acronym that will display a description when moused over","tag":"acronym","useoption":"1","example":"[acronym='Laugh Out Loud']lol[/acronym]","switch_option":"0","menu_option_text":"Enter the description for this acronym (EG: Laugh Out Loud)","menu_content_text":"Enter the acronym (EG: lol)","single_tag":"0","optional_option":"0","image":""},"hr":{"id":"12","title":"Horizontal Rule","desc":"Adds a horizontal rule to separate text","tag":"hr","useoption":"0","example":"[hr]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"1","optional_option":"0","image":""},"php":{"id":"14","title":"PHP Code","desc":"Allows you to enter PHP code into a formatted/highlighted syntax box","tag":"php","useoption":"0","example":"[php]$variable = true;\n\nprint_r($variable);[/php]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"html":{"id":"15","title":"HTML Code","desc":"Allows you to enter formatted/syntax-highlighted HTML code","tag":"html","useoption":"0","example":"[html]\n \n[/html]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"sql":{"id":"16","title":"SQL Code","desc":"Allows you to enter formatted/syntax-highlighted SQL code","tag":"sql","useoption":"0","example":"[sql]SELECT p.*, t.* FROM posts p LEFT JOIN topics t ON t.tid=p.topic_id WHERE t.tid=7[/sql]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"xml":{"id":"17","title":"XML Code","desc":"Allows you to enter formatted/syntax-highlighted XML code","tag":"xml","useoption":"0","example":"[xml]\n \n Test\n \n[/xml]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"member":{"id":"31","title":"Member","desc":"Given a member name, a link is automatically generated to the member's profile","tag":"member","useoption":"1","example":"[member=skyhawk133] runs this site.","switch_option":"0","menu_option_text":"Input Username of Member","menu_content_text":"","single_tag":"1","optional_option":"0","image":"memberbbcode.png"},"extract":{"id":"33","title":"Extract Blog Entry","desc":"This will allow users to define an extract for an entry. Only this piece of the entry will be displayed on the main blog page and will show up in the RSS feed.","tag":"extract","useoption":"0","example":"[extract]This is an example![/extract]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"blog":{"id":"34","title":"Blog Link","desc":"This tag provides an easy way to link to a blog.","tag":"blog","useoption":"1","example":"[blog=100]Click me![/blog]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"entry":{"id":"35","title":"Blog Entry Link","desc":"This tag provides an easy way to link to a blog entry.","tag":"entry","useoption":"1","example":"[entry=100]Click me![/entry]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"twitter":{"id":"36","title":"Twitter","desc":"A tag to link to a user's twitter account","tag":"twitter","useoption":"0","example":"[twitter]userName[/twitter]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":"twitter.png"},"inline":{"id":"37","title":"Inline Code","desc":"Formats code inline instead of in a seperate code box. ","tag":"inline","useoption":"0","example":"[inline]style=\"font-size: 12px;\"[/inline]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"il":{"id":"38","title":"Abbreviated Inline (IL)","desc":"Abbreviated version of the [inline] tag. ","tag":"il","useoption":"0","example":"[il]Code Here[/il]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":"il2.png"},"code":{"id":"41","title":"Code","desc":"Allows you to enter general code","tag":"code","useoption":"1","example":"[code]$text = 'Some long code here';[/code]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"1","image":""}}) ); ipb.vars['emoticon_url'] = "http://cdn.dreamincode.net/forums/public/style_emoticons/default"; //Search Setup ipb.vars['search_type'] = 'forum'; ipb.vars['search_type_id'] = 32; ipb.vars['search_type_2'] = 'topic'; ipb.vars['search_type_id_2'] = 322059; //]]>

    6 Replies - 61 Views - Last Post: Yesterday, 10:14 PM Rate Topic: -----

    #1 ryan4509 ?Icon User is offline

    Reputation: 0

    • Posts: 5
    • Joined: 19-May 13

    Posted Yesterday, 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

    Reputation: 1992

    • Posts: 4,849
    • Joined: 10-September 10

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

    Posted Yesterday, 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.


    #3 pbl ?Icon User is offline

    Reputation: 8040

    • Posts: 31,233
    • Joined: 06-March 08

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

    Posted Yesterday, 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


    #4 ryan4509 ?Icon User is offline

    Reputation: 0

    • Posts: 5
    • Joined: 19-May 13

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

    Posted Yesterday, 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      


    #5 macosxnerd101 ?Icon User is offline

    Reputation: 9056

    • Posts: 33,609
    • Joined: 27-December 08

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

    Posted Yesterday, 09:48 PM

    Duplicate threads merged. Please avoid duplicate posting.


    #6 ryan4509 ?Icon User is offline

    Reputation: 0

    • Posts: 5
    • Joined: 19-May 13

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

    Posted Yesterday, 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: Yesterday, 10:14 PM
    Reason for edit:: Removed large quote. It is unnecessary.


    #7 macosxnerd101 ?Icon User is offline

    Reputation: 9056

    • Posts: 33,609
    • Joined: 27-December 08

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

    Posted Yesterday, 10:14 PM

    You can't delete posts.


    Page 1 of 1


    Source: http://www.dreamincode.net/forums/topic/322059-i-need-help-saving-user-input-in-a-single-array/

    NCAA Bracket 2013 Robert Morris spring lululemon jon hamm southern university biggest loser

    No comments:

    Post a Comment

    Note: Only a member of this blog may post a comment.