• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with code  RSS feed

 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all! I am writing an inventory program for school and have run into a problem that I can't figure out. I believe it has to do with


switch (menuSelection){
case 1:
System.out.println("How Many Items would you like to add?");
x = userInput.nextInt();
for( int i = 0; i < x; i ++){
game[i] = new Game();
}

for( int i = 0; i < x; i++ ){
System.out.println("Enter name of Game: ");
name = userInput.nextLine(); <------
^^^^^^^

I just don't know why it is causing a problem.

The problem only happens while its running.
I run it, hit 1 then enter, hit 1 then enter, type in a random name then hit enter, type in a random number then hit enter. Once I do that the program is suppose to show: Name: (whatever you typed) Price: (whatever you typed). The problem is that it only outputs the price.

What is wrong and why?
Thank you for the help.





_______________________________________________________________________________________________________________________________________________________________________
import java.util.Scanner;
import java.util.Arrays;

public class Main {

static Scanner userInput = new Scanner(System.in);

public static void main(String[] args) {

int menuSelection = 0;
int x =0;
String name= "Bob";

Game[] game = new Game[100];

do{
System.out.println("\t\t\tVideo Game Inventory\n\n");
System.out.println("\t\t1 Add Item");
System.out.println("\t\t2 Edit Item");
System.out.println("\t\t3 Delete Item");
System.out.println("\t\t4 See Total value of Inventory");
System.out.println("\t\t5 See all of the Inventory");
System.out.println("\t\t6 Exit Program\n\n");
System.out.println("Please Select a Menu Item by pressing a number 1-6");
menuSelection = userInput.nextInt();

switch (menuSelection){
case 1:
System.out.println("How Many Items would you like to add?");
x = userInput.nextInt();
for( int i = 0; i < x; i ++){
game[i] = new Game();
}

for( int i = 0; i < x; i++ ){
System.out.println("Enter name of Game: ");
name = userInput.nextLine();
game[i].setgameName(name);
userInput.nextLine();

System.out.println("Enter Price of Game: ");
game[i].setpriceOfGame(userInput.nextDouble());

userInput.nextLine();

System.out.println("Item has been added");
System.out.println("Name: " + game[i].getgameName() + "\tPrice: " + game[i].getpriceOfGame());
game[i].setidentNumber(i);
game[i].setgamesInStock(x);
System.out.println();
}
break;

case 2:

break;

case 3:

break;

case 4:

break;

case 5:
for(int i = 0; i < x; i++){
System.out.println("Name: " + game[i].getgameName() + "\tPrice: " + game[i].getpriceOfGame());

}
break;

case 6:
System.out.println("Thank you for using this Program\nGoodBye");
break;
}

}while (menuSelection != 6);

}

}

_______________________________________________________________________________________________________________________________________________________________________


public class Game {

public String gameName = "awesome";
private int identNumber = 001;
public static int gamesInStock = 0;
private double priceOfGame = 0;
private double inventoryTotalValue = 0.00;

public String getgameName(){

return gameName;
}

public int getidentNumber(){

return identNumber;
}

public int getgamesInStock(){

return gamesInStock;
}

public double getpriceOfGame(){

return priceOfGame;
}

public double getinventoryTotalValue(){

return inventoryTotalValue;
}

public void setgameName(String newgameName){

gameName = newgameName;
}

public void setidentNumber(int newidentNumber){

identNumber= newidentNumber;
}

public void setgamesInStock(int totalgamesInStock){

gamesInStock = totalgamesInStock;
}

public void setpriceOfGame(double newPrice){

priceOfGame = newPrice;
}

public void showinventoryTotalValue(){

inventoryTotalValue = gamesInStock * priceOfGame;
System.out.println("Total Value of Inventory is: $" + inventoryTotalValue);
}

public Game(){

}

public Game(String name, double price){

gameName = name;
priceOfGame = price;
}
} _
 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the first post. It is my first ever on this site and I didn't fully understand how to post it as code. Because of my error I reposted it. Thank you for understanding.


Hello all! I am writing an inventory program for school and have run into a problem that I can't figure out. I believe it has to do with





I just don't know why it is causing a problem.

The problem only happens while its running.
I run it, hit 1 then enter, hit 1 then enter, type in a random name then hit enter, type in a random number then hit enter. Once I do that the program is suppose to show: Name: (whatever you typed) Price: (whatever you typed). The problem is that it only outputs the price.

What is wrong and why?
Thank you for the help.





 
vishal saha
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in Main :
~ comment #line 44
~ add userInput.nextLine() # line 34

reason ? ,

take a look:

read about Scanner.nextInt and Scanner.nextLine
 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. Now that I know where the problem is (tested and works) I am on my way to go study those. Thank you very much!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!