Results 1 to 5 of 5
  1. #1
    IrishWhiskey is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Please review while loop, need fresh eyes

    Hello,

    I'm new to programming, hence me being in this section. This is a class assignment that I can't seem to get to work. It reads the first few lines then does the loop once and gives me the this error

    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 SalesProcessor1.main(SalesProcessor1.java:55)

    This is my code and below will be the file (very small) its reading from

    /**

    This program will prompt the user for a file using a dialogue box. It will then read the specified file
    and create a table. The table will produce data relevant to revenue and profit. At the end the program
    will open a dialogue explaining which product sold the best with corresponding revenues and sales.

    @Thomas Sullivan

    */

    import java.io.*; // Input output classes
    import javax.swing.JOptionPane; //for dialog boxes
    import java.util.Scanner; //needed to for Scanner class
    import java.text.DecimalFormat; //needed for decimal formatting


    public class SalesProcessor1
    {
    public static void main(String[] args) throws IOException
    {

    double topRevenues = 0.0;//used to store the revenue amount of the top selling product
    double topProfit = 0.0; //used to store the profit amount of the top selling product
    String topProduct = "";//used to store the name of the top selling product



    //request input of file name (and location by default)
    String fileName;
    fileName = JOptionPane.showInputDialog("Please enter the name of the file containing the data.");

    // open the input file
    File myFile = new File(fileName);


    Scanner inputFile = new Scanner(myFile);
    //Initialize the above scanner to read from myFile


    String product = inputFile.nextLine();//read the first item in the file


    //display report headers
    //to explain %-15s the "-" aligns it to the left, the "15" gives it 15 char spaces, and the "s" is for string.
    System.out.printf("%-15s %8s %8s %8s %8s %8s %8s\n", "Product", "North", "East", "West", "South", "Revenues", "Profit");
    System.out.printf("%-15s %8s %8s %8s %8s %8s %8s\n", "===============","========" ,"========","========","========","========","==== ====");


    while (!product.equals("END")) //read from the file until "END" is read
    {

    double revenues = 0.0;//used to store the total revenues for a product
    int factory = inputFile.nextInt();//read the factory number

    // Read in the sales for various regions
    double northSales, eastSales, westSales, southSales;

    northSales = inputFile.nextDouble();
    eastSales = inputFile.nextDouble();
    westSales = inputFile.nextDouble();
    southSales = inputFile.nextDouble();

    revenues = northSales + eastSales + westSales + southSales;


    double profit = 0; //used to store the profit for current item


    //TODO: Complete the switch statement to
    // set profit according to the factory
    // For eg. for a factory with 8 percent profitability profit is calculated as
    // profit = revenues * 0.08f;


    switch (factory)//switch on factory number to determine profit percentage
    //and total profit for each product


    {
    case 1:
    profit = revenues * 0.06f;
    break;
    case 2:
    profit = revenues * 0.05f;
    break;
    case 3:
    profit = revenues * 0.07f;
    break;
    default:
    profit = revenues * 0.10f;
    break;


    }


    //determine the current top selling product, its revenues and profit
    if (revenues > topRevenues)

    {
    topProfit = profit;
    topProduct = product;
    topRevenues = revenues;
    }
    //TODO: Complete the following printf statement by providing the
    //the appropriate values for the various columns using variables above
    //Remember the number of parameters should match the number of format specifiers
    //in the printf format statement
    System.out.printf("%-15s %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n", product, northSales, eastSales, westSales, southSales, revenues, profit);

    //dummy line, needed when switching from int to string
    inputFile.nextLine();

    product = inputFile.nextLine();//read the next line in the file
    //before the WHILE loop is started again
    }

    //create a DecimalFormat object
    DecimalFormat formatter = new DecimalFormat("#0.00");



    //output the top selling product, its revenues and profit
    //formatted to 2 decimal places
    JOptionPane.showMessageDialog(null, "Top selling product " + topProduct
    + " sold " + (formatter.format(topRevenues)) + " million dollars with a profit of "
    + (formatter.format(topProfit)) + " million dollars.");


    inputFile.close(); //needed when reading from a file
    System.exit(0);//needed when using Dialogs

    }

    }


    Now here is whats in the file its reading from

    CrossTrainMax
    1 15.1 12.8 3.14 2.3

    AirGlider
    3 12.2 4.6 6.5 8.3

    AquaWalker
    2 3.82 1.75 7.6 6.38

    SuperHike
    1 9 11.2 7.5 8.4

    END


    This is driving me crazy and I'm sure its a simple error, please help!

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Please review while loop, need fresh eyes

    You actually just need to add another
    Java Code:
     inputFile.nextLine();
    after your other one. The reason is because it looks for line separators and
    returns 2 different separators in between each product. Another way to go about this would
    be to use a BufferedReader instead of Scanner.

  3. #3
    IrishWhiskey is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: Please review while loop, need fresh eyes

    I knew I had just been staring at it too long.

    Thank you so much, it worked perfectly.

  4. #4
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Please review while loop, need fresh eyes

    You are welcome! And good luck in the future.

    Best Regards,

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Please review while loop, need fresh eyes

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Human Eyes Only 1.02
    By java software in forum Java Software
    Replies: 0
    Last Post: 11-26-2011, 06:13 PM
  2. For Human Eyes Only
    By java software in forum Java Software
    Replies: 0
    Last Post: 11-25-2011, 07:12 PM
  3. Replies: 0
    Last Post: 09-15-2011, 12:02 PM
  4. Cartoon Eyes applet
    By chielt in forum Java Applets
    Replies: 6
    Last Post: 11-30-2009, 10:46 PM
  5. Java Review
    By toad in forum New To Java
    Replies: 5
    Last Post: 12-08-2007, 01:28 AM

Posting Permissions

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