I got an error message --------------------------- java.io.FileNotFoundException
ID: 3820937 • Letter: I
Question
I got an error message
---------------------------
java.io.FileNotFoundException: (Result too large)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at listhomework.LinkedList.main(LinkedList.java:348)
--------------------------------------
package listhomework;
//
public class Node<Tier1, Tier2, Tier3> {
//Members with level access
Tier1 category1;
Tier2 category2;
Tier3 category3;
Node<Tier1,Tier2,Tier3>left = null;
Node<Tier1,Tier2,Tier3>right = null;
Node<Tier1,Tier2,Tier3>down = null;
/**
* Constructor to initialize the category member variables by using the arguments
* @param t1 value for category1
* @param t2 value for category2
* @param t3 value for category3
*/
public Node(Tier1 t1,Tier2 t2,Tier3 t3){
// this.category1=t1;
// this.category2=t2;
// this.category3=t3;
category1=t1;
category2=t2;
category3=t3;
}
// cate1 // cate2 // cate3
public Tier1 getCategory1(){ //1
return category1;
}
public void setCategory1(Tier1 category1){//1
this.category1 = category1;
}
public Tier2 getCategory2(){//2
return category2;
}
public void setCategory2(Tier2 category2){//2
this.category2 = category2;
}
public Tier3 getCategory3(){//3
return category3;
}
public void setCategory3(Tier3 category3){//3
this.category3 = category3;
}
//testing
//
//public String toString(){
// return "["+category1+","+category2+","+category3+"]";
//} ...........
}
--------------------------------------
package listhomework;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LinkedList<Tier1, Tier2, Tier3>{
protected Node<Tier1, Tier2, Tier3> head;
// the head of linked lists
protected int size;
// total number of element in the main and sublists
protected int groupingCategory;
protected String category1Label, category2Label, category3Label;
// labels for 3 cates
public LinkedList(){
groupingCategory = 1; // set default
}
public LinkedList(int category){
groupingCategory=category;
}
public LinkedList(File file, int category)throws FileNotFoundException{
groupingCategory=category;
}
public void add(Tier1 value1, Tier2 value2, Tier3 value3){
Node<Tier1,Tier2,Tier3>node = new Node<Tier1, Tier2, Tier3> (value1, value2, value3);
if( head == null){ // empty list
head = node;
}
else{
Node<Tier1, Tier2, Tier3> p = head; // from beginnnnnnng
boolean inserted = false;
while(!inserted){
if(
// if groupingCategory == 1 and node's category also matches or
// if groupingCategory == 2 and node's category also matches or
// .......................3..................................
(groupingCategory==1 && p.getCategory1().equals(node.getCategory1())) || // ??? change static? fixed
(groupingCategory==2 && p.getCategory2().equals(node.getCategory2())) || // node != Node
(groupingCategory==3 && p.getCategory3().equals(node.getCategory3()))) //
// found the correct one (sublist)
{ // go down the sublist to end and add
while(p.down!= null) // stop at last node of sublist
{
p = p.down;
}
p.down = node;
inserted = true;
}
else{
if(p.right != null){ // move to next mainlist
p = p.right;
}
else{
p.right = node; // last.left to new node
node.left = p; // node.right = last node (mainlist)
inserted = true;
}
}
}
}
size ++;
}
public void clear(){
head = null;
size = 0;
}
//delete first node
public void deleteFirst(){
delete(0,0);
// mainlyindex, subindex
}
public void deleteLast(){
int mainIndex=0;
Node<Tier1, Tier2, Tier3> p=head;
//get the last index in main list
while(p.right!=null)
{
p=p.right;
mainIndex++;
}
//reuse the function to delete a node given mainindex and subindex
delete(mainIndex,0);
}
public void delete(int mainIndex, int subIndex){
if(mainIndex < 0 || head == null){
throw new IndexOutOfBoundsException(" the mainly index out of bound " + mainIndex);
}
if(subIndex< 0 || head == null){
throw new IndexOutOfBoundsException(" the sub index out of bound " + subIndex);
}
// ------------------------------
int mi=0; //mainindex
int si=0; //subindex
Node<Tier1,Tier2,Tier3> p=head,prev=null;
while(mi<mainIndex && p.right!=null) //move to right in the main list till we reach the specified mainindex node
{
p=p.right;
mi++;
}
if(mi!=mainIndex) //we could not move so many locations in main list
throw new IndexOutOfBoundsException("Main Index out of bound : "+mainIndex);
while(si<subIndex && p.down!=null) //move to down in the sub list till we reach the specified subindex node
{
prev=p;
p=p.down;
si++;
}
if(si!=subIndex) //we could not move so many locations in sub list
throw new IndexOutOfBoundsException("Sub Index out of bound : "+subIndex);
Node<Tier1,Tier2,Tier3> newnode=p.down,left=p.left,right=p.right;
if(subIndex>0) //not the 1st node in sublist
{
prev.down=newnode; //simply link the previous top node to the next bottom node ignoring the current node
}
else // its the first node in the sublist
{
if(left==null) //there is no left node and its 1st in sublist, so its the head node
{
head=newnode; //update head
}
else
left.right=newnode;
// set up all links from leftside and rightside of mainlist to this newnode from sublist moving up
if(newnode!=null)
{
newnode.left=left;
newnode.right=right;
}
if(right!=null)
right.left=newnode;
}
size--;
}
public String get(int mainIndex, int subIndex, int category){
if(mainIndex<0 || head==null)
throw new IndexOutOfBoundsException("Index out of bound : "+mainIndex);
if(subIndex<0 || head==null )
throw new IndexOutOfBoundsException("Sub Index out of bound : "+subIndex);
int mi=0; //mainindex
int si=0; //subindex
Node<Tier1,Tier2,Tier3> p=head;
while(mi<mainIndex && p.right!=null) //move to right in the main list till we reach the specified mainindex node
{
p=p.right;
mi++;
}
if(mi!=mainIndex) //we could not move so many locations in main list
throw new IndexOutOfBoundsException("Main Index out of bound : "+mainIndex);
while(si<subIndex && p.down!=null) //move to down in the sub list till we reach the specified subindex node
{
p=p.down;
si++;
}
if(si!=subIndex) //we could not move so many locations in sub list
throw new IndexOutOfBoundsException("Sub Index out of bound : "+subIndex);
if(groupingCategory==1)
return p.getCategory1().toString();
else if(groupingCategory==2)
return p.getCategory2().toString();
else if(groupingCategory==3)
return p.getCategory3().toString();
else
throw new IndexOutOfBoundsException("Category number out of bounds: "+category);
}
public void regroup(int groupingCategoryNumber){
if(groupingCategoryNumber > 3 || groupingCategoryNumber < 0){
throw new IndexOutOfBoundsException(" Category out of bound: " + groupingCategoryNumber);
}
if(head==null)
return;
Node<Tier1, Tier2, Tier3> newhead=null,mainnode,subnode,p,right,down;
boolean inserted=false;
groupingCategory=groupingCategoryNumber; //set the new grouping category
for(mainnode=head;mainnode!=null;mainnode=right) //for each node in mainlist
{
right=mainnode.right;
for(subnode=mainnode;subnode!=null;subnode=down) //for each node in sublist
{
down=subnode.down;
subnode.left=subnode.right=subnode.down=null; //clear old values
if(newhead==null)
{
newhead=subnode;
continue;
}
else
{
p=newhead;
inserted=false;
}
while(!inserted)
{
//if grouping is by category 1 and both node's category1 values matches or
//if grouping is by category 2 and both node's category2 values match or
//if grouping is by category 3 and both node's category3 values match
if(
(groupingCategory==1 && p.getCategory1().equals(subnode.getCategory1())) ||
(groupingCategory==2 && p.getCategory2().equals(subnode.getCategory2())) ||
(groupingCategory==3 && p.getCategory3().equals(subnode.getCategory3())))
//found the correct sublist
{
//go down the sublist to end and add
while(p.down!=null) //stop at the last node of sublist
{
p=p.down;
}
p.down=subnode;
inserted=true;
}
else
{
if(p.right!=null) //move to next item in mainlist
p=p.right;
else //reached the end of main list
{
p.right=subnode; //link the last nodes right to new node
subnode.left=p; //link the new nodes right to last node in mainlist
inserted=true;
}
}
}
}
}
}
// return size
public int size(){
return size;
}
// int size ********** not finished
public int size(int index){
if(index<0 || head==null)
throw new IndexOutOfBoundsException("Index out of bound : "+index);
int mi=0,count=0;
Node<Tier1,Tier2,Tier3> p=head;
while(mi<index && p.right!=null) //move to left in the main list till we reach the specified mainindex node
{
p=p.right;
mi++;
}
if(mi!=index) //we could not move so many locations in main list
throw new IndexOutOfBoundsException(" Index out of bound : "+index);
while(p!=null) //move down in the sub list end of sublist
{
count++;
p=p.down;
}
return count;
}
///////////////////// main
public static void main(String arg[]){
File file = new File(""); // ********************
Scanner scanner;
LinkedList<Integer, String, String> list = new LinkedList<Integer, String, String>();
// empty list
try{
scanner = new Scanner(file);
// read a file
list.category1Label = scanner.nextLine();
list.category2Label = scanner.nextLine();
list.category3Label = scanner.nextLine();
scanner.nextLine(); // blank line
Scanner linescanner;
while(scanner.hasNext()){ // read date
linescanner = new Scanner(scanner.nextLine());
linescanner.useDelimiter(","); // , separate
list.add(new Integer(linescanner.nextInt()), linescanner.next(), linescanner.next()); // integer , string , string
linescanner.close();
}
scanner.close();
//
// test test
//
}catch(FileNotFoundException e){
e.printStackTrace();
}
}
}
Explanation / Answer
Answer: For the above problem you missed to include your file path that is why it is showing the "file not found" exception.Try to include your file and compile it .I hav tried with my sample file and it got compiled with no errors.
line need to be modified is :
File file = new File(""); //include your file path.(change 1)
read data (change 2)
Few errors are also present in the code : Need to include "hasNextLine()".(change 3)
Modified main :
public static void main(String arg[]) {
File file = new File("C:\sample\filename.txt"); // change 1
Scanner scanner;
LinkedList<Integer, String, String> list = new LinkedList<Integer, String, String>();
// empty list
try {
scanner = new Scanner(file);
// read a file // change 2
list.category1Label = (scanner.hasNextLine())?scanner.nextLine():"";
list.category2Label = (scanner.hasNextLine())?scanner.nextLine():"";
list.category3Label = (scanner.hasNextLine())?scanner.nextLine():"";
Scanner linescanner;
while (scanner.hasNextLine()) { // read date (change 3)
linescanner = new Scanner(scanner.nextLine());
linescanner.useDelimiter(","); // , separate
list.add(new Integer(linescanner.nextInt()), linescanner.next(), linescanner.next()); // integer
// ,
// string
// ,
// string
linescanner.close();
}
scanner.close();
//
// test test
//
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Include the main then the code compiles with no errors
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.