Question

In: Computer Science

Write a static method called max that creates a Scanner and connects to a file “data.txt”....

Write a static method called max that creates a Scanner and connects to a file “data.txt”.

Use an exception handling  mechanism of your choice.

The method should read the file until the end of time and consume tokens that could be integer,  double or string type.

It should count and return the number of words that are not integer or doubles.

Solutions

Expert Solution

Explanation:-

System reads the file input from data.txt until the end of file . If file is Not found an exception of FileNotFoundException is thrown and Scanner object reads the file line by line untill the stream has next Line. and append that to a single String Builder object then the String Builder object is converted to String and splited into individual String on the basis of "Space" String tokens are generated then each token needs to be tested against if its a word or Integer or Double. Only Words are needed to be count.

Another function checkIntegerDouble() is used for that purpose. That function try to convert String to Integer / Double if successful with no NumberFormatException means it is a Integer/Double so it can be ignored while counting words in the file. then finally count of words is displayed.

Code is a follows:-

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static boolean checkIntDouble(String s){ // Check if string is a number
try{
Double.parseDouble(s);
return true;
} catch(NumberFormatException e){
return false;
}
}
public static void max(String fileName) throws FileNotFoundException { // Exception thrown if file is not found
int count =0;
File file = new File(fileName);
Scanner sc= new Scanner(file);
StringBuilder ss= new StringBuilder();
while(sc.hasNextLine()){
ss.append(sc.nextLine());
}
System.out.println(ss);
String s =ss.toString();
String [] data = s.split(" ");
for (int i=0; i<data.length; i++) {
if(!checkIntDouble(data[i])) // Check for Strings only
count++;
}
System.out.println("No of words that are not Integer an double are " +count); //printing count
  
}
public static void main(String args[]) throws Exception{
max("data.txt"); //function callled
}
}

-------------------

" File used is : data.txt"

-------------------

Output:-


Related Solutions

Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Write a program that creates a file called "data.dat" in the current directory. Prompt the user...
Write a program that creates a file called "data.dat" in the current directory. Prompt the user for five numbers, and write them, one at a time, on both the screen and into the file. Close the file, then open it again for reading only, and display the contents on the screen. Handle error conditions that may occur. Please Need this to be done in PERL. Thanks
Write a class file called lastname_digits.java. In it, write a method to compute and return the...
Write a class file called lastname_digits.java. In it, write a method to compute and return the total instances where the sum of digits of the array equals to the target. Write a demo file called lastname_digits_demo.java. Create an array of random 10 integers between 301 and 999. Also create an integer variable called target that is equal to a value between 5 and 10. Print the answer within this file. Example: If five of the 20 integers are equal to...
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Write a complete Java program that does the following: Open an input file named data.txt that...
Write a complete Java program that does the following: Open an input file named data.txt that consists of a series of unknown number of integers. If data.txt does not exist, give an appropriate error message and terminate the program. Define a constant MAX of value 100 and create an array of size MAX to hold items from the input file. Make sure your program will not generate ArrayIndexOutOfBounds exception. Open an output file named result.txt and write the array elements...
Write a static method called generateSubstrings that adds all subsets of the letters in its first...
Write a static method called generateSubstrings that adds all subsets of the letters in its first argument to the ArrayList that is its second argument. For example,
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT