I requested the assignment below to be code in Java. The program threw a few exc
ID: 3762058 • Letter: I
Question
I requested the assignment below to be code in Java. The program threw a few exception errors when I tried to run it. I fixed two of the errors, which can be review in the code section. My changed are bolded and italic in parentheses.Please fix the following program in Java. Verify it will run correctly in a Java IDE. No Rush take your time. Directly below is the exception error I could not figure out, followed by your code from yesterday with my small changes.
Exception Error
----jGRASP exec: java Count
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Count.main(Count.java:7)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
----jGRASP exec: java Count
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Count.main(Count.java:7)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
import java.util.Scanner; //Scanner is in the java,util package (I had to add this scanner package, i wasnt there originally)
public class Count {
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File(args[0]);
Scanner input = new Scanner(file);
int character = 0;
int line = 0;
int word = 0;
while(input.hasNextLine())
{
String s=input.nextLine();
line++;
character +=s.length();
// or character +=s.length()-2;
}
input.close();
input = new Scanner(file);
//int word = 0; (I had to comment this out, because it was already defined above,)
while (input.hasNext()){
input.next();
word++;
}
input.close();
input = new Scanner(file);
String scan = input.nextLine();
System.out.println(scan);
String scan2[] = "Testing 123 ".split(" ");
System.out.println("Number of characters = " + character);
System.out.println("Number of lines = " + line);
System.out.println("Number of words = " + word);
}
}
Coding Instructions
(Count characters, words, and lines in a file) Write a program that will count
the number of characters, words, and lines in a file. Words are separated by
whitespace characters. The file name should be passed as a command-line
argument, as shown in Figure 12.13.
Below is an example of the output. When the program is run it should open Command Prompt. From there I should be able to enter any file name on my computer. After the file name is type and I press Enter, I should see the following
Name of the file has
Number of characters
Number of words
Number of Lines
Example of Command Prompt and output:
Explanation / Answer
public class countnumber{
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File(args[0]);
Scanner input = new Scanner(file);
int character=0;
int line=0;
while(input.hasNextLine())
{
String s=input.nextLine();
line++;
character +=s.length();
// or character +=s.length()-2;
}
input.close();
input = new Scanner(file);
int word = 0;
while (input.hasNext()){
input.next();
word++;
}
input.close();
input = new Scanner(file);
String scan = input.nextLine();
System.out.println(scan);
String scan2[] = "Testing 123 ".split(" ");
System.out.println("Number of lines = " + line);
System.out.println("Number of words = " + word);
System.out.println(scan2);
}
}