3 Replies - 365 Views - Last Post: 19 May 2014 - 08:51 AM Rate Topic: -----

#1 player1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 19-April 14

Program skipping over user's input

Posted 19 May 2014 - 07:10 AM

So i'm just toying around with making a simple inventory system, and when i'm trying to get the user's input (first the ID number, then the item name, then the item price) it will take the ID number, output asking for the name (but not take any input for it) and then ask for the item's price. I'm really not sure why it's never happened to me before when getting user input.

This is the input part of my code:
public void getInfoAdd() {	
		
		int id;
		String name;
		double price;
		
		System.out.print("Enter ID: ");
		id = sc.nextInt();
		System.out.print("Enter item name:");
		name = sc.nextLine();
		System.out.print("Enter desired price: ");
		price = sc.nextInt();
		
		addItem(id, name, price);
	}


And this is what it outputs:

Quote

Enter ID: 2
Enter item name:Enter desired price: 40
Successfully added: 2 $40.0


Any help is appreciated, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Program skipping over user's input

#2 javaJarrett  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 19
  • Joined: 30-April 14

Re: Program skipping over user's input

Posted 19 May 2014 - 07:36 AM

the scanner class nextInt is leaving the new line character which is getting used for the input for the item name. try adding another sc.nextLine() after the sc.nextInt(). that should correct this issue.
Was This Post Helpful? 2
  • +
  • -

#3 player1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 19-April 14

Re: Program skipping over user's input

Posted 19 May 2014 - 07:40 AM

View PostjavaJarrett, on 19 May 2014 - 07:36 AM, said:

the scanner class nextInt is leaving the new line character which is getting used for the input for the item name. try adding another sc.nextLine() after the sc.nextInt(). that should correct this issue.


Awesome, works great, thanks!

Edit: I am running into another problem as well though. I am trying to search an array list for a specific number (not searching by the index) but i'm getting an index out of bounds error. I think it has something to do with the [b][int toDelete = ids.indexOf(id);/b] but i'm not certain

	public void removeItem(int id) {
		if (ids.contains(id)) {
			int toDelete = ids.indexOf(id);
			ids.remove(toDelete);
			names.remove(toDelete);
			prices.remove(toDelete);
			
			System.out.println("Successfully removed: " + id + "[" + toDelete +"] " + 
								names.get(toDelete) + " $" + prices.get(toDelete));
		} else {
			System.out.println("");
			System.out.println("Error finding item ID: " + id + " - Returning to menu");
			System.out.println("");
			showMenu();
		}
	}

This post has been edited by player1: 19 May 2014 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 player1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 19-April 14

Re: Program skipping over user's input

Posted 19 May 2014 - 08:51 AM

Can't edit my post, but resolved!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1