dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Beginner in java

Hybrid View

  1. #1
    Join Date
    Aug 2012
    Posts
    1

    Beginner in java

    Hi Guys,
    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

  2. #2
    Join Date
    Sep 2012
    Posts
    1

    Smile read dis

    Try using methods
    a method for writting the input
    and a method for doing the calculations den call those methods by creating objects of this class it sholud work...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles