0

I would like to create a shopping list program, after entering the name and price of product enters them into the arrays and then print the entire list of what is wrong with this code?

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

public class List {
    public static void main (String[] args){
        Scanner sc = new Scanner(System.in);
        String [] name = new String[4];
        double [] price = new double[4];

        for (int i =0; i<name.length; i++) {
            System.out.println("name");
            name[i] = sc.nextLine();

            System.out.println("price");
            price[i] = sc.nextDouble();
        }
        System.out.println("your product: " + Arrays.toString(name) + Arrays.toString(price));
    }
}
Vitalii Elenhaupt
  • 7,146
  • 3
  • 27
  • 43

3 Answers3

2

You can resolve it by using nextLine() instead of nextDouble(). However, you need to parse it to a double as your value declared as double :

Scanner sc = new Scanner(System.in);
String [] name = new String[4];
double [] price = new double[4];

for (int i =0; i<name.length; i++) {
    System.out.println("name");
    name[i] = sc.nextLine();
    System.out.println("price");
    price[i] = Double.parseDouble(sc.nextLine()) ;
}
System.out.println("your product: " +  Arrays.toString(name) + Arrays.toString(price));
Saleh Parsa
  • 1,415
  • 13
  • 22
  • This version is better (doesn't has the bug I've mentioned in the other answer you've wrote [here](http://stackoverflow.com/questions/30955555/loop-array-output) and it deserves its upvote), but one question: why have you choosed a different "style" to fix that issue? Just a curiosity, since both versions can work. – Tom Jun 20 '15 at 17:32
1

Scanner#nextLine() reads the whole line.

Scanner#nextDouble() reads the next token and not the whole line.

So for the second iteration of loop nextLine() will read the same line where you placed the token giving you empty in name[1] and error for double[1]=sc.nextDouble().

Docs

The problem can be solved by adding a nextLine() after reading double variable

for (int i =0; i<name.length; i++) {
          System.out.println("name");
          name[i] = sc.nextLine();

          System.out.println("price");
          price[i] = sc.nextDouble();

          if(i<name.length-1)
             sc.nextLine();        //will skip the line
 }

Demo

singhakash
  • 7,891
  • 6
  • 31
  • 65
  • Take some time to read your answer before posting it. – xrisk Jun 20 '15 at 17:10
  • what did I say wrong @RishavKundu – singhakash Jun 20 '15 at 17:11
  • Your answer had lot of formatting issues. It is ok now. – xrisk Jun 20 '15 at 17:11
  • What is the purpose of this check `if(i – Tom Jun 20 '15 at 17:35
  • @Tom for the last iteration it will give a no such element errror because there is no line only the token to skip – singhakash Jun 20 '15 at 17:41
  • No, there is no such exception. You can run your code perfectly without that check. – Tom Jun 20 '15 at 18:30
  • @Tom [check](http://ideone.com/K47Yc0) the last line of output .If user enters only the token(and dint press enter after entring token) in the last line it will get that exception – singhakash Jun 20 '15 at 18:47
  • ok ... then tell he: how should a user be able to enter a token input a console without hitting "return" after that? And no, I don't think that OP overwrites `System.in` with a different `InputStream`, like ideone does. – Tom Jun 20 '15 at 18:57
  • 1
    @Tom ok now I realize my mistake while running from cmd user has to hit enter inorder to proceed to the code but anyway the above code will also work in that case.Thanks for the clarification:) – singhakash Jun 20 '15 at 19:23
  • 1
    Yes, he has to hit enter/return. But I also see now one advantage: _if_ `System.in` points to a different stream than the console, then this check can prevent problems. But I wonder if it is better to change the check to `if(sc.hasNextLine())`, so it will consume the remaining "line delimiter" if there is one and it prevents an exception, if there is none :D. – Tom Jun 20 '15 at 19:28
0

So I was using input.nextDouble() and it was giving me Type mismatch error

public static void main (String[] args)
{
    Scanner input = new Scanner(System.in);
    double[] numbers = new double[4];
    String [] name = new String[4];

    for (int i = 0; i < numbers.length; i++)
    {
        System.out.println("Please enter product price");
        numbers[i] = Double.parseDouble(input.nextLine()); 

        System.out.println("Please enter product name");
        name[i] = input.nextLine();
    }

System.out.println(Arrays.toString(name));
System.out.println(Arrays.toString(numbers));
}
Ritesh Karwa
  • 2,196
  • 1
  • 13
  • 17
  • It is always a good idea to explain what you've changed and why you did that :). – Tom Jun 20 '15 at 17:33