Print the file on the BlueJ console Java Filename: input.txt Number of lines: 5 Longest Line: 10 Total number of characters: 33 Asterisk character found at 3,2
I need help
Print the file on the BlueJ console Java
Filename: input.txt
Number of lines: 5
Longest Line: 10
Total number of characters: 33
Asterisk character found at 3,2
xxxxxxxxx
xxxxxxx
xxx*xxxxxx
xx
xxxxx
Work done so far (my code)
// we need to import this library for classes to read and write files
import java.io.*;
import java.util.Scanner;
import java.io.FileReader;
/**
* Write a description of class FileStats here.
*
* @author (Tanishq Verma)
*/
public class FileStats
{
/**
* Constructor for objects of class FileStats
*/
public FileStats()
{
}
public static void main(String args[]) {
// Assignment 5 Part A
// print out a message to help show where we are at in the console
System.out.println(
"\n\nFileStats program running!!");
// create a scanner object to read from the keyboard
Scanner keyboard = new Scanner(System.in);
// Get the filename from user input
System.out.print("Enter the filename: ");
String filename = keyboard.nextLine();
// Create a new File object
File file = new File(filename);
// If the file does not exist, default to "input.txt"
if (!file.exists()) {
// Display an error message.
System.out.println(
"Sorry that file doesn't exist, tring input.txt");
filename = "input.txt";
// try creating the File again using input.txt
file = new File(filename);
}
System.out.println("Reading in file: " + filename);
// use try to check for errors
try {
// create a new Scanner object to read the file
Scanner inputFile = new Scanner(file);
int lineCounter=0;
int strl=0;
int max=0;
int xC=0;
int yC=0;
while (inputFile.hasNext()) {
String str = inputFile.nextLine();
int lineLength=str.length();
lineCounter++;
for (int x=0; x < lineLength; x++){
char next=str.charAt(x);
if(next=='*'){
xC=x-1;
yC= lineCounter;
}
}
//if(strl > max){
// max=strl;
// }
//count++;
// Assignment 5 - Part A
// For now, we are just printing it out, you need
// to count number of lines etc.!
System.out.println(str);
System.out.println("the number of line is "+ );
System.out.println("the number of character "+ );
System.out.println("the number of Longest Line is "+ );
System.out.println("the number of Asterisk character fount line "+ );
}
// close the file when done.
} catch (FileNotFoundException e) {
System.out.println("Error reading file: " + e.toString());
System.exit(1);
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 4 images