Array Of Linked List / Reading From File - Index Out Of Bounce
Oct 17, 2014
I'm trying to read from a file. we made an array of LinkedList and when I'm reading from the file i get a runtime error "index out of bounce in line 66"
import java.lang.*;
import java.util.*;
public class HashTester{
LinkedList_t [] hash;
LinkedList_t [][] doubleHasher;
int size;
[Code] .......
View Replies
ADVERTISEMENT
Mar 9, 2015
How can i convert this linked list code to a read from input.txt
The first line in the input file will give the elements to initialize the linked list with. Consecutive lines will provide operation instructions.
Your code should read one line at a time. After reading each line, it should perform the corresponding operation and print the linked-list on the console.
If an operation is not possible, it should print "N/A".
Sample input file. Please note, the comments (// ...) are given for explanation, the input file will not have them:
4, 5, 6, 3// First line. This will provide the initial values for the linked list : 4->5->6->3
1, 9// Add a 9 at the front of the linked-list. After this operation the linked-list should be: 9->4->5->6->3
2, 1// Add a 1 at the end of the linked-list. After this operation the linked-list should be: 9->4->5->6->3->1
3, // Delete the first node in the linked-list. After this operation the linked-list should be: 4->5->6->3->1
4, // Delete the last node in the linked-list. After this operation the linked-list should be: 4->5->6->3
5, 11// Delete the node with the value 11 in it. Since this is not possible, it should print "N/A"
5, 6// Delete the node with the value 6 in it. After this operation the linked-list should be: 4->5->3
Sample output to the console:
4->5->6->3
9->4->5->6->3
9->4->5->6->3->1
4->5->6->3->1
4->5->6->3
N/A
4->5->3
My Code:
LinkedList.Java
class linkedList
{
protected Node start;
protected Node end ;
public int size ;
[Code] .....
View Replies
View Related
Oct 21, 2014
I need to read from a text file given to us that has a list of books with authors names and book titles separated by an @ symbol for our delimiter. Here is the code I have right now, but it throws an ArrayIndexOutOfBoundsException at line 7...and I am unsure why?
import java.io.*;
import java.util.*;
public class Driver {
public static void main(String[] args) {
new Driver(args[0]);
[Code] ....
I realize that it must have something to do with my command line argument...but I am unsure what. Am I not entering the file name correctly?
View Replies
View Related
Oct 29, 2014
Basically, the program is to be able to read information from a file like this;
11111 63.5
22222 59.9
33333 82.4
44444 73.1
55555 77.3
66666 99.5
77777 68.7
88888 53.2
99999 66.8
where the first number is student number and the second is their grade. I need to read this information from a .txt file and dynamically create a new node containing that student's number and mark, and insert it in the correct position in the linked list (in descending order based on grade). So I get that each Node needs to contain two data types, an Int for Student # and a Double for their grade, and I'm pretty sure I've done it correctly with my StudentNode class which can be found in the source code linked above.
But what I don't get it using that class to create Nodes in my main class and then sort them based on their Double grade value WHILE they are being sorted. I just don't understand where to actually put the methods and such that does these things. Apparently I'm supposed to have three classes.
One named StudentNode which is just the node info, which I have done.
The second is called StudentList which is apparently supposed to contain the head of LinkedList and the methods I need? I'm not sure how it ties into StudentNode though.
The third and final is just the main class which I'll use to test it.
Then after all that I need to print out the median mark through a recursive method that isn't allowed to use any loops or call and functions/methods that use loops. The function should return the node in the list which contains the median mark. Secondly, in order to find the median, you need to know how many items in total are in the list. Your recursive function must calculate that number (also recursively); you may not keep track of this count elsewhere your program. Your recursive solution should only examine each node only once, and the depth of the recursion should be equal to the number of nodes in the list.
Here's my current in-progress code.
import java.io.FileNotFoundException;
import java.util.Scanner;
class StudentNode {
private int studentNum;
private double grade;
private StudentNode next;
[Code] .....
I'm pretty sure the StudentNode class is fine, it's the StudentList class that I'm not sure about. I'm not sure how to add nodes since there isn't a getNext() method in the StudentList class.
View Replies
View Related
Sep 21, 2014
I'm having trouble completing my homework. The problem:
>Suppose that you want an operation for the ADT list that adds an array of items to the end of the list. The header of the method could be as follows:
public void addAll(T[] items)
>Write an implementation of this method for the class LinearLinkedList
At first this seemed like an easy problem to me. This is the solution I came up with:
public void addAll(T[] items) {
int length = items.length;
for(int i = 0 ; i < length; i++){
this.addLast(items[index]);
}
}
However, one of the requirements for the assignment is that we're not allowed to use the addLast method. how else to insert items to the Linked List without this method. These are the user-defined LinearLinkedList and ListNode classes we made during class:
**LinearLinkedList**
// Linear linked list class
import java.util.NoSuchElementException;
public class LinearLinkedList {
[code]....
View Replies
View Related
Mar 9, 2014
TL,DR: observe the nodes just below, that is the data each node in the link list contains. How do I iterate through the entire list to reach the last element (H), so I can add an element right after it?
So I have 4 nodes doubly linked with two dummy nodes for head and tail:
head
node1 = {A}
node2 = {null, B}
node3 = {C, null, D, E}
node4 = {null, F, null, G, null, null, H, null}
tail
Ok, so since the list only contains 8 elements that are not null, its size is actually 8 correct? So now lets say I have an add method that has
add(E item) and inserts the item at the end of the list. So I can get to the last node with tail.previous(), but then how do I iterate to the end so I can add the item after the last item in the list (H). I guess I don't know how you only access one nodes data when that data is an array with empty spaces.
Here is the entire Node code:
Also, I can't just iterate through the whole thing because I am not supposed to. I am supposed to just find the right node and iterate through that only.how to maneuver around a linked list containing nodes where each node contains an array.
/**
* Node class that makes up a DoublingList. Feel free to add methods /
* constructors / variables you might find useful in here.
*/
public class Node<E> {
/**
* The node that comes after this one in the list
[code]....
View Replies
View Related
Jan 22, 2014
I have `country.properties` file which have values as follows:
1=USA
91=India
20=Egypt
358=Finland
33=France
679=Fiji
and, have a response class file, which is setting a response from database to display it on `JSP` file. The value that I am getting from database is in the form of `code` or `integer`. I needed to have that value from the database and before setting the response I need to use `getProperty(code)` and save the String representation of that code into a new list and then pass that list to `setResponse`. For e.g: This is the value I am getting from database:
col1 | col2 | col3 |
1 helo done
I needed to show on my JSP page as:
col1 | col2 | col3 |
USA helo done
I was following this tutorial [URL].... but not able to exactly understand how to achieve the same.
This is my `DAOImpl` where I needed to `iterate` and save the `mapped key-value` in a new list and then pass to `JSP` page
public class CountDAOImpl implements IDataDAO {
private Connection conn = null;
private Statement statement = null;
private ResultSet rs = null;
private List<String> country_code = new LinkedList<String>();
[Code] ....
View Replies
View Related
Mar 30, 2015
Trying to read integers from a text file and place them in a linked list in order. Program is compiling and running fine, just not the way I am intending when I am calling the methods. (Note: You have to enter the file location in the var x if you plan on running this.)
import java.io.*;
import java.util.*;
public class Node {
public int value;
public Node next;
Node(int x) {
this.value = x;
[code]....
View Replies
View Related
Oct 13, 2014
I am having a little trouble with a part of my Java assignment that needs to have linked lists in it.
I need to write a public method that takes an array of type object to load strings into a linked list.
View Replies
View Related
Feb 14, 2014
I have some class called sorted to sort the linked list through the nodes of the list. and other class to test this ability, i made object of the sort class called "list1" and insert the values to the linked list.
If i make other object called "list2" and want to merge those two lists by using method merge in sort class. And wrote code of
list1.merge(list2);
How can the merge method in sort class know the values of list1 that called it as this object is created in other class.
View Replies
View Related
Dec 9, 2014
I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...
The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data. Here is the base Product class that must be used to create the objects for the array.
public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;
[Code]...
these continue for about 40-50 entries, they are not seperated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name seperated with spaces, then price after a comma, then quanity after the second comma.....
View Replies
View Related
Dec 8, 2014
I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...
The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data.
Here is the base Product class that must be used to create the objects for the array.
public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;
//Constructor
public Product( String pName, double price, int quanity )
[code]....
and then here is the data from the text file that i must extract to use to create product objects.
Dill Seed,938,34
Mustard Seed,100,64
Coriander Powder,924,18
Turmeric,836,80
Cinnamon (Ground Korintje),951,10
Cinnamon (Ground) Xtra Hi Oil (2x),614,31
Cinnamon (Ground) High Oil (1X),682,19
these continue for about 40-50 entries, they are not separated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name separated with spaces, then price after a comma, then quanity after the second comma.....
View Replies
View Related
Oct 5, 2013
So we have an assignment regarding a linked list implementation of a given list interface.
In my list interface, the method contains(T anEntry) is defined.
In the LList implementation, contains is already implemented as part of getting the core methods in.
Now I am being tasked with the following:
Provide a second implementation of the method contains2(T anEntry) that calls a private recursive method
Private boolean contains (T anEntry, Node startNode) that returns whether the list that starts at startNode contains the entry anEntry.
I've written the private recursive method already. That's not an issue (at least not right now).
But what I don't understand is how startNode is supposed to be populated when this private contains method is called from the public contains2 method? contains2 only takes one parameter: anEntry. the private method takes two parameters: anEntry and startNode. How am i supposed to provide startNode when I am calling contains2?
View Replies
View Related
Mar 22, 2015
How do I read in a file line by line into an array without using arraylist?
I know how to do this using BufferedReader, but I am wondering how to do this using Scanner? When I used BufferedReader I noticed that there must be two exceptions to be caught which were IOException and FileNotFoundException, whereas a Scanner needs only a FileNotFoundException, why is that?
Java Code: public class practice {
public String[] array;
Scanner inputStream = null;
Scanner n = new Scanner(System.in);
public String line;
[Code].....
View Replies
View Related
May 5, 2015
I'm currently working on a program what is reading a txt file with a bunch of numbers in it but keep getting an error back.
Validator.java:
import java.io.IOException;
import java.io.File;
import java.util.Scanner;
public class Validator {
public int[] accounts = {};
[Code] ....
Accounts.txt:
5658845
8080152
1005231
4520125
4562555
6545231
7895122
5552012
3852085
8777541
5050552
7576651
8451277
7825877
7881200
1302850
1250255
4581002
View Replies
View Related
Jun 5, 2014
I can read the data to a monitor perfectly. But, I'm having problem reading data from an external file into an array of class objects. Here's my attempt at writing the method:
private void openFile()
//This method asks the user to enter a file name(including the file extension) and then
//Sets the data to an array of Product type {
String fileName, storeName="", emptyLine1="", emptyLine2="", name="", productName="";
int demandRate=0;
double setupCost=0.0, unitCost=0.0, inventoryCost=0, sellingPrice=0;
Scanner inputStream = null;
[code]...
View Replies
View Related
Apr 15, 2014
I am reading records from a txt file and storing it into an array
import java.util.*;
import java.io.*;
public class PatientExercise {
//patients exercise time
public static void main (String[]args) throws IOException{
Scanner in = new Scanner(new FileReader("values.txt"));
double [] patientTimeRecords = new double [300];
int noExerciseCount=0, numPatients =0;
double highest=0, lowest=0, avg=0, totalTime=0;
[Code] ....
However an error msg keeps popping up:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at pastpapers.PatientExercise.main(PatientExercise.ja va:44)
line 44 is:patientTimeRecords[i]= in.nextDouble();
View Replies
View Related
Apr 5, 2014
I have an assignment on sorting, i kno i can get the sorting down but im having an issue with inputing the 512 ints in a file into an array. the instructor provided us with a file with 4 equal sets of ints. i tried to make my array of size [scan.nextInt()] and it cuts off the last 21 ints. and skips the first int. how can i get all of the integers in the text file into my array? this is what i have so far. if i hard code the array to size 50000 and then try to print the array it compiles but errors out when running it.
System.out.println("Please Enter text file in this format, XXXXX.txt :");
String file =fileName.nextLine();
Scanner scan = new Scanner(new File(file));
int [] data = new int[scan.nextInt()]; <-------here it skips first int
int count= data.length;
for (int i=0; i<data.length-1;i++) {
data[i]=scan.nextInt();
}
System.out.print(Arrays.toString(data));
rst 4 ints in output are: 501, 257, 390, 478...., supposed to be 492,501,390....and last ints are: ....88, 83, 79, 0 and supposed to be :88 83 79 77 76 72 71 71 66 57 56 48 48 41 33 30 23 23 18 17 15 13 9....it replace last ints with 0. why ? and how do i fix this. attached it the text file
View Replies
View Related
Oct 6, 2014
I have this ListInterface class that has operations for my linked list and a LList class. The Llist and ListInterface classes are perfect. My job is to create a driver, or a demo class that showcases these operations. That being said, heres the driver so far:
import java.util.*;
public abstract class DriverWilson implements ListInterface
{
public static void main(String[] args)
{
LList a = new LList();
[code]....
View Replies
View Related
Sep 13, 2014
//compiler error I'm receiving
J:CS3Student Folder Review Lab #2APlusImage.java:41: error: cannot find symbol
xcor.add(read.nexInt());
[Code].....
View Replies
View Related
Oct 10, 2014
I have an assignment for my intro class that requires me to read from a file that is a list of songs, their artists, and the year they were released. As seen below, a print line statement prompts the user to enter an artist name, and then it uses a buffered reader to gain input, and then it is supposed to match that input.I realize that this is not a complete statement, but I'm mostly concerned with getting the .indexOf statement to work.Currently it only returns the first object in the array.
for(int i = 0; i < song.length; i++) {
System.out.println("Enter an Artist name");
String input1 = kb.readLine();
if (song[i].getArtist().indexOf(input1) > -1) {
/*tried changing -1 to -2. When I do, it returns the
first array entry, regardless of what I input*/
System.out.println(song[i].toString());
}
}
View Replies
View Related
Mar 21, 2014
What I'm supposed to do is make a method to insert a set of Tiles to the list,i.e.,a detour(make sure that the inserted detouris compatible with thecurrent path so that the resultingpathdoesnot have any gaps). But I'm confused on how to go about doing it. I was thinking of maybe just adding 1 to the current Node.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;
public class Path {
static Tile startTile;
[code].....
View Replies
View Related
Jan 30, 2014
I'm trying to implement an Office class that contains an inner class: WorkerNode. The WorkerNode class has a name attribute (String) and WorkerNode attributes for boss, peer and subordinate. The attributes of Office are manager and current which are WorkerNode references. The manager refers to the entry point of the structure and current is the current node in the structure. For simplicity, i'm going to try to limit it to 3 levels and assume that the names are unique. I've put together a Office class that containing main and provided the code I've worked on so far.
public class Office {
public static void main(String[] args) {
String name=Input.getString("input the manager's name: ");
Office office=new Office(name);
int option;
[code]....
View Replies
View Related
Apr 20, 2014
I have made a node class and im trying to implement a sorting method. I must use a selection sort but with specific instructions: "Your method should not need to use the new operator since it is just moving nodes from one list to another( not creating new nodes)
this is my current implementation ..but i am instantiating new object..
public class NodeInt
{
private int data;
private NodeInt next = null;
public NodeInt(){}
//precondition:
//postcondition:
public NodeInt(int data, NodeInt next)
{
this.data = data;
this.next = next;
[code]....
edit: this is the part that worked but i had it commented out so i have the previous and current declared above but didnt copy.
View Replies
View Related
Apr 30, 2014
Ok here I have a code that generates 1 million random values then converts them to a string then hashcode. I then insert into a linked list and then I want to run through each hash and find it in the linked list timing each run then averaging out the time at the end.
It works great for smaller amounts of numbers it is searching for (fine under 50 thousand searches for the for loop starting at line 24 LinkedListTest.java) but when I try to do the full million searches it gives me "a Exception in thread "main" java.lang.StackOverflowError" at line 158 in List.java. Maybe im getting tired but I cannot figure out why.
// class to represent one node in a list
class ListNode< T >
{
// package access members; List can access these directly
T data; // data for this node
ListNode< T > nextNode; // reference to the next node in the list
[code]....
View Replies
View Related
May 1, 2014
public void add(int d){
listNode l = new listNode (d, null);
l.next = first;
first= l;
}
public list Sum2List (list l1, list l2){
//variables
int sum;
[Code] .....
But I have a problem in my first listNode where it ll be pointing to null, thus in the sum2List method the program checks the while condition into false and doesn't go through the loop.
View Replies
View Related