Searching ArrayList By Object Variable?

Oct 7, 2014

I'm almost finished my Bank Account exercise and I found myself stuck at the last part. Its asking me to add a method that asks the user to input the name of the account into which they want to deposit money, then search the ArrayList for that account. If it is found, the user is asked how much money they wish to deposit.

I already have my deposit method sorted so basically what I need is just searching through the ArrayList by the name variable. I assume its don't by iterating through with some form of for loop.Heres what I have:

import java.util.Scanner;
public class BankAccount {
private double balance;
private String name;
public BankAccount(double balance, String name){
this.balance = balance;

[Code]......

And the driver class

import java.util.ArrayList;
import java.util.Scanner;
public class BankDriver {
Scanner scan = new Scanner(System.in);
ArrayList<BankAccount> list;
public BankDriver(){

[Code]...

View Replies


ADVERTISEMENT

Searching For Object In Linked List Then Removing The Object

Nov 19, 2014

I have just started working with linked lists. I have a linked list of Objects and I want to be able to search for a specific object. But currently my code continues to return false. Also how would I go about removing the first index of the linked list.

public static void main(String[] args) {
LinkedList<Cookies> ml = new LinkedList<>();
int choice = 0;
while (choice >= 0) {
choice = menu();

[Code] ....

View Replies View Related

Searching ArrayList Of Objects

Mar 23, 2014

Okay, I will be as succinct as possible. I am writing a rudimentary book store program for homework. The program consists of two class files and a main. The main issue I am having is on getting the search portion of my program to work. I will post as little as I can and still make sense. This is the block of code belongs to my public Book getBook() method, which is of the BookStore class.

View Replies View Related

Searching ArrayList - Cannot Find Symbol For Search Method

Mar 30, 2014

This is the code fragment I have for searching my ArrayList. Now, each contact is stored in the ArrayList with five elements (first name, last name, etc.) and they're Strings. The error I get when I try to compile my program lies within this code fragment. It says it cannot find the symbol for the search method. I'm not quite sure what to do with this error.

int foundIndex = SAAddressBook.search(aBook);
System.out.println();
if (foundIndex > -1)
aBook.get(foundIndex).displayContact();
else {
System.out.println("No Entry Found");
}

View Replies View Related

Searching For Object In LinkedList?

Nov 3, 2014

I'm doing LinkedList at the moment and I'm having a bit of trouble with my assignment. The part I'm struggling with is remove an employee from a training course (as specified by their employee number),

what I'm confused about is iterating through the linked list to find the employee we're looking for. What I would do in this situation if I was using an array list is

for(Employee emp : myList) {
if(emp.getEmployeeNumber().equals(searchedNumber)) {
remove from training course..
break;
}
}

"Can only iterate over an array or an instance of java.lang.Iterable" is what it is telling me, and I can't figure out why/how its done differently for linked lists.

View Replies View Related

Populating ArrayList Object With Nested ArrayList Object

Jul 8, 2014

I have stumbled onto a problem with ArrayLists (not sure if nested ArrayList objects would be a more accurate description) ....

As simply put as I can describe it, I have an Inventory class which creates an ArrayList of a Product superclass, which has two subclasses, PerishableProduct and ItemisedProduct.

By default, the program is meant to have a starting inventory, which is why I have added them in the constructor

public class Inventory {
private List<Product> products;
public Inventory() {
addProduct(new Product("Frying pan", 15, 20));
addProduct(new PerishableProduct("Apple", 5.8, 30, 7));
addProduct(new ItemisedProduct("Cereal", 5.8, 0));
// this is where I am having problems. Wanting to add
// objects to the ItemisedProduct's ArrayList for cereal.
}

Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers.

public class ItemisedProduct extends Product {
private ArrayList<String> serialNumbers = new ArrayList();
public ItemisedProduct(String name, double price, int qty) {
super(name, price, qty)

[Code] .....

My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.

View Replies View Related

Object Reference Variable Unwilling To Be Reset To A Different Object Type

Nov 6, 2014

I don't understand why the object reference variable 'a' cannot be recast from a thisA object reference to a thisB object reference.Is it the case that once a reference variable is linked to a particular object type then it cannot switch object types later on.I am facing the Java Associate Developer exam soon and I am just clearing up some issues in my head around object reference variable assignment,

class thisA {}
class thisB extends thisA { String testString = "test";}
public class CastQuestion2 {
public static void main(String[] args) {
thisA a = new thisA();
thisB b = new thisB();

[code]....

View Replies View Related

Count Vowels In ArrayList Of Strings - N Cannot Be Resolved To A Variable

Sep 11, 2014

What I'm trying to do here is to count the vowels in an arraylist of strings. What I did may not be right, but that's not my problem for now. My problem is that i cannot return the value (n) I want to return. I don't know why.

import java.util.*;
import java.util.Arrays;
public class One {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("aaa");
list.add("brr");
list.add("unn");
System.out.println(vowels(list));

[URL] ....

View Replies View Related

Old Object Take Variable From New

Apr 30, 2014

I have a problem with my code. When, on strausParser class, i create the new object Beam i give him an array of Node object called "nodiestremi".

The problem is that when a Beam is created, the Beam created before take the Node of new Beam. Why it appends?

public class Main { 
public static void main(String[] args) throws NumberFormatException, IOException{
SetFile.setupWindows();
Structure structure = new Structure();
}
public class SetFile {
}

I get an output like this:

Import node complete
0 0 0 1
0 1 1 2
0 1 1 0
0 2 2 4
Import beam complete

and not like this:

Import node complete
0 0 0 1
0 0 1 2
0 0 1 0
0 0 2 4
Import beam complete

View Replies View Related

How To Add In Arraylist Of Object

Apr 7, 2015

I'm creating a card game assignment... so i have an arraylist called cards that has 20 cards and every card has contains 2 objects, suit and the point Value.

I shuffled the deck, now i want to add half of it to player 1 and the rest of the cards goes to the bot or computer.

how can i add the cards to the player one arraylist and have all the information of the cards?

here is my Deck class code :-

public class Deck {
private ArrayList cards;
private int size;
private ArrayList player1;
private ArrayList bot;

[code]....

the problem i have is this one doesn't work

size = cards.size() / 2;
for (int i = 0; i < size - 1; i++) {
player1.add(cards.get(i));
}
for (int s = size; s < cards.size() - 1; s++) {
bot.add(cards.get(s));
}

View Replies View Related

Editing ArrayList Object

Jan 30, 2015

So I'm Half way done with this assignment and all I need to is edit and sort my directory. What I've been trying to do as of now is edit my directory. I've tried to use the set function in the Array List, Iterator List etc but I just don't know how implement them mainly because I keep thinking "how can check which variable in the directory the user wants to change(Name, cost etc etc)?".

Main class

Java Code:

package plantnursery;
import java.util.Scanner;
import java.util.ArrayList;
public class PlantNursery {
private ArrayList<Plant> plantDirectory = new ArrayList<>();
private static Scanner read = new Scanner(System.in);

[Code] .....

View Replies View Related

One Object / One Instance Variable - Different Values?

Feb 5, 2015

Let's pretend I'm working on an RPG. Like in all RPGs, there are items found all throughout the imaginary world. Each player and NPC can obtain an item. My question will concern those items.

In other words, I'd like to use instances of a class in multiple places of the code. Each instance will have its own, individual values of instance variables, with one obvious exception: itemQuantity should have a different value in playerInventory, npcInventory, etc. Also, I'd like a list of all items that can be found in the game. This list doesn't need itemQuantity at all.

class Items {
String itemName;
float itemWeight;
int itemQuantity;

[Code] ....

The question is: should I really make itemQuantity an instance variable of the Item class? It seems as though for each copy of the Item class I should create a separate copy with different value of itemQuantity, but that's not very efficient. Where is the error in my logic?

What's important is that there may be plenty items in a game and a player may be given power to create new items during the course of the game.

View Replies View Related

Launcher References Object Variable

May 22, 2015

So I set out to write a program that takes two things from user: Name and Age

Then prints out "Name is Age"

I went through using a "launcher" and having a proper object: [URL]

The class names are Practice and Practice Launcher because I just use a Practice file as a sandbox environment so I understand its not correctly named. I also understand my comments aren't great but I'm just trying to make it work.

Practice.java
public class Practice {
//constructor
public Practice (String a, int b) {

[Code]....

My Practice.userName doesnt reference the variable userName. Why is this?

Also y does this line need Practice twice?
Practice Practice = new Practice(userName, 45);

View Replies View Related

Print Contents Of Arraylist In Object

Nov 7, 2014

i have a class that creates an object made of 2 strings and an arraylist

class Users{
private String ip;
private String userName;
private ArrayList <String>cons=new ArrayList();
Users(String i_p,String un,ArrayList con){//construc

[Code] .....

This object then gets returned to an arraylist of these objects

static ArrayList<Users>users=new ArrayList();

My question is how can i then get the arraylist from this object to print its contents?

View Replies View Related

Finding Object Containing Given Property In ArrayList?

Oct 12, 2014

I have got the code listed below:

class Address {
String name;
String street;
String city;
String state;
String code;
  Address (String n,String s, String c, String st, String cd){

[code]....

Using JDK7 is there a way to get an object that has got a specific property from the collection? For instance, I might want to seek if there is an Address containg "Tim Carlton" in the ArrayList.

View Replies View Related

How To Check If ArrayList Of Object Contains A Certain String

Mar 25, 2015

I have a number of objects stored in an ArrayList called inventory. Let's say I have two objects inside.

inventory.add(new Lamborghini(2011, "aventador", 411.3, false));
inventory.add(new Lamborghini(2012, "sesto elemento", 512.3, true));

I am making a function to search through the whole inventory to see if any of the Lamborghini object has a certain model name such as aventador, diablo, etc....

This is what I have but I figured there's a big mistake when I make it true / false; it's making it going through the list and what's return is the last one instead of saying there's such match in the whole list or not.

public boolean hasCarModel(String modelName){
boolean exist = false;
for (Lamborghini lambo : inventory){
String carModelName = lambo.getModelName();
if(carModelName.equalsIgnoreCase(modelName)){

[Code] ....

I figured if I add break; under exist = true; it'll work because as soon as it found one match then it'll turn to true and break out the loop but I don't think this is the best way to do it right?

View Replies View Related

Accessing Object Methods Within Arraylist

Oct 14, 2014

Scanner in = new Scanner(System.in);
ArrayList<rand> selectedRand = new ArrayList<Rand>();
selectedRand.add(new Rand(in.nextLine()));

I have created the most minimal code for creating an array list. I was wondering what the basic syntax of accessing objects methods that are within an Array List. So if I was to trying and get a method such as [.returnValue,] how would this look within a Rand object that is declared in a Array List Since you cannot simply declare a new Rand object and say:

newRandObject.returnValue();

And you must go through the actual slotted portion of the array list. I have searched the web and my text book for an example however none are provided.

View Replies View Related

Copy ArrayList Object Into Array

May 29, 2014

package com.practice;
public class Car {
private String name; //name of the car
private String modelName; //Name of the model
private int year; //The year car was made in
private int speed=0;

[Code] ...

It wont let me copy it into a array is there any solution to this.

View Replies View Related

Getting Max Volume From ArrayList Of Type Object

May 27, 2014

I want to get the max volume from a file that I stored in an arraylist but it don't know if this is the way to do it. also I don't know how to print the method in the main method. here is the method that will get the max volume

public Shape3D maxVolume(ArrayList<Shape3D> shapes ){
Shape3D currentMax;
int currentMaxIndex = 0;
for ( int i = 1; i < shapes.size(); i++)

[Code] ....

This is my shape3D class

public abstract class Shape3D implements Comparable<Shape3D>
{
private String type;
public double radious;
public double height;

[Code] ....

View Replies View Related

What Does Creating ArrayList Object In A Constructor Mean

Mar 22, 2014

import java.util.ArrayList;
public class LectureRoom{
private String courseName;
private String roomNumber;
private String Lecturer;
private ArrayList <Student> studentList;

[Code] .....

Question:

Given the following BlueJ class diagram

Lecturer class (same with previous lab, no changes needed)
Student class (same with previous lab, no changes needed)

LectureRoom (changes occurs here)

1. LectureRoom has roomNumber (e.g. A301), courseName (e.g. Java), lecturer (a reference to a Lecturer object), and studentList (a reference to an ArrayList that stores Student object).
2. LectureRoom has a constructor that receives courseName, roomNumber, and Lecturer. The constructor then sets/assign the courseName, roomNumber and Lecturer. This constructor also creates the studentList arraylist object.

View Replies View Related

ArrayList And Object - How To Make A Menu

Jun 26, 2014

So i have a problem i want to make a menu that allows you to press 1 and then make lets say a Car,

The code would look like

Car car = new Car();

And then add to my list

cars.add(car);

If in my menu the person presses 1 again, will they add another car or will it override it

Also: how can i make a menu that doesnt allow you to press option 5 (something that requires you something that should be registered in other options ), until you registered them?

View Replies View Related

How To Grab Graphical Object From Arraylist And Modify It

Oct 26, 2014

I am trying to grab a graphical object from an arraylist, and reposition its coordinates on a Jframe when adding it. My program of course deals with strings, and once it sees specific words in my console, some method is called that adds, removes, or otherwises modifies certain objects on screen.

Here I want to say something like move(object[1],xpos,ypos) which will move a certain object from a specified point in the array, and move it to new x and y positions on the JFrame. I use a different class that extends a graphics program, so when I say add(something,x,y) it draws the object onscreen where I want it. These are some relevant, though incomplete, methods that should move an object already painted on screen:

Console class

Java Code:

public void doMoveCommand(String cmd, String arg, String xpos, String ypos) {
int x = Integer.parseInt(xpos);
int y = Integer.parseInt(ypos);
if (cmd.equals("posMake") && arg.equals("star")) {
box.moveStar(box.historyG.get(1), x, y); //historyG is an arraylist of GPolygons
freeCommand();

[Code] ....

When I say makepos(whatever) I am getting a arraylist out of bounds exception. How I might be able to accomplish moving objects already on screen?

View Replies View Related

Link Objects And Calling A Variable Object Method

Jun 14, 2014

Say I have two classes, Author and Book, and I have 2 author objects and 10 book objects. I would like to know how to do two things:

1) Make some sort of connection that makes clear that author X wrote books A, B and F.
2) Call a method from a book object that is connected to an author.

Seeing as I don't know which books will be connected to an author, is there some way to call a method of an object bases on a variable object name? Something like call the getFirstPage() method for every book that is linked to author X?

View Replies View Related

Sort ArrayList By Object Field WITHOUT Using CompareTo Methods?

Mar 9, 2014

I have a project where I must sort a collection of songs by a number of fields: year, rank, title and artist. in the project, we must use certain methods and we cannot add others without getting marked down. Here are the specific requirements:

Sorting

The -sortBy option will cause the output to be sorted by a particular field. If this option is specified, the output should be ordered according to the field named. If there are ties, the tied songs should appear in same order in which they were in the input file. If no -sortBy option is specified, the output should maintain the order of the input file.

public void sortYear()

Order the songs in this collection by year (ascending).public void sortRank()
Order the songs in this collection by rank (ascending).public void sortArtist()
Order the songs in this collection lexicographically by artist (ascending, case-insensitive).public void sortTitle()
Order the songs in this collection lexicographically by title (ascending, case-insensitive).

Here is the relevant code:

SongCollection Class
Java Code: import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.*;

[code]....

View Replies View Related

Accessing Arraylist Member Object Methods In Enhanced For Loop?

Feb 24, 2014

I've tried a couple ways to do it, and they don't work. I'm aiming for functionality like I got with the regular for loop, but from an enhanced for loop. Is this simply beyond the scope of an enhanced for loop, or am I just not getting the right syntax?

TestObject to1 = new TestObject("first", 11);
TestObject to2 = new TestObject("second", 12);
TestObject to3 = new TestObject("third", 13);
TestObject to4 = new TestObject("fourth", 14);
TestObject to5 = new TestObject();
List<TestObject> testList;
testList = new ArrayList<TestObject>();

[code]....

The TestObject class is simply an int and a String, with getters getInt and getString. It all works fine with the regular for loop.

edit: I should probably mention that I know what I have in the enhanced for loop now will only display the class name and the hash. I've tried adding the .getString and .getInt, and tried a few other ways to make it work. I just reverted to this because it compiles and runs

View Replies View Related

Difference In Variable Assignment Between Constructor And Variable Section

May 21, 2014

Given the case I have an object which is assigned only once. What is the difference in doing:

public class MyClass {
private MyObj obj = new MyObj("Hello world");

private MyClass(){}
//...
}

and

public class MyClass {
private MyObj obj;
private MyClass(){
obj = new MyObj("Hello world");
}
//...
}

Is there a difference in performance or usability? (Given also the case there are no static calls expected) ....

View Replies View Related







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