Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this program that compiles but i dont know why it is not reading the file that i am sending is there something wrong

I have this program that compiles but i dont know why it is not reading the file that i am sending is there something wrong with my logic here? or why am i always getting "THE FILE COULD NOT BE FOUND" error given to me?

import java.util.*;

import java.io.*;

class MyTest{

public static int[] POIcounter(Scanner scnr){

int[] quantity = new int[1]; //we create an Integer array of size 1 (this array will hold the quantity of POI's)

int Persons = 0;//holds the quantity of POI's

int POIThreatLevel = 0;

while(scnr.hasNext()){ //while the file has a next line then execute

String[] line = scnr.nextLine().split(" ");//each split gets saved into an index of the String array

if(line.length == 1){

POIThreatLevel = Integer.parseInt(line[0]);

if(POIThreatLevel > 0 && POIThreatLevel < 6){ //if the scanner reads an integer less than 6 then that is the end of a POI object

Persons ++;

}

}

}

//Assign quantity to index

quantity[0] = Persons;

scnr.close();//close scanner

return quantity;//return how many persons are found in the file

}

public static void populatePersonOfInterest(Scanner scnr1, POI[] PersonOfInterestArray){ //takes an BoxType array, Basketball array as a parameters (void because we're just populating) and takes Scanner as parameter as well

int PersonOfInterestCounter = 0;

int longID = 0;//long ID

String name = "a"; //name

int ThreatLevel = 0; //threat level

while(scnr1.hasNext()){

//assuming that the File will always have the threat level in the third line of every person of interest object that we want to create and they will be in the same order then we can do this

String longIDLine = scnr1.nextLine();

String nameLine = scnr1.nextLine();

String ThreatLevelLine = scnr1.nextLine();

longID = Integer.parseInt(longIDLine); //take in the value for the long integer id

name = nameLine; //set the String of where the name of the person should be and assign it to the string type name

ThreatLevel = Integer.parseInt(ThreatLevelLine); //take in the third line or the line that will have the threat level of the person of interest which is the last thing we read for every person

PersonOfInterestArray[PersonOfInterestCounter] = new POI(longID, name, ThreatLevel); //populate one by one every person of interest object that we have

PersonOfInterestCounter++;//increment the count (the index of our array)

//reset values

longID = 0;

name = "a";

ThreatLevel = 0;

}

scnr1.close();

}

public static POI createLinkedList(POI[] arr){ //create the linked list from the array of POI's

if (arr.length==0){

return null;

}

POI h = arr[0];

POI temp = h;

System.out.println(temp);

for (int i=1; i

temp.next= arr[i];

System.out.println(temp.next);

temp=temp.next;

}

return h;

}

public static void main (String[]args) throws FileNotFoundException{

try {

Scanner file = new Scanner(new File("NSA.txt")); // Scanner will scan the NSA.txt file (look for Persons of interest)

POI[] PersonOfInterestArray = new POI[POIcounter(file)[0]];

Scanner file3 = new Scanner(new File("NSA.txt"));

populatePersonOfInterest(file3, PersonOfInterestArray);

createLinkedList(PersonOfInterestArray);

}

catch(Exception eee1) {

System.out.println("THE FILE COULD NOT BE FOUND");

}

/*

POI n1 = new POI();

n1.ID = 389114 ;

n1.Name = "Dan Carte";

n1.ThreatLevel = 5;

POI n2 = new POI();

n2.ID = 399012;

n2.Name = "Prio Notim";

n2.ThreatLevel = 0;

n1.next = n2;

POI n3 = new POI();

n3.ID = 685015;

n3.Name = "Hons Nohish";

n3.ThreatLevel = 3;

n2.next = n3;

POI temp = n1;

while(temp != null){

temp.printNode();

temp = temp.next;

}

*/

}

}

//this is the object that i created for the POI

public class POI{

int ID;

String Name;

int ThreatLevel;

POI(){

}

POI(int ID, String Name, int ThreatLevel){

this.ID = ID;

this.Name = Name;

this.ThreatLevel = ThreatLevel;

}

POI next;

public void printNode(){

System.out.println(this.ID);

System.out.println(this.Name);

System.out.println(this.ThreatLevel);

}

}

//This is the file that the program should read

389114 Dan Carte 5 399012 Prio Notim 0 685015 Hons Nohish 3 179318 Diran Egrac 2 284139 Osama Laden 5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions