Hey guys and girls, first post here been reading for a few days but I am pretty new to java and I have to edit a class to import a file into an array. The class initially defined the array within itself. So I made my changes and I am getting a symbol not found on lines 75 and 82, in reference to inFile object of the Scanner class. Any help would be greatly appreciated, especially format criticism as I would like my future programs to be readable.
//package ChargeAccount;
import java.util.*;
import java.util.Scanner;
import java.io.*;
public class ChargeAccount {
private int search;
private String fileName;
private int indexPos;
private boolean check;
private int sizeCount;
private boolean fileExist;
private int[] accounts; //modify the original array into a non final
/*= {
5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4851002 };*/
public ChargeAccount(){ //no arg constructor, sets search to -1 and sends toresults method
search = 0;
}
public ChargeAccount(int search, String fileName){
//I need to define a public array here dependant on a file identified by user
this.fileName = fileName;
Scanner keyboard = new Scanner(System.in);
//Used for debugging System.out.println(""+accounts.length);
this.search = search;
check = returnResults(search);
if(!check){
System.out.println("The account "+search+" was not found");
}
else {
System.out.println("The account "+search+" was found at position: "+indexPos);
}
}
private boolean returnResults(int search){
if(search == 0){
return false;
}
else {
Arrays.sort(accounts);
indexPos = Arrays.binarySearch(accounts, search);
if(indexPos != -1)
return true;
else
return false;
}
}
//create a method that does the file validation and array creation fileToArray()
public void fileToArray(String fileName)throws IOException{
this.fileName = fileName;
Scanner keyboard = new Scanner(System.in);
//first i need to identify how many elements will be in the file
//ill use a do while with nested if/else
do{
File file = new File(fileName);
if(!file.exists()){
fileExist = false;
System.out.println("File "+fileName+" does not exist, please enter a different file name with extension(*.txt)");
//Consume the next line into fileName
fileName = keyboard.nextLine();
}
else{
Scanner inFile = new Scanner(file);
fileExist = true;
}
}while(!fileExist);
//Now that we have a validated file, lets find how many elements are within
//ill use a while to determine this
while(inFile.hasNext()){
sizeCount++;
}
//now we've obtained the sizeCount, we'll use that to initialize the account array
accounts = new int[sizeCount];
//thats done, lets add our elements
for(int i = 0; i < sizeCount; i++){
accounts[i] = inFile.nextInt();
}
}
}
Thanks!