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];
Related
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
Can you tell me how to solve that problem ?
Scanner scanner = new Scanner(System.in);
package Exercise2501;
import java.util.Scanner;
public class Friends
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("How many names do you want to enter?");
int number = scanner.nextInt();
for (int i = 0; i < number; i++)
{
System.out.println("Type a few names. ");
String names = scanner.nextLine();
}
String names = scanner.nextLine();
System.out.println(names);
}
}
package Exercise2501;
import java.util.Scanner;
public class Friends {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//Get the number of entries
System.out.println("How many names do you want to enter?");
int number = scanner.nextInt();
//create an array with that number
String names[] = new names[number];
//Ask the user to enter the Names
System.out.println("Type a few names. ");
//Store the names in the array
for (int i = 0; i < number; i++)
{
names[i] = scanner.nextLine();
}
//Do the remaining you want
}
}
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
How do I add an array with several methods together
public void inputArray() {
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the number of invoices: ");
numInvoices = keyboard.nextInt();
Invoices = new Invoice[numInvoices];
int BillTotal = 0;
for(int i = 0; i < numInvoices; i++){
Invoices[i] = new Invoice();
Invoices[i].setCompanyNameFromUser();
Invoices[i].setBillAmountFromUser();
Invoices[i].SetDateFromUser();
BillTotal = BillTotal + Invoices[i].setBillAmountFromUser;
In this case I want to add up the values input by the user in the setBillAmountFromUser method.
This program block may help you
public void inputArray()
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the number of invoices: ");
int numInvoices = keyboard.nextInt();
int BillTotal=0;
Invoice Invoices[numInVoices];
for(int i=0;i<numInVoices;i++)
{
Invoices[i]=new Invoices();/*Your Code*/
BillTotal+=Invoices[i].setBillAmountFromUser();
}
System.out.println("BillTotal="+BillTotal);
}
public int setBillAmountFromUser()/*Example Code block/*
{
/*your code */
return 5;//Example Return value
}
I have been working on this for a while, but can't quite seem to figure out how to add an item to an ArrayList. I would like to add grocItem (should be 7 grocItems from user input from for loop) into an ArrayList of grocList:
public class ItemData{
public ItemData(String name, double cost, int priority){
Main.(ArrayList grocList).add(grocItem);
// Main.groclist.add(grocItem);
}
}
Main Class:
import java.util.*;
public class Main {
public static List<ItemData> itemData = new ArrayList<ItemData>();
public static void main(String[] args) {
int i=0;
//String name1;
//int priority1;
//double cost1;
String[] item = new String[7];
for (i=0; i<item.length; i++) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
String name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
double cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
int priority = keyboard3.nextInt();
ItemData grocItem = new ItemData(name, cost, priority);
}
//How do I add grocItem to an Array list of other grocItems (6 grocItems from user input array item)
Main.itemData.add(groclist);
}
}
Change your code, add the method inside the loop.
for (i=0; i<item.length; i++) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
String name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
double cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
int priority = keyboard3.nextInt();
ItemData grocItem = new ItemData(name, cost, priority);
itemData.add(grocItem ); // add here
}
You should add the ItemData object to your arraylist inside your loop:
for (i=0; i<item.length; i++) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
String name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
double cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
int priority = keyboard3.nextInt();
ItemData grocItem = new ItemData(name, cost, priority);
itemData.add(groclist); // <-- add to arraylist inside the loop
}