Hi,

So for an assignment i had to read input from a file and then output it.
The information in the file was in the following format: 1 line with a stock symbol and the next line with the quantiti bought.
eg of a file that we'd use
.ab
15
mox
16
.fy
8
mixe
34

And then i have to output the count and total price of each symbol.
But somehow when i run it, it only reads the first 3 sets of data - there are only 3 lines of data instead of 4 for this example.
eg. output would be:
Enter filename ... lab5data.txt
00000 54.05
00002 Symbol mox does not exist
00003 10.70
00004 Symbol mixe does not exist

And my code would not out put the last line "00004 Symbol mixe does not exist"

import java.util.Scanner;
import java.io.PrintStream;
import type.lib.Stock;
import java.io.File;
import java.text.DecimalFormat;

public class Check05B
{
    public static void main(String[] args) throws java.io.IOException
    {
        Scanner input = new Scanner(System.in);
        PrintStream output = new PrintStream(System.out);
        output.print("Enter filename ... ");
        String fileName = input.nextLine();
        Scanner fileInput = new Scanner(new File(fileName));
        double totalValue = 0;
        int count = 00000;
        String symbol = fileInput.next();
        int quantity = fileInput.nextInt();
        while (fileInput.hasNext())
        {
            Stock myStock = new Stock(symbol);
            double total = myStock.getPrice() * quantity;
            String aFormatCounter = new DecimalFormat("00000").format(count);
            if (myStock.getName() == null)
            output.println(aFormatCounter + " Symbol " + symbol + " does not exist!");
            else
            output.printf("%s %.2f%n", aFormatCounter, myStock.getPrice());
            totalValue += total;
            symbol = fileInput.next();
            count++;
            quantity = fileInput.nextInt();
            count++;
        }
        output.printf("Total value = %.2f%n", totalValue);
        fileInput.close();
    }
}

Could anybody please help? How can i make it read all the lines? Thanks!!

Never mind, i found the answer somewhere else.
i had to have the reading of symbol and quantity at the top of my loop.
so my loop would be:

    while (fileInput.hasNext())
    {
        String symbol = fileInput.next();
        int quantity = fileInput.nextInt();
        Stock myStock = new Stock(symbol);
        double total = myStock.getPrice() * quantity;
        String aFormatCounter = new DecimalFormat("00000").format(count);
        if (myStock.getName() == null) {
            output.println(aFormatCounter + " Symbol " + symbol + " does not exist!");
        }
        else {
            output.printf("%s %.2f%n", aFormatCounter, myStock.getPrice());
        }
        totalValue += total;
        count = count + 2;
    }

thanks to Scary Wombat from stackoverflow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.