Jump to content
import java.io.*; //Importing to read from file.import java.util.Scanner; //Importing Scanner class.import java.text.DecimalFormat; //Importing DecimalFormat class.public class StockAssessment    {public static void main(String[] args) throws IOException    {        System.out.println("");        // Setting up DecimalFormat.        DecimalFormat money = new DecimalFormat("$###.##");        // Setting up the scanner.        Scanner keyboard = new Scanner(System.in);         /*          Prompting for the file name and initalizing         it to fileName.          */           System.out.print("Name of the file? ");          String fileName = keyboard.nextLine();             //Opening the file.             File theFile = new File(fileName);             Scanner inputFile = new Scanner(theFile);             //Making sure the file exists.             if (!theFile.exists())             {                    System.out.println("========================");                     System.out.println("No transactions processed.");                    System.exit(0);             }            //Getting data from stock file.           while (inputFile.hasNext())            {            String str = inputFile.nextLine();            int shareAmount = inputFile.nextInt();            double purchasePrice = inputFile.nextDouble();            double purchCommissRate = inputFile.nextDouble();            double salePrice = inputFile.nextDouble();            double saleComiss = inputFile.nextDouble();            }            inputFile.close();      } }
 
Basically, I've got two text files in the same folder that my source code is in, and when i run the program, it will only find one, and throw the following exceptions at me, that i have no idea what they mean, or how to fix them, any help would be awesome
 
-------------------------------------------
Name of the file? StockData.txt
Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:909)
        at java.util.Scanner.next(Scanner.java:1530)
        at java.util.Scanner.nextInt(Scanner.java:2160)
        at java.util.Scanner.nextInt(Scanner.java:2119)
        at StockAssessment.main(StockAssessment.java:46)
Press any key to continue . . .

---------------------------------------

Link to comment
https://linustechtips.com/topic/323591-java-finds-one-file-but-not-another/
Share on other sites

Link to post
Share on other sites

 

Basically, I've got two text files in the same folder that my source code is in, and when i run the program, it will only find one, and throw the following exceptions at me, that i have no idea what they mean, or how to fix them, any help would be awesome

 

It's finding the file fine. The problem is that you're calling nextInt on the scanner and the next thing in the file isn't an int.

 

Also please use code tags.

1474412270.2748842

Link to post
Share on other sites

@fizzlesticks I've found out that you can't do what i'm trying to do, but now I have to take all of the data in as a string, but do you know if parseInt and parseDouble can get their data from specific lines of a string?

You can use the string.split method to get an array of all the strings then call parseInt on an element of the array.

1474412270.2748842

Link to post
Share on other sites

@fizzlesticks It's an assignment for school, and we haven't covered arrays yet, do you know of any other way of doing this?

The first way you were doing it should work fine as long as the file is formatted correctly.

1474412270.2748842

Link to post
Share on other sites

DELL: Dell Inc
125    25.567    0.025    28.735    0.025
MSFT: Microsoft
34.1    -15.75    0.012    15.90    0.013
GOOG: Google
56.5    58.125    0.032    67.975    0.030
IBM: IBM Corp
87.3    8.875    0.015    7.500    0.020
DTLK: DataLink
345    23.250    0.055    25.750    0.050
CSCO: Cisco
90    14.570    1.025    16.890    1.024
INTC: Intel
89.1    78.120    0.042    99.355    0.042
BGUS: Bogus Corp
0    25.567    0.012    25.678    0.023

Link to post
Share on other sites

All of the numbers are doubles, so that inputFile.nextInt needs to be changed to nextDouble.

Also after reading all the numbers in a line you need to eat the newline character at the end, so at the end of the loop check if there is more input and if there is, do another inputFile.next() to move to the next line.

1474412270.2748842

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×