{ public static void main(String[] args) throws IOException { // Create a new LinkedList object for holding the ValueData object: LinkedList linkedList =

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

When I attempt to run this program, I am getting an ArrayIndexOutOfBounds Exception on line 21 of the given Main. What is causing this to occur and what can be done to fix it? 

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
public class Main{
public static void main(String[] args) throws IOException {

// Create a new LinkedList object for holding the ValueData object:
LinkedList<ValueData> linkedList = new LinkedList<>();

// Create a new ObjectStack object
ObjectStack objectStack = new ObjectStack();

// Read a text file containing lines (records) of ValueData.
// This is in order to fill the LinkedList with ValueData from the file.
BufferedReader reader = new BufferedReader(new FileReader("values.txt"));
String line = reader.readLine();
while (line != null) {
String[] parts = line.split(",");
String name = parts[0].trim();
int value = Integer.parseInt(parts[1].trim());
ValueData data = new ValueData(name, value);
linkedList.add(data);
objectStack.push(data);
line = reader.readLine();
}
reader.close();
// Use the linkedList and objectStack objects as needed.
}

}

___________________________________________________________________________________________

ObjectStack.java: 

import java.util.EmptyStackException;

public class ObjectStack extends LinkedList implements Stack {

private int size;

// Creates a new instance of ObjectStack:
public ObjectStack() {
super();
}

@Override
public void push(Object item) {
// Method body for push()
}

@Override
public Object pop() throws EmptyStackException {
// Method body for pop()
return null;
}

@Override
public Object top() throws EmptyStackException {
// Method body for top()
return null;
}

@Override
public boolean isEmpty() {
// Method body for isEmpty()
return false;
}

@Override
public int size() {
// Method body for size()
return 0;
}
}

__________________________________

Stack.java (interface):

import java.util.EmptyStackException;

public interface Stack {
public int size();
public boolean isEmpty();
public Object top () throws EmptyStackException;
public Object pop () throws EmptyStackException;
public void push (Object item);
}

___________________________________

LinkedListNode.java: 

public class LinkedListNode
{
private Object data;

private LinkedListNode next;

// Constructor:
public LinkedListNode(Object data) {
this.data = data;
this.next = null;
}

// Getter and setter:
public LinkedListNode getNext() {
return next;
}

public void setNext(LinkedListNode next) {
this.next = next;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

public Object getItem() {
return null;
}
}

__________________________________________

LinkedList.java: 

public class LinkedList{
private Node head; // head node of the list
/* Default constructor that creates an empty list */
public LinkedList() {
head = null;
}
//inserts new node at beginning of list
public void insert(Node v){
v.setNext(head);
head = v;
}
//searches elements of linked list for a match
public String search (String target) {
Node temp = head;
while(temp != null){
if(target.equals(temp.getElement())){
return "Found the target";
}
temp = temp.getNext();
}
return "Target not found";
}
//remove the first node of the array
public void removeFirstNode( ) {
if(head != null){
head = head.getNext();
}
}
//removes node with element matching the string target
public void removeNode( String target) {
Node prev = null;
Node curr = head;
while(curr != null){
if(target.equals(curr.getElement())){
break;
}
prev = curr;
curr = curr.getNext();
}
if(curr == null)
return;
if(prev == null){
removeFirstNode();
return;
}
prev.setNext(curr.getNext());
}
//loop from first to last element and prints them all
public void printList(){
Node temp = head;
while(temp != null){
System.out.print(temp.getElement() + " ");
temp = temp.getNext();
}
System.out.println();
}



}//end of class

______________________________________

Node.java:

public class Node {

private String element; // we assume elements are character strings
private Node next;

/* Creates a node with the given element and next node. */
public Node(String s, Node n) {
element = s;
next = n;
}

/* Returns the element of this node. */
public String getElement() {
return element;
}

/* Returns the next node of this node. */
public Node getNext() {
return next;
}

// Modifier methods:

/* Sets the element of this node. */
public void setElement(String newElem) {
element = newElem;
}

/* Sets the next node of this node. */
public void setNext(Node newNext) {
next = newNext;
}

}

__________________________________________

ValueData.java: 

public class ValueData {
private String variable;
private double value;

// Define the constructor:
public ValueData(String variable, double value) {
this.variable = variable;
this.value = value;
}

// Define the getter and setter:
public String getVariable() {
return variable;
}

public void setVariable(String variable) {
this.variable = variable;
}

public double getValue() {
return value;
}

public void setValue(double value) {
this.value = value;
}

// Define the toString method:
@Override
public String toString() {
return "Variable: " + variable + ", Value: " + value;
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Map
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education