0

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.

  • 1
    What type of object is `input`? (Is it a scanner? How is it instantiated? ) – Davy M Apr 07 '19 at 01:03
  • 1
    This code should work right if you define `input` as `Scanner input = new Scanner(System.in);`. – CryptoFool Apr 07 '19 at 01:10
  • 1
    A few notes: 1) `Cart` and `Price`, and `Amount` should be `cart`, `price` and `amount`, per standard coding conventions and 2) you're calling `input.nextInt();` but assigning the result to a an array of `double` values. Another student once told me that the instructor told them to use uppercase variable names. That's wack. – CryptoFool Apr 07 '19 at 01:18
  • 1
    @Steve My bet is that they're using the scanner before this point, maybe they're reading in Amount as an integer before this, so the new line character is still sitting on stdin before they read the item name, but they haven't provided enough information to be sure, since as you point out, this code should work. – Davy M Apr 07 '19 at 01:19
  • @DavyM, I thought of that. I was thinking of asking if the loop worked right the second time around. - Your point makes perfect sense. Another `input.nextLine();` is needed. – CryptoFool Apr 07 '19 at 01:20
  • 1
    [Anti-pattern: parallel collections | Jon Skeet's coding blog](https://codeblog.jonskeet.uk/2014/06/03/anti-pattern-parallel-collections/) – Ole V.V. Apr 07 '19 at 03:06
  • What do your arrays contain after you run your code? Your code works on my computer. In one run I got `[Carots, Rice, Onions]` and `[5.0, 3.0, 4.0]`. – Ole V.V. Apr 07 '19 at 03:14
  • I'm sorry I didn't realize I hadn't added the initialization of the variables in my code I have added them into the original post as well as some of the formatting changes you mentioned. As for the loop this problem only exist for the first loop after which the loop runs normally, it returns `[ , Apples, Carrots] `and `[4.99, 6.99, 5.99]` and the first indice of the "cart" array is left empty. Thank you all for the feedback. – E.DelCarlo Apr 09 '19 at 20:21

1 Answers1

0

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