When I run this program, it cannot find the files I direct it to. I put the two text files into the src folder of the program, and to my understanding all I would have to do to call it is File f = new File("filename.txt")
. But that doesn't work. I also tried using the exact directory inside of File()
but it doesn't work either. The files just contain a name and an amount of money beside them. Any ideas?
import java.io.*;
import java.util.Scanner;
class Donors {
File donor2;
File donor3;
Scanner inD2;
Scanner inD3;
Donors(File d2, File d3){
donor2 = d2;
donor3 = d3;
}
double totalDonations(){
double total = 0;
try{
inD2 = new Scanner(donor2);
while(inD2.hasNext()){
total += inD2.nextDouble();
}
}catch(java.io.FileNotFoundException e){
System.out.println("File can't be found");
}
try{
inD3 = new Scanner(donor3);
while(inD3.hasNext()){
total += inD3.nextDouble();
}
}catch(java.io.FileNotFoundException e){
System.out.println("File can't be found");
}
return total;
}
public void closeFile(){
inD2.close();
inD3.close();
}
}
public class DonorCalculations {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int userInput;
File donor2 = new File("H:\\CSC 191\\Assignment9\\src\\resources\\donor2.txt");
File donor3 = new File("donor3.txt");
Donors dObj = new Donors(donor2, donor3);
do{
System.out.println("SELECT");
System.out.println("1. Total money from donations");
System.out.println("2. Total donation from a individual");
System.out.println("0. Quit");
userInput = input.nextInt();
System.out.println();
switch(userInput){
case 1:
System.out.println(dObj.totalDonations());
break;
case 2:
System.out.println("Enter donor's name: ");
String name = input.next();
//dObj.donorTotal(name);
break;
case 0:
System.out.println("Goodbye!");
break;
}
System.out.println();
}while(userInput != 0);
}
}