File java code First method: lineLengthControl: This method should check whether
ID: 3891846 • Letter: F
Question
File java code First method: lineLengthControl: This method should check whether each line in the txt file is more than 80 characters, if it exceeds 80 characters, add the error to the correct error code. (error code: "LINE_MUST_BE_SHORTER_THAN") Second method: line With Only Whitespace Control: This method checks whether any line in the txt file consists of only spaces and inserts the line numbers of the incorrect lines together with the corresponding error code. (error code: "LINE_WITH_ONLY_WHITE_SPACE") The third method is:check space at the end of a line This method checks whitespace at the end of the lines in the txt file and adds the line numbers of the incorrect lines together with the corresponding error code to the corresponding place. (error code: "NO_TRAILING_WHITE_SPACE").
Explanation / Answer
public static void lineLengthControl(String filename, String[] errorCode){
try {
FileInputStream fin = new FileInputStream(filename);
Scanner fc = new Scanner(fin);
int count = 0;
while (fc.hasNextLine()){
String line = fc.getLine();
if (line.length() > 80){
errorCode[count] = Integer.toString(count) + ":" + "LINE_MUST_BE_SHORTER_THAN";
}
count++;
}
fc.close();
}
catch (Exception e){
e.printStackTrace();
}
}
public static String lineWithOnlyWhitespaceControl(String filename, String[] errorCode){
try {
FileInputStream fin = new FileInputStream(filename);
Scanner fc = new Scanner(fin);
while (fc.hasNextLine()){
String line = fc.getLine();
if (line.length() > 0){
int whiteSpace = 1;
for (int i = 0; i<line.length(); i++){
if (line.charAt(i) != ' '){
whilteSpace = 0;
break;
}
}
if (whiteSpace == 1){
errorCode[count] = Integer.toString(count) + ":" + "LINE_WITH_ONLY_WHITE_SPACE";
}
}
count++;
}
fc.close();
}
catch (Exception e){
e.printStackTrace();
}
}
public static String lineWithNoTrailingWhitespaceControl(String filename, String[] errorCode){
try {
FileInputStream fin = new FileInputStream(filename);
Scanner fc = new Scanner(fin);
while (fc.hasNextLine()){
String line = fc.getLine();
if (line.length() > 0){
if (!line.endsWith(" "){
errorCode[count] = Integer.toString(count) + ":" + "NO_TRAILING_WHITE_SPACE";
}
}
count++;
}
fc.close();
}
catch (Exception e){
e.printStackTrace();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.