user3384292
user3384292

Reputation: 5

Reading from file error

i am having a problem reading from a file that looks like this:

1 7 21 16 44
2 7 21 16 45
3 7 21 16 46
4 7 21 16 47
5 7 21 16 48
6 7 21 16 49
7 7 21 16 50

I need to set each number to a specific variable in my program and then continue scanning for 365 times.each line represents (day of month sunriseHour sunriseMin sunsetHour sunsetMin). I'm trying to read and set these variables so I can perform computations. I'm getting the following error:

Exception in thread "AWT-EventQueue-0" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)

Here is the code I have so far:

 public void graph(Graphics page)
{
             int y = 0; //y-coordinate
    int sunriseX = 101;
    int sunsetX = 101;

    Scanner inputStream = null;
    try
    {
        inputStream = new Scanner("sunData.txt");
    }
    catch(Exception e)
    {
        System.out.println("File not found");
        System.out.println("or could not be opened.");
    }


    for (int i = 365; i <= 365; i++)
    {
        int day = inputStream.nextInt();

        setSunriseHour(inputStream.nextInt());
        setSunriseMin(inputStream.nextInt());
        setSunsetHour(inputStream.nextInt());
        setSunsetHour(inputStream.nextInt());
        inputStream.next();

        switch(sunriseHour)
        {
        case 4:
            y = 120;
            break;
        case 5:
            y = 180;
            break;
        case 6:
            y = 240;
            break;
        case 7:
            y = 300;
            break;
        case 8:
            y = 360;
            break;
        default:
            System.out.println("invalid time");
            break;

        }

        sunrisePoint = y + sunriseMin;
        page.drawOval (sunriseX, sunsetHour, 2, 2); // circle
                 sunriseX += 2;


        switch (sunsetHour)

        {
        case 16:
            y = 420;
            break;
        case 17:
            y = 480;
            break;
        case 18:
            y = 540;
            break;
        case 19:
            y = 600;
            break;
        case 20:
            y = 660;
            break;
        default:
            System.out.println("invalid time");
            break;
        }

        sunsetHour = y + sunsetMin;
        page.drawOval (sunsetX, sunsetHour, 2, 2); // circle
        sunsetX += 2;

    } //ends for loop

Upvotes: 0

Views: 62

Answers (2)

Braj
Braj

Reputation: 46841

Always check for next item before getting it.

while (inputStream.hasNext()) {

    int day = inputStream.nextInt();

    setSunriseHour(inputStream.nextInt());
    setSunriseMin(inputStream.nextInt());
    setSunsetHour(inputStream.nextInt());
    setSunsetHour(inputStream.nextInt());

    ....
}

You can look for other methods of Scanner class as well that is more specific to value type, such as Scanner#hasNextInt()

while (inputStream.hasNextInt()) {
   int day = inputStream.nextInt();
   ...
}

Upvotes: 2

Daniel Allen
Daniel Allen

Reputation: 1

Try using:

        try
        {
          Scanner inputStream = new Scanner("sunData.txt");
        }

instead of:

    Scanner inputStream = null;
        try
        {
          inputStream = new Scanner("sunData.txt");
        }

Upvotes: -1

Related Questions