1

im a beginner in Java, and i have a problem to do: the problem prompts the user 5 times to enter, the Name of a stock then the Share Price then the number of shares owned and we should calculate the sum of all the stock values, i wrote it using only two prompts using a loop, but my issue is that, in the second prompt time the loop Skips the String input for the second Name of stock instead of promting...bellow is the code:

import java.util.Scanner;
public class test1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double sharePrice =0,stockPrice = 0, temp = 0 ;
        int i = 0;
        double sum=0;
        String name;
        while (i < 2) {
            System.out.println("Enter the Name of the Stock "); 
            name = input.nextLine();

            System.out.println("Enter the Share price ");
            sharePrice = input.nextDouble();

            System.out.println("Enter the number of owned shares");
            int numberOfshares = input.nextInt();
            stockPrice = sharePrice * (double)(numberOfshares);

            sum += stockPrice;

            i++;
        }
        System.out.println("the total stockprice owned is: " + sum );
    }
}

And this is the output i get:

  Enter the Name of the Stock 
  nestle
  Enter the Share price 
  2
  Enter the number of owned shares
  4


  Enter the Name of the Stock 
  Enter the Share price

What makes the input skip during the second loop?

2
  • It may be due to your need to handle the end of line token after getting nextInt() or nextDouble(). Consider putting in a nextLine() Commented Aug 25, 2012 at 14:37
  • 1
    Please use a consistent and logical indent for code blocks. Doing so makes the flow of code easier to understand. This is especially important for things like conditional or loop statements. Commented Aug 25, 2012 at 14:39

3 Answers 3

3

Again as per my comment the problem is that your code doesn't handle the End Of Line (EOL) token when calling nextInt() or nextDouble().

The solution is to use input.nextLine() after getting your int and double in order to swallow the EOL token:

sharePrice = input.nextDouble();
input.nextLine();  // add this 

System.out.println("Enter the number of owned shares");
int numberOfshares = input.nextInt();
input.nextLine();  // add this 
stockPrice = sharePrice * (double)(numberOfshares);
Sign up to request clarification or add additional context in comments.

1 Comment

i tried a different trick and it worked the same as yours: so i changed the input.nextLine() to input.next() However could you explain what happens when you mean by Swallow the End of Line?? i assume the input.nexLine takes the Line as a String input ? right? while the .next() works since the String input is not filled??
3

The problem is that the last time you call

int numberOfshares = input.nextInt();

in the first iteration of the loop your first passes a carriage return as the next stock name. You could instead use:

double sharePrice = Double.parseDouble(input.nextLine());

and

int numberOfshares = Integer.parseInt(input.nextLine());

1 Comment

No need to use Integer.parseInt(...) since Scanner#nextInt()` will parse the int input just fine, although this solution will work 1+
0

You should readout the newline characters after reading the share price and number of shares:

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    double sharePrice = 0, stockPrice = 0;
    int i = 0;
    double sum = 0;
    String name;
    while (i < 2)
    {
        System.out.println("Enter the Name of the Stock ");
        name = input.nextLine();
        System.out.println("Enter the Share price ");
        sharePrice = input.nextDouble();
        // Read out the newline character after the share price.
        input.nextLine();
        System.out.println("Enter the number of owned shares");
        int numberOfshares = input.nextInt();
        // Read out the newline character after the number of shares.
        input.nextLine();
        stockPrice = sharePrice * (double) (numberOfshares);
        sum += stockPrice;
        System.out.println(String.format("Name: %s, Price: %f, Shares: %d, Total: %f",
                                         name,
                                         sharePrice,
                                         numberOfshares,
                                         stockPrice));
        i++;
    }
    System.out.println("the total stockprice owned is: " + sum);
}

See the lines with comments above:

1 Comment

i tried a different trick and it worked the same as yours: so i changed the input.nextLine() to input.next() However,could you explain what happens when you mean by Swallow the End of Line?? i assume the input.nexLine takes the Line as a String input ? right? while the .next() works since the String input is not filled?? while (i < 3) { System.out.println("Enter the Name of the Stock "); name = input.next(); System.out.println("Enter the Share price "); sharePrice = input.nextDouble(); @Hover

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.