I need to accept a String input and a double input from a user and store them into two parallel arrays to create a shopping cart.
I have initialized both arrays to a user determined size Amount and created a for loop to run for the inputs and store the values in the two arrays, and have set an extra input.nextLine() so the code will swallow the new line created at the end of the loop. This is my code so far,
import java.util.*;
import java.io.*;
public class Item
{
public static void main(String[] args)
throws java.io.IOException
{
int amount;
String nextItem,I;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of items you would like to add:");
amount = input.nextInt();
String cart[] = new String[amount];
double price[] = new double[amount];
for(int i = 0; i<amount; i++)
{
System.out.println("Please enter the name of an item:");
cart[i] = input.nextLine();
System.out.println("Price?");
price[i] = input.nextDouble();
input.nextLine();
}
}
}
However when the program runs the loop it runs both System.out.println commands skipping past the first input.nextLine(), and will only accept an integer input.
use two scanner class
Scanner sc=new Scanner(System.in);
Scanner sc1=new Scanner(System.in);
int p[] = new int[3];
String n[] = new String[3];
for(int i = 0; i < p.length; i++){
System.out.print("Name: ");
n[i] = sc.nextLine();
System.out.print("Percentage: ");
p[i]=sc1.nextInt();
// use two scanner class
Related
My code breaks when the user is supposed to input a unique number for itemNumber, however there is no error displayed in NetBeans, can someone help me with why this happens?
package pre_release;
import java.util.*;
public class Pre_Release {
public static void main(String[] args) {
int numberOfItems = 0;
int number;
int bidNumber;
double highestBid = 0;
Scanner userInput = new Scanner (System.in);
Scanner userString = new Scanner (System.in);
Scanner userDouble = new Scanner (System.in);
Scanner userBuyer = new Scanner (System.in);
Scanner userInt = new Scanner (System.in);
Scanner userItem = new Scanner (System.in);
do{
System.out.println("Please enter the the amount of items for the auction. Needs to be more than or equal to 10");
numberOfItems = userInput.nextInt();
} while(numberOfItems<10);
String[] description = new String[numberOfItems];
double[] reservePrice = new double[numberOfItems];
double[] bid = new double[numberOfItems];
int[] itemNumber;
itemNumber = null;
int[] buyNumber = new int[numberOfItems];
for(int count=0;count<numberOfItems;count++){
System.out.println("Input a number for each item number");
itemNumber[count] = userInt.nextInt();
}
for(int count=0;count<numberOfItems;count++){
bidNumber=0;
System.out.println("Please give your item a reserved price");
reservePrice[count] = userDouble.nextDouble();
System.out.println("Please describe your item");
description[count] = userString.nextLine();
}
System.out.println("Please enter the desired item you wish to see");
number = userInt.nextInt();
for(int count2=0;count2<numberOfItems;count2++){
if(itemNumber[count2] == number){
System.out.println("The reserved price is" + reservePrice[numberOfItems]);
System.out.println(description[numberOfItems]);
}
System.out.println("Would you like to put a bid on this item? Needs to be more than" + reservePrice[numberOfItems]);
}
}
}
Haven't seen your error message, but probably you've got the error because your array reference is null
int[] itemNumber;
itemNumber = null
In order to fix this, replace these lines with this one:
int[] itemNumber = new int[numberOfItems];
I am trying to create a program which takes in a name and an age and stores them in their respected arrays. When the program is run, it ignores the line:
personName[i] = in.nextLine();
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Persons {
public static void main (String[] args){
Scanner in = new Scanner(System.in);
String[] personName = new String[9];
int[] personAge = new int[9];
for(int i = 0; i < personName.length; i++){
System.out.println("Please enter a name: ");
personName[i] = in.nextLine();
if(!personName[i].equalsIgnoreCase("done")){
System.out.println("Please enter an age: ");
personAge[i] = in.nextInt();
}
}
}
}
Any ideas?
You have tried to read the integer value without reading entire line.
Change your line to:
personAge[i] = Integer.parseInt(in.nextLine());
Hello guys I am having a problem with an array and a .nextInt(); this is causing my output line at the 3rd prompt to shift up instead of under, and seriously cannot figure out what's wrong.
I have tried .hasNextInt(); but nothing, it actually gives me an error, so here is the code:
import java.util.Random;
import java.util.Scanner;
public class birthday {
public static void main(String[] args) {
System.out.println("Welcome to the birthday problem Simulator\n");
String userAnswer="";
Scanner stdIn = new Scanner(System.in);
do {
int [] userInput = promptAndRead(stdIn); //my problem
double probability = compute(userInput[0], userInput[1]);
// Print results
System.out.println("For a group of " + userInput[1] + " people, the probability");
System.out.print("that two people have the same birthday is\n");
System.out.println(probability);
System.out.print("\nDo you want to run another set of simulations(y/n)? :");
//eat or skip empty line
stdIn.nextLine();
userAnswer = stdIn.nextLine();
} while (userAnswer.equals("y"));
System.out.println("Goodbye!");
stdIn.close();
}
// User input prompt where you make the simulation. For people and return them as an array
public static int[] promptAndRead(Scanner stdIn)
{
System.out.println("Please enter the number of simulations you want to do: ");
int[] userInput =new int [2]; //my problem
userInput[0]= stdIn.nextInt(); //my problem
System.out.println("Please enter the size of the group you want : ");
int[] userInput1 = new int [2];
userInput[1] = stdIn.nextInt();
int a = userInput[1];
while (a<2 || a>365)
{
System.out.println("please type the number that is between 2~365");
}
System.out.println();
return promptAndRead(stdIn);
}
// Method for calculations
public static double compute(int numOfSimulation, int numOfPeople)
{
for (int i =0; i < numOfPeople; i++)
{
Random rnd = new Random(1);
//Generates a random number between 0 and 364 exclusive
int num = rnd.nextInt(364);
System.out.println(num);
System.out.println(num / 365 * numOfPeople * numOfSimulation);
}
return numOfPeople;
}
}
Found it!!!!!!!!!!!
do this:
// User input prompt where you make the simulation. For people and return them as an array
public static int[] promptAndRead(Scanner stdIn)
{
System.out.println("Please enter the number of simulations you want to do: ");
int[] userInput =new int [2]; //CHANGE THIS TO 1?
userInput[0]= stdIn.nextInt(); //my problem
System.out.println("Please enter the size of the group you want : ");
int[] userInput1 = new int [2]; //CHANGE THIS TO 1?
userInput[1] = stdIn.nextInt();
int a = userInput[1];
while (a<2 || a>365)
{
System.out.println("please type the number that is between 2~365");
}
System.out.println();
return(userInput);
}
To return the array
Let me know!
I actually don't think you can do it there with that userInput, I am saying this because the methodology of doing this program is quite arcane.
You are then calling 2 arrays at prompting, I wonder if you might change that to one what will change such as:
// User input prompt where you make the simulation. For people and return them as an array
public static int[] promptAndRead(Scanner stdIn)
{
System.out.println("Please enter the number of simulations you want to do: ");
int[] userInput =new int [2]; //CHANGE THIS TO 1?
userInput[0]= stdIn.nextInt(); //my problem
System.out.println("Please enter the size of the group you want : ");
int[] userInput1 = new int [2]; //CHANGE THIS TO 1?
userInput[1] = stdIn.nextInt();
int a = userInput[1];
while (a<2 || a>365)
{
System.out.println("please type the number that is between 2~365");
}
System.out.println();
return promptAndRead(stdIn);
}
As also the return promptAndRead(stdIn); might be part of the problem
Don't know though just trowing suggestions at Markov ;)
I want the user to only be able to type in no more than three number into the console. Is there any way to limit or stop the console from accepting keyboard input?
Thanks
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Type three integers ");
int firstInt = userInput.nextInt();
int secondInt = userInput.nextInt();
int thirdInt = userInput.nextInt();
System.out.println(firstInt + secondInt + thirdInt);
}
}
int count = 3;
Scanner z = new Scanner(System.in);
List<Integer> l = new ArrayList<>();
while(count>0)
{
int n = z.nextInt();
l.add(n);
count--;
}
Runtime.getRuntime().exit(0);
Runtime.getRuntime().exit(0) will allow code to skip next lines in code.
give it a try
Hope this is the solution you are looking for
What I am trying to do is have multiple inputs that all have different variables. Each variable will be part of different equations. I am looking for a way to do this, and I think I have an idea. I just want to know if this would be legal, and if there is a better way to do this.
import java.util.*;
public class Example{
public static void main(String args[]){
Scanner dd = new Scanner(System.in);
System.out.println("Enter number.");
int a = dd.nextInt();
System.out.println("Enter number.");
int b = dd.nextInt();
System.out.println("Enter number.");
int c = dd.nextInt();
}
}
If every input asks the same question, you should use a for loop and an array of inputs:
Scanner dd = new Scanner(System.in);
int[] vars = new int[3];
for(int i = 0; i < vars.length; i++) {
System.out.println("Enter next var: ");
vars[i] = dd.nextInt();
}
Or as Chip suggested, you can parse the input from one line:
Scanner in = new Scanner(System.in);
int[] vars = new int[3];
System.out.println("Enter "+vars.length+" vars: ");
for(int i = 0; i < vars.length; i++)
vars[i] = in.nextInt();
You were on the right track, and what you did works. This is just a nicer and more flexible way of doing things.