Printing A List From Different Method
Sep 21, 2014
Right, so I got this method that creates and sorts 2 lists. What I want to do is merge these lists into a third list, have it sorted, and then print the contents of the list. The problem is, I'm tired and I don't remember how I can print it.
import java.util.*;
public class MergeTwoSortedListWilson {
public void CHANGEME() {
// To do
ArrayList<String> aList = new ArrayList<String>();
aList.add("Banana");
[Code] ...
MergeTwoSortedListWilson.java:35: error: cannot find symbol
System.out.println(aList);
^
symbol: variable aList
location: class MergeTwoSortedListWilson
1 error
View Replies
ADVERTISEMENT
Feb 20, 2015
I'm trying to print sentences from a list which read in sentences from a text file. I'm able to get the program to print out the sentences in a numbered format corresponding to each sentence, but the first sentence in the list is always repeated, same as the next sentence. How can I print out the contents of a list in a numbered format (1. _ 2. _ 3._) without repeating sentences?
Here's my code:
package jacsim;
import java.util.Scanner;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.io.FileInputStream;
[Code] ....
The output looks like:
Input Sentences:
1:The cat in the hat
2:The cat in the hat The cat sat on the mat
3:The cat in the hat The cat sat on the mat Pigs in a blanket
Sorted Single Arrays:
I'm trying to make it look like:
1. The cat in the hat
2. The cat sat on the mat
3. Pigs in a blanket
View Replies
View Related
Feb 17, 2015
I'm trying to print the contents of my linked list. I'm using nodes and within those nodes it hold String data. So i want to print out the data within the nodes. Whenever i do
System.out.println(node1.data),
it prints perfectly. But i'm trying to use a method where it would loop through the list and print out the data for every node in it. when i run my print method i get results such as
Node@15db9742
Node@6d06d69c
Node@7852e922.
Here is my print method i created
Java Code:
public void print(){
Node<E> current = head;
while (current.next != null){
System.out.println(current.data);
current = current.next;
}
} mh_sh_highlight_all('java');
View Replies
View Related
Sep 30, 2014
Its a program that creates a singly linked list that stores names and high scores and prints them. For some reason it is printing an entry extra times. Also my remove function is not working properly.
package project;
public class GameEntry implements Comparable<GameEntry> {
private String name;
private int score;
public GameEntry(String n, int s) {
name = n;
score = s;
[code]...
View Replies
View Related
Jun 15, 2014
I am having a hard time trying to figure out how to print random numbers from a an array list. I tried google but nothing worked. I have to pick certain values from two lists and print them on the screen. I have included comments in the code to facilitate the explanation.
import java.util.Random;
public class Parachute {
public static void main(String[] args) {
Random randomNumbers=new Random();
int number;
int array []={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
char A[] = {'a', 'b', 'c','d','e','f','g','h', 'i','j','k','l','m','n','o','p','q'};
[Code]...
View Replies
View Related
Nov 3, 2014
The only problem I have now is getting a method to return the median element of a LinkedList without using loops of any kind or by using a global counter anywhere.
I've pretty easily figured out how to get the index value for the median number (there is some lee way allowed. If the list has an even size, any of the middle values are accepted) but I can't figure out how to print it without loops.
I'm sure I need to make a method that finds an element at the given index value, but I don't know how to do it without loops.
Here's all of my code. Inside is my Assignment3 class I use for testing, StudentList which contains the LinkedList head and other List methods, and StudentNode which is obviously, the Node class. Also I've attached the first test1.txt file as well.
import java.io.FileNotFoundException;
import java.util.*;
public class Assignment3 {
public static void main (String []args){
StudentList<StudentNode> myList = new StudentList<StudentNode>();
[Code] .....
I tried making a method that basically counts up the list recursively then a second method that counts down recursively and is supposed to stop once it hits the middle number, then print that node.
Attached File(s) : test1.txt (106bytes)
View Replies
View Related
Apr 27, 2014
I need to print certain information from an arraylist depending on the arguments the method has. I'm trying to print the last 3 methods from the database class but nothing is printing and I don't know what the problem is.
package hw3;
import java.util.*;
public class Driver {
public static void main(String[] args){
/* Comments required
PersonFileReader pfr = new PersonFileReader("person.dat");
ArrayList<Person> testData = pfr.readData();
Database db = new Database(testData);
*/
[Code] .....
On the driver class I just print what the methods from the database class do.
View Replies
View Related
Nov 23, 2014
KtMok1t.jpg
Below is what I go so far, but how to do star C and E.
public class PrintTriangle
{
public static void printStars (int star)
{
for (int number = 0; number < star;number ++)
{
System.out.print("*");
[Code] ....
View Replies
View Related
Jan 30, 2015
Here is the java code that i compiled on eclipse but each time i run it i am getting different sequence of output !!
What I did is that my main() calls a function m1() which calls a function m2() which throws an exception back to m1() which throws exception back to main().
public class ClassB {
public static void main(String[] args) {
try {
m1();
} catch (Exception ex) {
ex.printStackTrace();
[Code] ....
The output is as shown in screenshots at different times :
SCREENSHOT 1:
SCREENSHOT 2:
SCREENSHOT 3:
How is this possible ??
View Replies
View Related
Mar 17, 2015
I have wrote the necessary program for the class which was : Modify the customer class to include changeStreet(), changeState(), and changeZip methods. Modify the account class to include a changeAddress() method that has street city and zip parameters. Modify the Bank application to test the changeAddress method.
The problem arose when I went to test it. For some reason when it asks "Would you like to modify your account information? (y/n)" it will not allow the user to input anything and thus test the class. Here is my code
Class Customer
import java.util.*;
import java.io.*;
public class Customer {
private String firstName, lastName, street, city,state, zip;
[Code] ....
View Replies
View Related
Nov 13, 2014
My issue is that when I run my search, it does find a goal. However, when I try and print the route using my print route method, it just gets stuck in a loop of printing the same two nodes. What is wrong with My A* implementation?
package search;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
[code]....
View Replies
View Related
Apr 13, 2014
I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Hours Traveled ");
int hoursTraveled = input.nextInt();
System.out.println("Enter MPH ");
int mph = input.nextInt();
[Code] .....
View Replies
View Related
Mar 28, 2014
I had to write a program that prompts the cashier to enter all prices and names, adds them to two arrays lists, calls the method that I implemented, and displays the result and use 0 as the sentinel value. I am having difficulty coming up with a for loop in the method, I believe I have the condition right but I am having trouble with the statements. I now know that String does not have the get property, but I have only done examples with integers and I am not very good with for loops and wouldn't know how to fix it.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Double> sales = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
System.out.print("Enter Number of Customers");
double salesAmount;
System.out.print("Enter Sales for First Customers");
salesAmount = in.nextDouble();
while(salesAmount != 0)
[code]....
View Replies
View Related
Dec 30, 2014
Is there a particular implementation of a linked list you are referring to in your question?
View Replies
View Related
Apr 26, 2015
Implementing the add(int i, E o) method for a SinglyLinked List.
public void add(int i, E o)
// pre: 0<=i<= size()
// post: adds ith entry of list to o
View Replies
View Related
Jul 16, 2014
I'm still working with the singlylinkedlist data structure and trying to return a string of individual characters stored in each node. ( head--('J')---('A')---('V')---('A')---tail ) Hopefully this beautifully executed depiction to the left will clarify.
This is what I came up with after designing the algorithm w/ pen and paper. I know that I'm not accounting for the OutOfBound errors, an empty list, or an index < 0.... I will get to that.
Right now, I'm not sure why my assignment to the character array, ' chars[i] = cursor.getLink(getElement()); ' , is not working. The two methods getLink and getElement, type Node and T, respectively, exist in my Node class which is a private nested class in MySLList. Why would I be getting the following error: "The method getElement() is undefined for the type StringX" ? Is this a good design and implementation of the substring method?
public String substring(int index) {
char[] chars = new char[(getSize() - index)]; //getSize() defines the size of list in MySLList
Node cursor = null;
//Set the cursor to the node = index
if(cursor == head) {
[Code] ....
View Replies
View Related
Jan 27, 2015
Write the following method that shuffles an array list of numbers.
public static void shuffle(ArrayList<Number> list)
package shuffleArrayList;
import java.util.ArrayList;
import java.util.Arrays;
public class ShuffleArrayList
{
public static void main(String[] args)
[code]....
View Replies
View Related
Oct 21, 2014
So I have to write all the methods for a LinkedListQueue. I've got isEmpty, enqueue and dequeue working correctly (I think) but I'm having trouble with the toString method. I tried to do it recursively and it works if there is only one element in the list, but with multiple elements it throws a StackOverflowerror exception. I've tried it multiple different ways, but I can't seem to figure out how to print it out with out clearing everything. We haven't been taught StringBuilder or .append yet, which I saw a lot of as I was looking for solutions, so I can't use those.
public class LinkedQueue<T>
{
protected LLNode<T> front; // reference to the front of this queue
protected LLNode<T> rear; // reference to the rear of this queue
private T info;
public LinkedQueue()
{
front = null;
rear = null;
[Code] ....
and this is the ITD used with it, for some reason it has the "empty the queue" function as a choice but we weren't assigned that function, so just ignore it.
import java.util.Scanner;
public class ITDLinkedQueue
{
public static void displayMenu()
{
System.out.println("(1) display menu");
System.out.println("(2) check isEmpty");
System.out.println("(3) enqueue");
System.out.println("(4) dequeue");
[Code] ....
View Replies
View Related
Apr 9, 2014
I am trying to put a reference to a given subclass object into a linked list, and then come back later, and invoke a method of the subclass object that is in a given spot in the linked list. This produces an error because Object does not have that method. Is it necessary to cast the object to the correct subclass every time I want to use one of its methods, or is there a way to convince the JVM to treat it as always of type MySubclass?
View Replies
View Related
Jun 7, 2014
I'm trying to write an indexOf() method that will return every time a value occurs in a linked list. I need to use my user-created linked list not the built in Java linked list. For example in a linked list of characters: "i, p, z, z, n, d, p, z" when I search for "z" it should return position variables for 3, 4, and 8. Currently what I have is obviously only returning the first instance.how I can return more than one instance?
public int indexOf(char input) {
LLNode currentNode = this.first;
int position =1;
boolean found = false;
[code]...
View Replies
View Related
Dec 1, 2014
So I have created a array list of pizza toppings, only 4 in total, and each ingredient needs to have a cost property. I'm supposed to access the cost through a get / set method, as the user can create their own pizza and it will total up the cost, but how to.
View Replies
View Related
Apr 23, 2015
I am trying out solving the question but i am stuck.The problem is to write a method that print data of single linked list backward using stack.The question is as follow
public class Stack{
public boolean isEmpty(){};
public void push(int n){};
public int peek(){};
public int pop(){};
}
public class node{
int data;
node next;
}
public class list{
node first;
}
View Replies
View Related
Oct 6, 2014
I'm having trouble with sorting Strings- 3 strings inputted by user, and I would like to output them in alphabetical order. I've used the str.compareToIgnoreCase method, and then I've tried to loop them through a series of if/ else statements. Everything I've been able to find online (including the forums here) has suggested to use the Comparator class, or to put the strings into an array, and sort list- I really would like to stick with just the String class, and its methods .
The program itself works and compiles, but I am getting logic errors that I have been unable to solve. I'm using IntelliJ Idea, and I've ran it through the built in debugger, about 100+ times (not exaggerating, lol) just to see what it's doing in particular scenarios. For instance, I can get c, a, b, to print out as a,b,c correctly, but a,b,c, will print out as b,a,c.
For me this is kind of like a Sudoku puzzle, or a Rubik's cube! Each time I fix one scenario, it breaks another one, so I don't know if there's a(logic) solution to fix all possible scenarios (abc, acb, bac etc... to all print abc) or if possibly I just need more if statements. I've only pasted in the area where I'm having problems (the if statements). I'm a big fan of the "Next Line" syntax.
(Note: please assume the non relevant content- import Scanner class, main method, etc... I didn't want to paste the entire program.)
System.out.println("Enter the first statement: ");
//input.nextLine();
string1 = input.nextLine();
System.out.println("Enter the second statement: ");
string2 = input.nextLine();
System.out.println("Enter the third statement: ");
string3 = input.nextLine();
[Code] ....
View Replies
View Related
May 5, 2014
This program contains a superclass and a subclass that will gather the following information from the user:
name, address, phone number, and customer number.
Everything works fine except that I have to create a boolean method in this program that is required to determine based on user input whether they want to be added to a mailing list.
I cannot get this method to work with main, each time it is called it will always return the value but main will constantly read the last statement (else, where it will read "not wanting to be added to the mailing list).
The only way I can get this part of the program to work is by adding an equals method in main that ignores the case, but I am required to write a boolean method so this is not allowed.
Superclass:
public class Person
{
private String name;
private String address;
private String phoneNum;
public Person(String pName, String add, String number)
{
name = pName;
address = add;
[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