Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help me find the error here.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help me find the error here.

    so this is my code. I am attempting to read input from a .txt file that has data about earthquakes. Below is the actual data. My code should store each occurence of an earthquake as an ArrayList object. I have a class called Earthquake. The code of the class is below too. I the error I get is also copied below. Could someone help me figure out what is wrong? I know the error is in the EarthquakeDemo

    Here is the error I get when I run the EarthquakeDemo code
    ----jGRASP exec: java EarthquakeDemo

    time latitude longitude depth mag place
    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.nextDouble(Scanner.java:2456)
    at EarthquakeDemo.main(EarthquakeDemo.java:20)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    earthquakeUpdated.txt
    time latitude longitude depth mag place
    2013-10-01T20:44 38.834 -122.802 2.3 2.5 California
    2013-10-01T20:17 62.254 -150.816 46 2.6 Alaska
    2013-10-01T19:41 55.7806 -34.7243 12.03 5.2 Reykjanes Ridge
    2013-10-01T19:26 36.1288 -97.6639 5.01 3 Oklahoma
    2013-10-01T17:51 38.8343 -122.795 2.5 2.7 California

    EARTHQUAKE CLASS

    public class Earthquake
    {
    private String time;
    private double latitude;
    private double longitude;
    private double depth;
    private double magnitude;
    private String place;
    /* +Earthquake (lat: double, long: double): void
    +setTime(t: String): void
    +setDepth(d: double) void
    +setMagnitude(m: double): void
    + setPlace(p: String): void
    + getPlace(): String
    + getMagnitude(): double
    +toString():String*/

    public void Earthquake(double lat, double longi)
    {
    latitude = lat;
    longitude = longi;
    }
    public void setTime(String t)
    {
    time = t;
    }
    public void setDepth(double d)
    {
    depth=d;
    }
    public void setMagnitude(double m)
    {
    magnitude = m;
    }
    public void setPlace(String p)
    {
    place = p;
    }
    public String getPlace()
    {
    return place;
    }
    public double getMagnitude()
    {
    return magnitude;
    }
    public String toString()
    {
    String stat= "An earthquake in "+place+ " with magnitude of "+ magnitude+ " occurred at "+time;
    return stat;
    }
    }

    EarthquakeDemo
    import java.util.*;
    import java.io.*;
    public class EarthquakeDemo
    {
    public static void main(String [] args)throws IOException
    {
    ArrayList<Earthquake> list = new ArrayList<Earthquake>();
    File file = new File("earthquakeUpdated.txt");
    Scanner infile = new Scanner(file);

    String ignore = infile.nextLine();
    System.out.println(ignore);
    int i=0;
    while(infile.hasNext())
    {
    Earthquake temp = new Earthquake();
    temp.setTime(infile.next());
    temp.Earthquake(infile.nextDouble(),infile.nextDou ble());
    temp.setDepth(infile.nextDouble());
    temp.setMagnitude(infile.nextDouble());
    temp.setPlace(infile.nextLine());

    list.add(i, temp);
    i++;
    }
    infile.close();
    for(int x =0; x<list.size();x++)
    {
    System.out.println(list.get(x).toString());
    }

    }
    }


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Help me find the error here.

    Hello.
    I executed your code in ubuntu. It worked without any exceptions.
    Please try to run again.

    Syed.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Help me find the error here.

    Quote Originally Posted by koolkirtzz View Post
    Could someone help me figure out what is wrong? I know the error is in the EarthquakeDemo

    time latitude longitude depth mag place
    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.nextDouble(Scanner.java:2456)
    at EarthquakeDemo.main(EarthquakeDemo.java:20)
    Please, note that Scanner is locale-aware. It interprets numeric values according to the configured (or default) locale.
    I tried your code and since my system has the Italian locale as default, I got InputMismatchException like you.
    To fix the problem (and make the code portable) I just added:

    infile.useLocale(Locale.ENGLISH);

    Always consider these questions about locales!
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. The Following User Says Thank You to andbin For This Useful Post:

    GregBrannon (December 5th, 2013)

Similar Threads

  1. find an error
    By nehaverma in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 5th, 2013, 01:26 AM
  2. find the error
    By nidhi goyal in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 1st, 2013, 07:48 AM
  3. Please Help me Find the Error
    By jugalrockz in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 18th, 2012, 02:31 AM
  4. error :cannot find symbol
    By iswan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 1st, 2011, 08:26 AM
  5. Need help, cannot find error
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 5th, 2010, 05:26 PM