throws exception won't permit compilation

B

BillJosephson

Hello, I get the general output errors;

--------------------Configuration: <Default>--------------------


public class monthTempsClient
{
public static void main( String [] args ) throws IOException
{
int belowFreezing = 0;
int above100 = 0;
int[] temperatures = new int [30];
int i = 0;
int bigChange = 0;
File tempsFile = new File( "tempsFile.csv" );
Scanner scan = new Scanner( tempsFile );

while (scan.hasNext ())
{
temperatures = scan.nextInt();
System.out.println( "\nReading temp from file: \t\t\t" + i +
"\t\t\t" + temperatures );
i ++;
}

// make month
monthTemps aprilTemps = new monthTemps ( temperatures );
System.out.println( aprilTemps.toString( ) );

// make another month same as first
monthTemps marchTemps = new monthTemps( temperatures );
System.out.println( marchTemps.toString( ) )

// are months equal?
if ( aprilTemps.equals( marchTemps ) )
System.out.println( "\nThe two months have equal temps\n"
);
else
System.out.println( "\nThe two months do not have equal
temps\n" );

// get temperatures
temperatures = aprilTemps.getTemps( );
System.out.println( "\nApril temperatures: " );
for (i = 0; i < temperatures.length; i++)
System.out.println( "\n " + i + " temperature: " +
temperatures );

// change month's temps
marchTemps.setTemps( temperatures );

// are months equal now?
if ( aprilTemps.equals( marchTemps ) )
System.out.println( "\nThe two months have equal temps\n" );
else
System.out.println( "\nThe two months have different temps\n"
);

// get month's biggest temp change
bigChange = marchTemps.biggestChange( );
System.out.println( "\nThe biggest daily change is " + bigChange
);

// how many > .300 hitters on the home team?
belowFreezing = aprilTemps.belowFreezing( );
System.out.println( "\n " + belowFreezing + " temps were under
freezing" );

// how many total hits does the home team have?
above100 = aprilTemps.above100( );
System.out.println( "\nThe month has " + above100 + " days above
100 degrees." );
}
}


I do have a csv file with integer values separated by commas.

Thanks.....
 
B

BillJosephson

My apologies, somehow I left out the error message:


--------------------Configuration: <Default>--------------------
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at monthTempsClient.main(monthTempsClient.java:25)

Process completed.
 
P

Paul Hamaker

Compilation was succesful, the exception happens when running the code.
You forgot to indicate the comma to be the delimiter, so I guess
something like this would solve it
scanref.useDelimiter("\\s*,\\s*");
// use a comma as delimiter, possibly flanked by whitespace.
 
P

Paul Hamaker

Compilation was succesful, the exception happens when running the code.
You forgot to indicate the comma to be the delimiter, so I guess
something like this would solve it
scan.useDelimiter("\\s*,\\s*");
// use a comma as delimiter, possibly whitespace on either side.
 
B

BillJosephson

Paul said:
Compilation was succesful, the exception happens when running the code.
You forgot to indicate the comma to be the delimiter, so I guess
something like this would solve it
scan.useDelimiter("\\s*,\\s*");
// use a comma as delimiter, possibly whitespace on either side.


WOW, that did it!!! Thank you SO MUCH !

Many many many thanks...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top