Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task The task is to read the tokenrom a file and print out three values: the number of tokens that are numbers (double s), thenumber
Task The task is to read the tokenrom a file and print out three values: the number of tokens that are numbers (double s), thenumber of tokens that are not numbers, and the sum of the numbers. For example, if this is the contents of the file being processed 1 2 3 one two three Your program should print out: 6 7 12.6 and nothing else. Assume that the name of the file is data.txt.In Eclipse, the file needs to be in the top directory of the peoject. To open a fie, use a sequence of code like: Scanner in - null; try 1 in ~ new Scanner (new File("data.txt" catch (FileNotFoundException e) System.err.println("failed to open data.txt"); System.exit(l); You will need import statements at the top of your . ava file in onder to use the Scanner and File classes. import java.util.Scanner; import java.io.File; After you are done processing the file, close it in.close(); Reading all the tokens in a file can be done via a loop: while (in.hasNext())f String token- in.next(; One way of converting String to a double is if (new Scanner (token) .hasNextDouble()) System.out.println("It's a double!"; double d = Double. parseDouble (token); 1 else System.out.println("It's not a double
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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