PLEASE HELP This is my code for a file reading assignment... i have to choose an
ID: 3907248 • Letter: P
Question
PLEASE HELP
This is my code for a file reading assignment...
i have to choose any program from this semester that accesses a file but that does not use try-catch blocks to handle file not found exceptions. Add a try and catch block to handle the exception. You can decide what the handler does. It could simply print a message – such as in a JOptionPane error message dialog box or it could allow the user to enter the correct file name.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package weeklyassignment6;
import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;
/**
*
* @author Andrew
*/
public class WeeklyAssignment6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)throws Exception {
File inFile = new File("numbers.txt");
Scanner fileReader = new Scanner (inFile);
PrintWriter fileWriter = new PrintWriter("results.txt");
int[]numbers=getNumbersList(fileReader);
int hightestIndex=findHightestIndex(numbers, 0);
fileWriter.println("The largest number was " + numbers[hightestIndex] +" which was found on line " + hightestIndex );
fileWriter.close();
System.out.println("The largest number was " + numbers[hightestIndex] +" which was found on line " + hightestIndex );
}
public static int[] getNumbersList(Scanner file) {
int[] numbers = new int[100];
while (file.hasNext()) {
for (int i = 0; i < numbers.length; i++) {
numbers[i] = file.nextInt();
}
}
return numbers;
}
public static int findHightestIndex (int[] someArray, int start) {
int hightestIndex = start;
int hightestNumber = someArray[start];
for (int i = start; i < someArray.length; i++) {
if (someArray[i] > hightestNumber) {
hightestNumber = someArray[i];
hightestIndex = i;
}
}
return hightestIndex;
}
}
Explanation / Answer
If you have any doubts, please give me comment...
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package weeklyassignment6;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;
/**
*
* @author Andrew
*/
public class WeeklyAssignment6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args){
try {
File inFile = new File("numbers.txt");
Scanner fileReader = new Scanner(inFile);
PrintWriter fileWriter = new PrintWriter("results.txt");
int[] numbers = getNumbersList(fileReader);
int hightestIndex = findHightestIndex(numbers, 0);
fileWriter.println(
"The largest number was " + numbers[hightestIndex] + " which was found on line " + hightestIndex);
fileWriter.close();
System.out.println(
"The largest number was " + numbers[hightestIndex] + " which was found on line " + hightestIndex);
} catch (FileNotFoundException e) {
System.out.println("Unable to open file");
}
}
public static int[] getNumbersList(Scanner file) {
int[] numbers = new int[100];
while (file.hasNext()) {
for (int i = 0; i < numbers.length; i++) {
numbers[i] = file.nextInt();
}
}
return numbers;
}
public static int findHightestIndex(int[] someArray, int start) {
int hightestIndex = start;
int hightestNumber = someArray[start];
for (int i = start; i < someArray.length; i++) {
if (someArray[i] > hightestNumber) {
hightestNumber = someArray[i];
hightestIndex = i;
}
}
return hightestIndex;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.