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 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
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 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...
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 a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
C programing language A file "data.txt" contains only integers. Write a code to find average of...
C programing language A file "data.txt" contains only integers. Write a code to find average of all values and print the average How would you use execlp function to execute "ps –e –a –l" command char *dt = "The five boxing wizards jump quickly"; write a program to count frequency of each letter, ignore case. Print the letter and frequency of each letter. // 1A: . Ask the user to enter a password string, store it in pass. Password should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT