Question
hello, i desperatley need someones help, everytime i run this(pasted below) Java program i get this console error below. im new to java and dont
hello, i desperatley need someones help, everytime i run this(pasted below) Java program i get this console error below. im new to java and dont know where im making the error. heres a link to a similar program
https://github.com/baguilar1998/CSCI212_Projects/tree/master/Project2/src
Exception in thread "main" java.lang.RuntimeException: java.io.FileNotFoundException: BoxList.txt (The system cannot find the file specified)
at TextFileInput.
at Project2.main(Project2.java:11)
Caused by: java.io.FileNotFoundException: BoxList.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.
at java.io.FileInputStream.
at TextFileInput.
... 1 more
C:\Users\bwezani.bwezani-PC\AppData\Local\NetBeans\Cache\8.2\executor-snippets un.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
public class Box {
private int length,width,height;
/*
* Default Box Constructor for a box. The default height,
* width and length of the box will be set equal to 1
*
*/
public Box() {
length=1;
width=1;
height=1;
}//Box Constructor
/*
* A Box Constructor that takes in 3 parameters
*
* @param length the length of the box
* @param width the width of the box
* @param height the height of the box
* @throws IllegalArgumentException if any of the arguments
* are less than 1
*/
public Box(int length,int width,int height) {
if(length < 1 || width < 1 || height < 1) {
throw new IllegalArgumentException("Invalid Arguements. Try Again.");
}
this.length = length;
this.width=width;
this.height=height;
}//Box Constructor
/*
* @return the length passed to the constructor
*/
public int getLength() {
return length;
}//getLength
/*
* @return the width passed to the constructor
*/
public int getWidth() {
return width;
}//getWidth
/*
* @return the height passed to the constructor
*/
public int getHeight() {
return height;
}//getHeight
/*
* @param length sets the length of the box
* @throws IllegalArgumementException if the length is less than 1
*/
public void setLength(int length) {
if(length < 1) throw new IllegalArgumentException("Invalid Arguement. Try Again");
this.length = length;
}//setLength
/*
* @param width sets the width of the box
* @throws IllegalArgumentException if the width is less than 1
*/
public void setWidth(int width) {
if(width < 1) throw new IllegalArgumentException("Invalid Arguement. Try Again");
this.width = width;
}//setWidth
/*
* @param height sets the height of the box
* @throws IllegalArgumentException if the height is less than 1
*/
public void setHeight(int height) {
if(height < 1) throw new IllegalArgumentException("Invalid Arguement. Try Again");
this.height = height;
}//setHeight
/*
* Calculates the volume of the box
*
* @return the volume of the box which is the
* length multiplied by the width and height
*/
public int volume() {
return length*width*height;
}//volume
/*
* Compares two boxes to see if they are equal to each other.
*
* @return true if the length, width, and height of both boxes
* are equal, otherwise false
*/
public boolean equals(Box b) {
return this.length==b.length && this.width==b.width && this.height==b.height;
}//equals
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
* Represents a box object as a string
*/
@Override
public String toString() {
return "L:" + length + " W:" + width + " H:" + height + " (V:" + volume() + ")";
}//toString
}
import java.awt.*;
import javax.swing.*;
public class BoxGUI extends JFrame{
/**
*
*/
Container container;
TextArea unsorted;
TextArea sorted;
public BoxGUI(UnsortedBoxList b1, SortedBoxList b2) {
// set the title of the JFrame
setTitle("Project 1");
// set the location of the JFrame
setLocation(400, 200);
// set the size of the JFrame
setSize(500, 500);
// set the layout of the JFrame,
setLayout(new GridLayout(1,2));
container = getContentPane();
unsorted = new TextArea();
sorted = new TextArea();
unsorted.setText("Unsorted: " + b1);
sorted.setText("Sorted: " + b2);
container.add(unsorted);
container.add(sorted);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
public abstract class BoxList {
protected BoxNode first,last,head;
protected int length;
/*
* Default constructor for a BoxList
* Creates an empty list
*/
public BoxList() {
head = new BoxNode();
first= head;
last= head;
length=0;
}//BoxList Constructor
/*
* Adds a BoxNode to the BoxList
* @param b a Box object with a length,width and height
*/
public void append(Box b) {
BoxNode node = new BoxNode(b);
last.next=node;
last=node;
++length;
}//append
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*
* Represents a BoxList as a string
* @return a BoxList as a string representation
*/
@Override
public String toString() {
String list = "";
BoxNode display = first.next;
while(display!=null) {
list += (display.data).toString() +" ";
display = display.next;
}
return list;
}
}
public class BoxNode {
protected Box data;
protected BoxNode next;
/*
* constructor for a BoxNode. Default values
* for a BoxNode will be set equal to null
*/
public BoxNode() {
data=null;
next=null;
}//BoxNode Constructor
/*
* One-argument constructor that creates a BoxNode, which stores
* a Box object and the location of the next BoxNode
* @param data a Box object with a length, width, and height
*/
public BoxNode(Box data) {
this.data=data;
next=null;
}//BoxNode Constructor
}//BoxNode
import java.util.StringTokenizer;
public class Project2 {
private static Object fileInput;
public static void main(String[] args) {
//will read off the given file
@SuppressWarnings("LocalVariableHidesMemberVariable")
TextFileInput fileInput = new TextFileInput("BoxInfo.txt");
//Stores a unsorted list of Box objects
UnsortedBoxList unsorted = new UnsortedBoxList();
//Stores a sorted list of Box objects
SortedBoxList sorted = new SortedBoxList();
String line = fileInput.readLine();
//will read through the file until there are no more lines to read
while(line!=null) {
//An array to store the 3 values from the file (length,width,height)
int [] values = new int [3];
//To determine how to separate each part of the string
StringTokenizer st = new StringTokenizer(line,",");
int numberOfCommas = st.countTokens();
for(int i=0;i values[i]=Integer.parseInt(st.nextToken(",")); } //Stores the 3 values into a box object and stores the box object in an unsorted list unsorted.add(new Box(values[0],values[1],values[2])); //Stores the 3 values into a box object and stores the box object in a sorted list sorted.add(new Box(values[0],values[1],values[2])); line = fileInput.readLine(); } BoxGUI b = new BoxGUI(unsorted,sorted); } private static class fileInput { public fileInput() { } } } public class SortedBoxList extends BoxList { /* * Default Constructor for a SortedBoxList which calls * on its parent class constructor to initialize the variables */ public SortedBoxList() { super(); }//SortedBoxList Constructor /* * Adds a BoxNode to the list and will put the node in the correct * position. The list is sorted from lowest to highest based on the Box's * volume * @param b a Box object with a length, width, and height */ public void add(Box b) { //The new BoxNode that will be added to the list BoxNode tempNode = new BoxNode(b); //A BoxNode that keeps track of the next node BoxNode nextNode = first.next; //A BoxNode that keeps track of the previous node BoxNode prevNode = first; //Keeps going through the list until there are no more nodes while(nextNode!=null) { //If the next node has a greater volume than the new node, break out of the loop if(nextNode.data.volume()>tempNode.data.volume())break; //Otherwise move to the next node and keep track of the previous node as well prevNode=nextNode; nextNode=nextNode.next; } //Add the new box node to the right position prevNode.next = tempNode; tempNode.next=nextNode; }//add }//SortedBoxList // TextFileInput.java // Copyright (c) 2000, 2005 Dorothy L. Nixon. All rights reserved. import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.IOException; /** * Simplified buffered character input * stream from an input text file. * Manages an input text file, * handling all IOExceptions by generating * RuntimeExcpetions (run-time error * messages). * * If the text file cannot be created, * a RuntimeException is thrown, * which by default results an an * error message being printed to * the standard error stream. * * @author D. Nixon */ public class TextFileInput { /** Name of text file */ private String filename; /** Buffered character stream from file */ private BufferedReader br; /** Count of lines read so far. */ private int lineCount = 0; /** * Creates a buffered character input * strea, for the specified text file. * * @param filename the input text file. * @exception RuntimeException if an * IOException is thrown when * attempting to open the file. */ public TextFileInput(String filename) { this.filename = filename; try { br = new BufferedReader( new InputStreamReader( new FileInputStream(filename))); } catch ( IOException ioe ) { throw new RuntimeException(ioe); } // catch } // constructor /** * Closes this character input stream. * No more characters can be read from * this TextFileInput once it is closed. * @exception NullPointerException if * the file is already closed. * @exception RuntimeException if an * IOException is thrown when * closing the file. */ public void close() { try { br.close(); br = null; } catch ( NullPointerException npe ) { throw new NullPointerException( filename + "already closed."); } catch ( IOException ioe ) { throw new RuntimeException(ioe); } // catch } // method close /** * Reads a line of text from the file and * positions cursor at 0 for the next line. * Reads from the current cursor position * to end of line. * Implementation does not invoke read. * * @return the line of text, with * end-of-line marker deleted. * @exception RuntimeException if an * IOException is thrown when * attempting to read from the file. */ public String readLine() { return readLineOriginal(); } // method readLine() /** * Returns a count of lines * read from the file so far. */ public int getLineCount() { return lineCount; } /** * Tests whether the specified character is equal, * ignoring case, to one of the specified options. * * @param toBeChecked the character to be tested. * @param options a set of characters * @return true if /** * Tests whether the specified string is one of the * specified options. Checks whether the string * contains the same sequence of characters (ignoring * case) as one of the specified options. * * @param toBeChecked the String to be tested * @param options a set of Strings * @return true if String answer = readLine(); if ( answer == null ) throw new NullPointerException( "End of file " + filename + "has been reached."); if ( !TextFileInput.isOneOf(answer, options) ) { String optionString = options[0]; for ( int i = 1; i < options.length; i++ ) optionString += ", " + options[i]; throw new RuntimeException("File " + filename + ", line " + lineCount + ": \"" + answer + "\" not one of " + optionString + "."); } // if return answer; } // method readSelection /** * Reads a line from the text file and ensures that * it matches, ignoring case, one of "Y", "N", "yes", * "no", "1", "0", "T", "F", "true", or "false". * There must be no additional characters on the line. * * @return /** * Reads a line of text from the file and * increments line count. (This method * is called by public readLine and is * final to facilitate avoidance of side * effects when public readLine is overridden.) * * @return the line of text, with * end-of-line marker deleted. * @exception RuntimeException if an * IOException is thrown when * attempting to read from the file. */ protected final String readLineOriginal() { try { if ( br == null ) throw new RuntimeException( "Cannot read from closed file " + filename + "."); String line = br.readLine(); if ( line != null ) lineCount++; return line; } catch (IOException ioe) { throw new RuntimeException(ioe); } // catch } // method readLineOriginal } // class TextFileInput public class UnsortedBoxList extends BoxList { public UnsortedBoxList() { super(); } /** * method to add a box at the end of the list */ public void add(Box b){ super.append(b); } } toBeChecked
is * equal, ignoring case, to one of the * options
, false otherwise. */ public static boolean isOneOf(char toBeChecked, char[] options) { boolean oneOf = false; for ( int i = 0; i < options.length && !oneOf; i++ ) if ( Character.toUpperCase(toBeChecked) == Character.toUpperCase(options[i]) ) oneOf = true; return oneOf; } // method isOneOf(char, char[])toBeChecked
* contains the same sequence of * characters, ignoring case, as one of the * options
, false otherwise. */ public static boolean isOneOf(String toBeChecked, String[] options) { boolean oneOf = false; for ( int i = 0; i < options.length && !oneOf; i++ ) if ( toBeChecked.equalsIgnoreCase(options[i]) ) oneOf = true; return oneOf; } // method isOneOf(String, String[]) /** * Reads a line from the text file and ensures that * it matches one of a specified set of options. * * @param options array of permitted replies * * @return the line of text, if it contains the same * sequence of characters (ignoring case for * letters) as one of the specified options, * null otherwise. * @exception RuntimeException if the line of text * does not match any of the specified options, * or if an IOException is thrown when reading * from the file. * @exception NullPointerException if no options are * provided, or if the end of the file has been * reached. */ public String readSelection(String[] options) { if ( options == null || options.length == 0 ) throw new NullPointerException( "No options provided for " + " selection to be read in file " + filename + ", line " + (lineCount + 1) + ".");true
if the line matches * "Y", "yes", "1" "T", or "true". * false
if the line matches * "N", "no", "0", "F", or "false". * @exception RuntimeException if the line of text * does not match one of "Y", "N", "yes", * "no", "1", "0", "T", "F", "true", or "false", * or if an IOException is thrown when reading * from the file. * @exception NullPointerException if the end of the * file has been reached. */ public boolean readBooleanSelection() { String[] options = {"Y", "N", "yes", "no", "1", "0", "T", "F", "true", "false"}; String answer = readSelection(options); return isOneOf(answer, new String[] {"Y", "yes", "1", "T", "true"} ); } // method askUserYesNo
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started