Question
import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; public class FindCorruptData { /** Reads a set of floating-point values from a string. The first number is an
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FindCorruptData
{
/**
Reads a set of floating-point values from a string. The first number is an integer that indicates how
many floating point numbers follow. Some of the floating point numbers may be corrupt. This program uses exceptions
to count the number of bad floating (i.e. double) numbers and prints them.
*/
public static void printCorruptDataValues()
{
String numbers = "6 3.14 7.25 8,23 6.99 9.2w 6.34";
Scanner console = new Scanner(numbers);
// int numDoubles = console.nextInt(); // this statement is wrong, this will just get first integer number from string.
// Instead we need to get total space seperated values in the string numbers.
String[] words = numbers.split("s+");
System.out.println("total nos including valid/invalid :"+words.length);
//-----------Start below here. To do: approximate lines of code = 5
// Use a for loop to loop through numDoubles double number strings.
// Get the next double number string using the scanner then use Double.parseDouble() to determine
// if the current double number string is corrupt in some way. If it is, print "Corrupt Double Number: "
// followed by the number string.
// Hint: use try{..} catch{..} such that you catch any NumberFormatException and print out the bad double number
// making use of the getMessage() method of the exception class to print the corrupt number string.
Double doubleValue;
for(int i=0;i{;
String nextNumber = console.next();
try
{
doubleValue = Double.parseDouble(nextNumber);
System.out.println(doubleValue);
}
catch (NumberFormatException e )
{
System.out.println( "Corrupt Double Number: " + e.getMessage() );
}
}
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
public static void main(String[] args)
{
printCorruptDataValues();
System.out.println("Expected:");
System.out.println("Corrupt Double Number: For input string: "8,23"");
System.out.println("Corrupt Double Number: For input string: "9.2w"");
}
}
Why is the code giving me this error?
FindCorruptData.java:30: error: ';' expected for (int i=0;i{; 1 error
Step by Step Solution
3.38 Rating (157 Votes )
There are 3 Steps involved in it
Step: 1
JAVA CODE FindCorruptDatajava import javautilScanner public class FindCorruptData Reads a set of flo...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started