Linked Lists - Reading Information From A File

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


ADVERTISEMENT

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 View Related

Reading From Text File - Print Linked List On Console

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

Linked Lists In Java

Nov 27, 2014

I currently don't understand how the node's work.

private static class Node<AnyType>
{
private AnyType data;
private Node<AnyType> next;
public Node(AnyType data, Node<AnyType> next)
{
this.data = data;
this.next = next;
}
}

Why do I make private Node<AnyType> next;And why do I have an inner class of Node for a linked list?I had the same topic in C, but there it was somehow easier than in java. Because there you have pointers.

View Replies View Related

Iterating Through Linked Lists

Apr 27, 2014

Write a Java function Sum2List that takes two lists L1 and L2 of the same size and returns list L that contains the sum of data inside the corresponding nodes of lists L1 and L2.

ex: L1 = {1,2,3}
L2 = {4,5,6}
L = {5,7,9}

I do not know how to iterate through two different lists >>

public LinkedList Sum2List(LinkedList l1, LinkedList l2){
LinkedList l3 = new LinkedList();
ListNode Current1 = L1.getFront();
ListNode Current2 = L2.getFront();
while(Current1 != null){
l3.Add(Current1.getItem() + Current2.getItem());

[Code] .....

This my attempt to write the code , but the dr. side that we have not to use the build in methods ( getNext()...etc)
in the function .

View Replies View Related

How To Iterate Through Linked Lists And Add Them To A Map

Mar 10, 2014

I have an XML sheet and my project is top retrieve the required elements from XML sheet. So my format of XML was like follows:

<Class>
<Employees>
<EMPLOYEE>
<ENum> Abc123</ENum>
<Ename> John<?Ename>
<EType>Mathematics</EType>

[Code] ....

I have used unmarshalling concept to retrieve the data elements... I have to check whether the elements satisfy few regulations when compared with data in Database. So, i thought of grouping the employees depending on EType. I have created a Map with linkedlist of employees. Say Map<String, LinkedList<Employe>>EmpMap=new Map<String, LinkedList<Employe>>();

I have already created a class named Employee which has all the setter and getter methods for employee.

Here am going to take Etype(Employee type) as key and linkedlist(list of employees of certain type) as value. How to iterate these linked lists and place them in my Map.

View Replies View Related

Delete Method With Linked Lists?

Jul 19, 2014

Why is this method throwing a null pointer exception when I call it from main?

I'm trying to submit an integer to indicate the number of nodes in a linked list to delete and then delete them headfirst.

public boolean delete(int x){
if (x > count || head == null){
return false;
}else{ while (x>0){
head = head.getPrevious();

[Code] ....

View Replies View Related

Sorting Linked Lists Via Consecutive Nodes

Feb 13, 2014

I have the following code that supposed to perfrom sorting on the linked list using method sort in order usind node concept of Linked List but seems to give inlogic results. the following code tests only the values lower than the first value but i can't manage to sort the data higher than the first entered value;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.*;
public class ListNode<T extends Comparable> {
ListNode<T> nextNode;
T data;
public ListNode(T item)

[Code] .....

View Replies View Related

Doubly Linked Lists And Using Java Generics

Oct 25, 2014

I'm working with Doubly Linked Lists and using Java Generics..

My nodes looks like this:
class DNode<E> {
DNode<E> previous;
DNode<E> next;
E element;

//and all methods inside
}

My list of Nodes looks like this:
class DLL<E>{
private DNode<E> head;
private DNode<E> tail;
private int size;

[code]....

As you can see, as arguments they get "E o"...I need to write a program, which from the main function asks the users how long is the list, and after they type it's length, I ask them to start typing the elements (integers)...and this is how my main method is written, but I can't seem to make it work, specialy when I call the "insLast" method,I guess it's because the arguments i'm giving to the function...how to read the elements and write them into the list?

public static void main(String[] args) throws IOException {
DLL<Integer> lista=new DLL<Integer>();
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s = stdin.readLine();
int N = Integer.parseInt(s);
s = stdin.readLine();
String[] pomniza = s.split(" ");
for (int i = 0; i < N; i++) {
lista.instLast(Integer.parseInt(pomniza[i]));
}

}

View Replies View Related

Head Of Multiple Linked Lists All Getting Set To The Same Thing?

Apr 16, 2014

I am creating a chained hash table that uses my own LinkedListclass to handle collisons. Here is where my issue is occuring: I start with an array of LinkedLists, my "hash table", intially set to null. When i wish to add a word, if the desired location is null, I create a new LinkedList, place it in that array index. I then create a new LinkedListNode and set the head of the new LinkedList to the new node.

My issue is occuring here: Whenever I set the head of my new LinkedList to my new LinkedListNode all of my previously created linkedlists also have their head changed to the new node.

My debugger shows that I am truly creating new linkedlists every time. I was afraid each array index was just pointing to a single array, but that is evidently not the issue. Below is all the relevant code

public class SetOfStrings {
private int arraySize;
private LinkedList[] mainArray;
private int totalCount;
//other methods here
public boolean add(String toAdd) {
int hashToAdd = hash(toAdd);

[code]....

SUMMARY: Whenever I set the head of a Linked List to a new node, it changes the head of all my linked lists to point to the same new node

View Replies View Related

Big-O Involving Arrays And Linked Lists When Using PriorityQueue

Oct 25, 2014

I'm not sure if my understanding of PriorityQueues is correct, so I'm trying to check if my reasoning is valid. I'm supposed to compare the Big-O for arrays and linked lists for the following instructions:

Insert 100 objects having the priorities 1, 2, 3, ... , 99, 100
Big-O for Array: __________
Big-O for Linked List: ___________

Insert 100 objects having the priorities 100, 99, 98, ... , 2, 1
Big-O for Array: __________
Big-O for Linked List(Assume no tail reference): ___________

If my understanding is correct, priority queues take in items randomly with no particular order, but they are removed according to the priority of each element. If what I've said is true, wouldn't that mean that inserting any number of objects would be O(1) for both linked lists and arrays? If the PriorityQueue has no particular order, then wouldnt each add() simply insert something to the next array index/linked list node?

View Replies View Related

Implement A Set Abstract Data Type Using Singly Linked Lists

May 6, 2015

I am trying to implement product method below which returns the set representing the Cartesian product of the current set and a given set (the Cartesian product contains all ordered pairs (a, b) where a belongs to the current set, and b belongs to the given set). The product should be a ListSet <Tuple<E>> object where each ordered pair is a Tuple element. (I have a Tuple class which implements an ordered tuple)

What am I trying to do in the product method : Make 2 for loop and inside the for loop make an array of <E> then set the 2 elements of the tuple then again set tuple and add it to arrayList. how to set 2 elements of the tuple and set tuple ??

public class ListSet<E> implements Iterable<E>{
SinglyLinkedList<E> sl;
public ListSet(){
sl = new SinglyLinkedList<E>();

[code]....

View Replies View Related

Write A Java Program To Store Employee Information In A Linked List

May 12, 2015

Write a java program to store employee information in a linked list, the program should implement the following functions:

The program should display the list of functions, and ask the user to enter the number of function that he/she wants to do, then perform the function as it is required in the previous table.

import java.util.*;
public class Employee {
Scanner in = new Scanner(System.in);
String Name;
String Address;
String Department;
int ID;
int Salary;

[code]....

this is my out put

Please choose a number:
1-Add a new employee
2-Update employee's info
3-Remove employee's info
4-Exit
1
Enter name:
Enter address:
2
Enter department:
3
Enter ID:
4
Enter salary:

now:

1- why are not my adding coming out in the output only the Enter name & Enter address ??

2- how can I add names and ID's and information to test that program

View Replies View Related

Merge Two Sorted Linked Lists Into A New Sorted List

Nov 2, 2014

How to go through each link item in both lists, and directly link them into the new list in order without using insert()

class Link {
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long dd) // constructor
{ dData = dd; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
} // end class Link

[Code] ......

View Replies View Related

Rename To Functions And Lists Class File?

May 29, 2014

What are they the rename To functions and lists the class file?

View Replies View Related

Split Text File Lines Into Pieces And Put Them In Different Array Lists?

Mar 14, 2015

For example, if I had a text file with one line such as "first, second, third", would there be a way to make it so I could make "first" go to the first array list, "second" to the second, and "third" to the third?

I haven't posted any of my code so far as it wouldn't be right to be handed the finished code on a platter and I should put the work into it, but I'd just need a loop to make it go through the rest of the lines in the text file to add all the first, second and third parts of each line, right?

(I already know how to input data from a file, but not how to split up a line into bits to put into different array lists).

View Replies View Related

Read Information From File - How To Get Highest Value

Nov 12, 2014

The requirement asked me to write a Java program to read information from the file, display the original information along with the player's average score of the season (one line for each player), and announce who is the highest average-scoring player of the team. But I always only get the highest which show the last player.

Such like the output showed:

Smith 13.0 19.0 8.0 12.0 Player's average: 13.00
Badgley 5.0 Player's average: 5.00
Burch 15.0 18.0 16.0 Player's average: 16.33
Watson Player's average: 16.33
Knox 10.0 12.0 8.0 6.0 Player's average: 9.00
Casler 15.0 7.0 Player's average: 11.00
Winorburg 13.0 14.0 14.0 23.0 20.0 Player's average: 16.80
Morton 4.0 11.0 13.0 Player's average: 9.33
Seeling 16.0 8.0 9.0 11.0 Player's average: 11.00
Tanshi 5.0 13.0 10.0 Player's average: 9.33

The highest average-scoring player is: Tanshi

import java.util.Scanner;
import java.io.*;
public class PA7{
public static void main(String[] args) throws IOException{
String name = null;

[Code] .......

View Replies View Related

Insert Information From CVS File In Postgres Table?

Mar 15, 2015

I am trying to insert information from a CVS file in a postgres table and I have this exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at crislicenta.automateImport.main(automateImport.jav a:29)

and the cod line is: db.importData(conn,args[0]);

The source code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
 public class automateImport {
static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_URL="jdbc:postgresql://localhost5432/";

[code]....

View Replies View Related

Reading Text File Into Object Array And Create Random Access File

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

Reading Text File Into Object Array And Creating Random Access File?

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

Adding User Input To File - Information Overwritten?

Dec 8, 2014

I need to write a program that will let a user input name, age, email, and cell phone number, and commit the input to a text file. They need to add multiple entries. I figured out how to create the file and write to it, but when the user enters a second set of information, the first is overwritten. How can I make the file be added to instead of overwritten? The following is my code:

try
{
FileWriter writer = new FileWriter("ContactInformation.txt");
BufferedWriter bw = new BufferedWriter(writer);
nameTextField.write(bw);
bw.newLine();

[Code] ....

View Replies View Related

Unable To Write Text File With Information Stored

Jul 27, 2014

I am trying to write to a text file with information stored like this.

brick 500
stone 500

And so on. Here is the code I am using to write to the file.

switch(record){
case "brick":
x.format("%s%n", "brick " + value);
closeFile();
break;
case "log":
x.format("
%s%n", "log " + value);
closeFile();
break;

When I write like this, putting deletes the first line. What I can look into or other functions that would be useful?

View Replies View Related

Read Characters From Txt File / Store Them Into 2D Array And Print Information

Oct 10, 2014

The point of this program is to read characters from a txt file and store them into a 2D array. After this has been accomplished, the information is to be printed in the same manner it is read from in the txt file.

Here is the code I have so far:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) throws FileNotFoundException

[Code] ....

And this is the error I am receiving when trying to accomplish the goal of the project:

Exception in thread "main" java.lang.NumberFormatException: For input string: "############"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Maze.<init>(Maze.java:15)
at Main.main(Main.java:20)

What's going on here and how I can correct this?? The information I am trying to store in a 2D array and then print is this:

############
#.#........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#

View Replies View Related

Program Should Read File Contents And Store Information In Array Of Golfers

Dec 3, 2014

The main method will drive your program by doing the following:

-Create an array to hold all the individual golfers and par scores (type is Golfer[ ]).
-Prompt the user for a data file containing the Par scores and the player names and score. The format of the input file should look like

Par, 3, 4, 4, 3, 4, 3, 5, 3, 4
George, 3, 3, 4, 3, 4, 2, 3, 3, 4
Paul, 4, 5, 4, 3, 6, 3, 4, 3, 5
Ringo, 3, 3, 4, 3, 4, 2, 4, 3, 4
John, 4, 4, 4, 3, 4, 2, 5, 3, 4

Your program should read the file contents and store the information in your array of Golfers.

View Replies View Related

Hurricane Java Project - Read Information From Data File And Run It Through But Program

Dec 2, 2014

I am now stuck. I am writing a program that reads information from a data file and runs it through but program. However, I am almost finished with the program, but cannot figure out the last few parts.

public class Storm {
private final double KnotsToMPH = 1.15;
// global user-defined types:
private int beginDate;
private int duration;
private String name;

[Code] .....

I have not attached the data file because it contains a total of 360 lines of hurricane information.

View Replies View Related

Swing/AWT/SWT :: Populate JPanel Based On More Information About Selected JTree Node From A File?

Aug 27, 2014

When you select a node in a JTree can you then populate a JPanel based on more information about that node from a file?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved