Array List And Method

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


ADVERTISEMENT

Method That Shuffles Array List Of Numbers

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

Array List Of Pizza Toppings - Access Cost Through Get And Set Method?

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

Public Method That Takes Array Of Type Object To Load Strings Into Linked List

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

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

Insert Method Of Linked List

Dec 30, 2014

Is there a particular implementation of a linked list you are referring to in your question?

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?

View Replies View Related

Implement Add Method For Singly Linked List

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

Substring Method And Singly Linked List

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

Linked List Queue ToString Method

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

Invoking Subclass Method On Object In A Linked List

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

Writing IndexOf Method For More Than One Instance In A Linked List

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

Generate 10 Random Integers / Store In Array And Then Calling A Method To Display Array

Nov 8, 2014

So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:

public static void displayArray(int[] array)

This is what I have done
 
public class RandomIntegers {
 static int numbers = 0;
public static void displayArray(int[] array) {
System.out.println(numbers + "Numbers Generated");

[Code] .....

View Replies View Related

Method That Returns New Array By Eliminating Duplicate Values In Array

Jun 15, 2014

Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.

Here is the code:

Java Code:

import java.util.Scanner;
public class Exercise06_15 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter ten numbers: ");

[code]....

View Replies View Related

Array Initialization Method - Filling Entire Array With Last Input Value

Feb 7, 2015

I am passing input from the user to a method that will initialize an array of the data (scores in this case). The method is filling the entire array with the last input value.

array initializer method

Java Code:

public static float[] inputAllScores(float validScore) {
float[] diverScores = new float[7];
for (int i = 0; i < diverScores.length; i++) {
diverScores[i] = validScore;
}
return diverScores;
} mh_sh_highlight_all('java');

[Code] .....

View Replies View Related

Formatting Array List?

Oct 16, 2014

How do you format an arraylist?

Mine looks like this:

[<?xml version="1.0" encoding="UTF-8" standalone="no"?> <DefEnv id="Dev">, <Envt id="Test">, , <DB id="DM">,

But I want it to look like: I'd prefer if the '[' , '<>' and ',' were not on them also but I'm not too bothered about that bit.

[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DefEnv id="Dev">,
<Envt id="Test">, ,
<DB id="DM">, ]

View Replies View Related

Cannot Read Into Array List

Apr 5, 2014

I am creating a hangman game and I want to read in a list of words from a text file, but after the user inputs the name of the text file. I get 'Exception in thread "main" java.lang.NullPointerException'.

Here is the code, where I think the problems lie.

public void runModel(){
ArrayList<String> pirateWordsList = new ArrayList<String>();
System.out.println("What is the name of the file you would like to load? (The file included is called piratewords.txt'");
Scanner in=new Scanner(System.in);
String file=in.next();
load(file);

[Code] ....

The full error message is this:

Exception in thread "main" java.lang.NullPointerException
at uk.ac.aber.dcs.pirate_hangman.Model.load(Model.jav a:108)
at uk.ac.aber.dcs.pirate_hangman.Model.runModel(Model .java:45)
at uk.ac.aber.dcs.pirate_hangman.Main.main(Main.java: 6)

View Replies View Related

Highest Value In Array List?

Feb 2, 2015

I have 3 classes Pet, Cat and Dog classes. Cat and Dog classes are childs of Pet class.

Each pet has the properties of

private String myName;
private int myX;
private int myY;
private int mySpeed;
private int myAge;

I have an array list which creates 100 dogs. How can I print out the dogs with the two highest age values?

View Replies View Related

Alternatives To Array List?

Nov 23, 2014

Im trying to create a program in which I read line by line the contents of a text file, and then report each letter along with its frequency. I was wondering how to read through the lines and process it so that my program knows to increase by a number each time a letter appears in my text file. For example, if A appears in my file 5 times, B 3 times, and C 0 times I want to eventually print out

A -- 5
B-- 3
C-- 0

My first thought was to do this using array lists but is there any way I could do this without using one?

View Replies View Related

Why Array List Is Needed

Apr 28, 2015

method called []getLetterGrades but the only hint My professor told me was that I needed to declare another array list for this method and he wouldnt tell me anything else so bummer. But I don't understand why if what we are returning is a char. It would make sense to return an array list of char to get letter grade. Which is what i did but since the function is a char, the array list character wont work as a return.Primarily i would like to know the type that is needed. I just want an explanation for an array list in this method and how it would serve in this method.

import java.io.File;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

[code]....

View Replies View Related

Sort Both Array List

Jun 29, 2014

Directions: public static void initialize(ArrayList names, ArrayList sores)

You should write a function that sorts both array lists, based on the values in the scores array list. This is one of the more conceptually challenging parts of the assignment. You want to sort the scores array list, and simultaneously alter the names array list so that the names continue to line up with their respective scores by index. In the example data above, when the score 9900 moves to the first element of the scores array list, the name "Kim" should also be moved to the top of the names array list. The function should have the following signature:

I'm having trouble figuring out how to sort the lists.

import java.util.ArrayList;
import java.util.Scanner;
public class Assignment5
{
/**
*/
public static void main(String[]args) {
intializeArrays();

[Code] ....

View Replies View Related

Method That Print Data Of Single Linked List Backward Using Stack

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

Removing Duplicates In Array List

Sep 18, 2014

I am stuck on this exercise and I don't know what exactly is wrong. I think it's something with the .remove and the for each loop, but I am not sure.

public class seven {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("aaa");
list.add("brr");
list.add("unni");

[Code] ....

This is what i get

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at seven.removeDuplicates(seven.java:24)
at seven.main(seven.java:18)

View Replies View Related

Enum Type And Array List

Feb 10, 2015

Here I have an enum class

public enum Money{

ONE_PENNY(1),
TWO_PENCE(2),
FIVE_PENCE(5),
TEN_PENCE(10),
TWENTY_PENCE(20),
FIFTY_PENCE(50),
ONE_POUND(100),
TWO_POUNDS(200);

private int coin;
Money(int c) {
coin = c;
}
int showCoin() {
return coin;
}

and for a test class, I need an array list with a couple of coins in it (i.e. ONE_POUND, TWO_POUNDS) and a loop that adds together the values of the coins in the list and prints the result. How can I do this?

View Replies View Related

Input From Console To Array List?

Dec 10, 2014

I have this very simple application just to test console input:

import java.util.ArrayList;
import java.util.Scanner;
public class WriteTester {

[Code]....

When I let it run, only every third entry is put into the array list and I have to hit "enter" three times for the "break" in line 21 to trigger. I cannot find out why.

View Replies View Related

Adding Array To A List - String?

Sep 8, 2014

Im making a simple code to add an array to a List (the code im referring to is <String> )

import java.util.*;
public class L5_ArrayListProgram {
public static void main(String[] args){
String[] things = {"lasers","ghouls", "food", "dark"};
List<String>list1 = new ArrayList<String>();
for(String x: things)
list1.add(x);

My simple question is - what are the <String> ...<String> for? I understand it makes the list1 variable a string, but why is it made like this? do we usualy use <String> when we need to make a variable a String?

View Replies View Related







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