Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***HELP*** I am writing this program that should read data from a file and displays it in a JTree component. However I keep getting this

***HELP*** I am writing this program that should read data from a file and displays it in a JTree component. However I keep getting this error when I run the program.

image text in transcribed

I specified the file name and file path location but I keep getting the same error! I post my code below. CAN SOMEONE PLEASE HELP AND EXPLAIN TO ME WHAT I MIGHT BE DOING WRONG! I have the file on my desktop

// java packages are imported import java.io.BufferedReader; //java buffer reader import java.io.FileInputStream;// java input file sream import java.io.FileNotFoundException;// file not found exceptio import java.io.IOException;// java input output exception import java.io.InputStreamReader; // java input stream reader // java swing import javax.swing.JFrame;// java jframe import javax.swing.JTree;// java jtree import javax.swing.tree.DefaultMutableTreeNode;

//class JTreeDemo is defined public class JTreeDemo { // variables declared private int numberOfNodes; //for no. of nodes of int data type private String data[]; //or tree data of string data type private JTree tree; //for the components of JTree public JTreeDemo() //Default constructor defined

{ numberOfNodes=5;// the no. of nodes is defined data=new String[numberOfNodes];// creating data by passing the no. of nodes tree=new JTree();// creating the tree

}// end of the constructor

//method defined for reading the tree data from the given file public void readData(String fileName) throws FileNotFoundException { // object creation for the //FileInputStream FileInputStream inputStream=new FileInputStream(fileName); //ONE OF THE ERRORS // object creation for the //InputStreamReader InputStreamReader reader=new InputStreamReader(inputStream); // object creation for the //BufferedReader BufferedReader bufferedReader=new BufferedReader(reader); String line; // for reading the lines in the file one by one int countNode=0; // count node is set to zero // try block for enclosing the exception area try { // The white loop is defined while((line=bufferedReader.readLine())!=null) { // if conditional statement defined if(line.matches("^-?\\d+$")) { numberOfNodes=Integer.parseInt(line); // parse the nodes } // end of if

else { // else statement data[countNode]=line; countNode++;// countnode is incremented }// end of else }//end of while loop inputStream.close();// inputstream is closed reader.close();// reader is closed bufferedReader.close();// bufferreader is closed }// end of try block catch (IOException e) { // Catch block // TODO Auto-generated catch block e.printStackTrace();// method called }// end of catch block }// end of readData method // populateTree method is define for populating tree in JTree public void populateTree() { DefaultMutableTreeNode rootNode=new DefaultMutableTreeNode();// object creation for(int i=0;i { String[] nodeValues=data[i].split(" ");

if(nodeValues.length>1)// if conditional statement for length of nodes { rootNode.add(new DefaultMutableTreeNode(nodeValues[0]));// nodes are added to //Jtree for(int j=1;j { // adding a tree node ((DefaultMutableTreeNode)rootNode.getChildAt(i)).add(new DefaultMutableTreeNode(nodeValues[j]));;

}// end of for loop for length of nodes }// end of if statement //if length of node is equal to 1 else if(nodeValues.length==1){ // editable nodes are //added to Jtree. rootNode.add(new DefaultMutableTreeNode(nodeValues[0])); } // end of else if

}//end of for loop for no. of nodes

//JTree is created by passing the root node tree=new JTree(rootNode);

} //method is created to show some GUI public void createAndShowGUI() { JFrame frame=new JFrame("JTreeDemo");// object creation frame.add(tree);// add into the tree frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// to close the program frame.pack();// for sizing the frame frame.setVisible(true); //take a screenshot and restore the JFrame }// end of createAndShowGUI method // main method is defined // throws file not found //exception public static void main(String[] args) throws FileNotFoundException { String fileName = "Filedata.txt"; //Input file is defined // THIS IS THE FILE NAME I HAVE // source of the file is defined String fileLocation = "C:/Users/Dre/Desktop/Filedata.txt"; // THE FILE PATH I HAVE //use to create and show the JTree JTreeDemo treeDemo=new JTreeDemo(); // object of the class JTreeDemo is created // readData method is called treeDemo.readData(fileLocation + fileName); // THE SECOND ERROR // populateTree method is called treeDemo.populateTree(); //createAndShowGUI method is called treeDemo.createAndShowGUI(); }//end of main method }// end of main class

R Problems o lavadoc D. DeclaratonConsole X JTreeDemo [Java Application) C\Program FilesJavajre1.8.0_144 bin\javaw.exe (May 3, 2018, 10:51:56 AM) Exception in thread "main ia io.FileNotFoundException: C:\Users\Dre\Desktop\Filedata.txtFiledata.txt (The system cannot find the file specified) at java.io.FileInputstream.openo (Native Method) at java.io.FileInputStream.open (Unknown Source) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at 3TreeDemo. readData 3TreeDemo.java: 33) at JTreeDemo.main (JTreeDemo.java:137)

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

Question

Strategize messages to solve business problems

Answered: 1 week ago