Results 1 to 8 of 8
  1. #1
    Kvothe888 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default I can't seem to get my program to work no matter what i try, please help!

    My code:

    import java.io.*;
    import java.util.*;

    public class PortlandWeather {
    public static void main(String[] args)
    throws FileNotFoundException {
    Scanner input = new Scanner(new File("PortlandWeather2011.txt"));
    System.out.printf("STATION DATE PRCP(in) SNOW(in) SNWD(in) TMAX(F) TMIN(F)\n");
    System.out.printf("----------------- ---------- -------- -------- -------- -------- --------\n");
    while (input.hasNextLine()) {

    String station = input.next();
    String date = input.next();
    double prcp = input.nextInt();
    double snow = input.nextInt();
    double snwd = input.nextInt();
    double tmax = input.nextInt();
    double tmin = input.nextInt();

    String year = date.substring(0,4);
    String month = date.substring(4,6);
    String day = date.substring(6,8);
    double PRCP = prcp * 0.00393701;
    double SNOW = snow * 0.0393701;
    double SNWD = snwd * 0.0393701;
    double TMAX = tmax * 0.18 + 32;
    double TMIN = tmin * 0.18 + 32;


    if (SNOW == 9999 * 0.0393701 && SNWD == 9999 * 0.0393701){
    System.out.printf("%s%4s/%s/%s%9.1f ---- ----%9.1f%9.1f\n",station, month, day, year, PRCP, TMAX, TMIN);
    }
    else if (SNOW == 9999 * 0.0393701){
    System.out.printf("%s%4s/%s/%s%9.1f ----9.1f%9.1f%9.1f\n",station, month, day, year, PRCP, SNWD, TMAX, TMIN);
    }
    else if (SNWD == 9999 * 0.0393701){
    System.out.printf("%s%4s/%s/%s%9.1f%9.1f ----%9.1f%9.1f\n",station, month, day, year, PRCP, SNOW, TMAX, TMIN);
    }
    else{
    System.out.printf("%s%4s/%s/%s%9.1f%9.1f%9.1f%9.1f%9.1f\n",station, month, day, year, PRCP, SNOW, SNWD, TMAX, TMIN);}}}}

    Then the out put is:

    STATION DATE PRCP(in) SNOW(in) SNWD(in) TMAX(F) TMIN(F)
    ----------------- ---------- -------- -------- -------- -------- --------
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at PortlandWeather.main(PortlandWeather.java:14)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default Re: I can't seem to get my program to work no matter what i try, please help!

    Show us the content of file PortlandWeather2011.txt

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Kvothe888 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: I can't seem to get my program to work no matter what i try, please help!

    Last edited by Kvothe888; 11-28-2012 at 08:55 PM.

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

    Default Re: I can't seem to get my program to work no matter what i try, please help!

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

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: I can't seem to get my program to work no matter what i try, please help!

    You're not skipping the headers by the look of it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default Re: I can't seem to get my program to work no matter what i try, please help!

    Quote Originally Posted by Tolls View Post
    You're not skipping the headers by the look of it.
    As far as I can see, nor is he skipping the end-of-line after having read a line (so reading the next line goes straight into la-la-land)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: I can't seem to get my program to work no matter what i try, please help!

    Not sure about the next line.
    Scanner works in mysterious ways (OK, not that mysterious).
    hasNextLine() will return true if there's a newline in there, and next() will consume that as whitespace, so I think it'll work assuming there's no blank line at the end.

    Of course, that is largely guesswork and I can't be arsed to actually test it out.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default Re: I can't seem to get my program to work no matter what i try, please help!

    I checked that file again: you're right in reply #5 (if those header lines are part of the file).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Time bogging matter
    By zNinjaa in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-01-2011, 10:31 PM
  2. Replies: 6
    Last Post: 12-10-2010, 08:43 AM
  3. Trying to work out this program
    By KidneyinaCooler in forum New To Java
    Replies: 6
    Last Post: 07-18-2010, 03:14 AM
  4. Error message pops up no matter what?
    By murr0508 in forum New To Java
    Replies: 5
    Last Post: 02-26-2009, 10:10 PM

Posting Permissions

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