ArrayLists Objects Checking

Jun 27, 2014

I have to make ask for 2 houses or more (INSTANSED CLASS, with 2 ints ) with the same size(int ), but i they cant use the same door number(int). how do i check in my ArrayList that they have different numbers while not caring about different size? .

The ArrayList is in a class called system. I dont know if i should try to Override the equal or try something else.

View Replies


ADVERTISEMENT

User Inputting Objects Into ArrayLists

Feb 25, 2014

I'm new to java and up until now whenever I have created an object it has been via the BlueJ interface. I would make an object manually and then manipulate it with my methods.

I have made a basic program which adds objects into arraylists via the BlueJ interface. However I now need to prompt a user to enter a string which should create the specific object and add to its ArrayList.

Is there a way to create objects without manual specifying the parameters? I can provide some of my code if it is needed I am trying to make a crude version of Plant vs Zombies for an assignment.

I am aware of the scanner class but I only know how to prompt a user but I feel like if I can figure a way to create the objects I can dynamically I can associate the string inputs to the proper arraylists.

View Replies View Related

Save Class With Arrays So As To Reload Them Again And Hold Onto List Of Objects Within Those ArrayLists

Dec 7, 2014

I have a class with static ArrayLists to hold objects such as Members,Players etc.I want to save the class with the arrays so as to reload them again and hold onto the list of objects within those ArrayLists.

The ArrayClass

import java.io.Serializable;
import java.util.ArrayList;
public class ArrayClass implements Serializable {

[code]....

The arrays within the ArrayClass are empty when i reload the application.I cant tell if the arrays are being properly saved or is it in the reloading from file???

View Replies View Related

Using Iterators To Compare ArrayLists

Sep 15, 2014

The question states:

>Suppose that aList and bList are instances of java.util.ArrayList. Use two iterators to find and display all the objects that are common to both lists. Do not alter the contents of either list. Write the segment of code assuming that the objects are of type String.

If my understanding is correct, I need to create two Iterators (one for aList and one for bList) to compare both ArrayLists and I should output any value that appears in both lists.

This is my attempt at it:

import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> aList = new ArrayList<String>();
ArrayList<String> bList = new ArrayList<String>();

aList.add("One");
aList.add("Two");

[Code] ....

The output:

`One`

`Two`

It stops there. It's clear that my loop is wrong, but I don't really know how to go about fixing it.

View Replies View Related

Use If / Else Statement That Depends On Which Of 2 ArrayLists Contain Object

Sep 9, 2014

I have two arraylists. One is personalContactList. The other is businessContactList.

1. I want to take user input that references an object attribute (int),
2. Use it to determine which object is referenced,
3. Find the "type" attribute of that object,
4. Determine which arraylist the object belongs to, based on that type,
5. Use the if else/statement to print out some attributes that depends on which arraylist the object is in.

I believe the code successfully does 1-3, and probably 4. But there is a hangup on 5. I get an indexoutofbounds execption. Which I'm not sure I understand very well.

Here is the code--it's part of a switch statement:

case "c": {
//some code here prints out a list
boolean b = false;
int g; 
while (b == false) {
  Scanner detailScanner = new Scanner(System.in);
System.out.print("Enter a number to see details: ");
 
[Code] ....

View Replies View Related

Between Two ArrayLists / How To Find Which Elements They Have In Common

Apr 6, 2015

I have two ArrayLists and I want to compare them for common elements, and based on the result I want to update the first Arraylist to only have these elements. sort of like the opposite of RemoveAll() which removes elements in common and keep the ones that are unique. so far I thought of using for loop and .contains() in case it was fault,element not present, remove from list. but I was wondering
in what other ways, perhaps APIs i can use to do that?

View Replies View Related

Code Which Adds Marks Into 2 Different ArrayLists

Jan 27, 2014

I have this piece of code, which adds marks into 2 different arrayLists, one for homework marks, and one for examination marks..

ArrayList<Double> homeworkMark = new ArrayList<Double>();
ArrayList<Double> examinationMark = new ArrayList<Double>();
boolean markCheck = true;
do{
//
the purpose of this try catch is to make sure that the entered mark is a valid number. If the program encounters an exception markCheck will become true and the loop will begin again asking the user to enter the marks. If the marks are all encountered correctly markCheck will be false and the loop will end.

try{
for (int i = 1; i <= amountAssignment; i++ ) {
sc.reset();
System.out.print("Homework Mark for Assignment " +(i) + ": ");
homeworkMark.add(sc.nextDouble());
System.out.print("Examination Mark for Assignment " + (i) + ": ");
examinationMark.add(sc.nextDouble());
markCheck = false;
 
[code]....

View Replies View Related

HashMaps / ArrayLists - Diamond Operator?

Jul 8, 2014

So I am learning HashMaps/Arraylists and I can't really understand the diamond operator or what it's for. Take the following code for example: Why could we not just do this without the diamond?

Java Code:

import java.util.HashMap;
class Untitled {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();

map.put("California","Sacramento");
map.put("Oregon","Salem");
map.put("Washington","Olympia");

System.out.println(map);
}
} mh_sh_highlight_all('java');

View Replies View Related

Using Command Pattern For Undo And Redo In ArrayLists

Apr 15, 2015

So I have a program where you can log in and add/remove friends to and from the friends arraylist. Also I can like a certain thing and that thing will be stored into the likes arraylist. I'm asked to make undo and redo options for whichever action I do.I want to add apple as a friend. After that when I select the undo option, I can undo that action so apple wouldn't be my friend. How I can approach this with a Command Pattern when the input is whatever name or word I inputted to store into the friends arraylist?

I did some research and found that using a command pattern could be my best bet since this has to be done under the Facebook Class I already have. I decided to add parts of what I have ...

In the driver program

Facebook facebook1 = new Facebook();

if (userInput == 6) {
System.out.println("Login");
String operand1 = getOperand("What is the Username? ");
String operand2 = getOperand("What is the Password? ");
System.out.println("Enter a friend to be added. ");
String operand3 = getOperand("What is the Username? ");
facebook1.friend(operand3);

[code]....

View Replies View Related

Linking Elements Of Two Arraylists Together For A Bubble Sort?

Jun 13, 2014

I have been doing a program that allows the user to input students names and numerical grade values. I have already completed 3/4 of the program but am stuck on the bubblesort method as this is what it requires:

The program must have an indefinite loop, which prompts the user to select a sorting criterion or to end the program, and must also use bubble sort. Note: The user may either select name or grade as the sorting criteria. The program must use bubble sort to sort that data according to the specific criteria and then use another loop to display the data. This process must continue until the user ends the program.

My problem is, while i can easily do the bubblesort, where to look on how to link elements of the two arraylists I'm using (one for grades, one for names) so that when the user decides what sort they want, the individual's name and grade stays together.

View Replies View Related

Drawing A Graph Based On Stock Values From 2 ArrayLists?

Apr 19, 2015

[URL] I made a program that takes 2 stock values by URL tickers, and now I need it to draw a graph with the values for the 2 stocks, so they can be compared visually. So far my idea was to create 2 arraylists that consist of the values for each stock, so that I can draw the graph so that x always move with 1 step, and y0 is the first coordinate, and y1 is the second, and after that y1 is the first, y2 second etc.

View Replies View Related

Text Based RPG Game - Working With Multiple ArrayLists

Feb 22, 2014

I have come across an issue with arraylists. I am writing a text based RPG game as something to start with ...

Initially I had a single zone which was all stored in an arraylist and everything was working in regards to the player moving around. The problem I now have is how to add further zones to my game. Ideally I would like an arraylist for each zone, and would use the below to create each arraylist

public static ArrayList<RoomsClass> castleMap = new ArrayList<>();

The problem I now have is how to handle the player moving, initially with a single zone/arraylist I could reference that arraylist directly

public void findRoomCoords(int ID) {
for (int i = 0; i < castleMap.size(); i++) {
if (castleMap.get(i).roomID.equals(ID)) {
PLAYER.setCurrentRoomZone(castleMap.get(i).roomZone);
PLAYER.setCurrentRoomX(castleMap.get(i).roomX);
PLAYER.setCurrentRoomY(castleMap.get(i).roomY);
PLAYER.setCurrentRoomZ(castleMap.get(i).roomZ);
}
}
}

My initial thought was to use a getter/setter to remove the reference of castleMap from my movement code in order to access different arraylists, however this is where things have fallen over, I can't seem to work out how to get the arraylist name to change, depending on the outcome of the setZoneMap() method.

public void setZoneMap() {
switch (PLAYER.getCurrentRoomZone()) {
case 0: {
zoneMap = Castle.castleMap;
break;

[Code] ....

View Replies View Related

Populate DB From Another Using ArrayLists And Arrays - Index Out Of Bounds Exception

Mar 16, 2015

I have an issue with an IndexOutOfBoundsException. I am trying to populate a db from another using arraylists and arrays. I can get the data but the program fails when trying to run the inserts. I am trying to perform SQL in batches of 5. I have added a comment to the failing line.

/*Set in code at beginning*/
ArrayList<String[]> privacyList = new ArrayList<String[]>();
ArrayList<String[]> statementBuffer = new ArrayList<String[]>();
 
/*Some sql is performed and the following String array is populated*/
while (rs.next()) {
String[] row = new String[55];
resultSetIsEmpty = false;
row[0] = rs.getString("ID");

[Code] ....

View Replies View Related

Copying Data Objects To Serializable Objects

Jul 27, 2014

I am currently working on a project where I need to return data from a database over RMI to a client who requests it. Some of the fields in the Data Object can not be seen by the client so I need to create another object to send over the network instead. The method I use is this...

public static SerializableObject createSerializableObjectFromDataObject(DataObject dataObject){
SerializableObject serializableObject = new SerializableObject();
serializableObject.setField(dataObject.getField());
serializableObject.setAnotherField(dataObject.getAnotherField());
return serializableObject;
}

Is there a better way of doing this? I am creating many subclasses DataObject which all require this static method to be implemented and I can't push it into the superclass because each one needs custom behaviour.

View Replies View Related

Checking If Int Is Lowest Out Of 4?

Apr 30, 2014

I'm trying to find the int with the lowest value, and I only know how to set it up like this:

Let's say x1 is the lowest.

int x1, x2, x3, x4;
 
if (x1 < x2 && x1 < x3 && x1 < x4) System.put.println("x1 is the lowest value out of those 4");

Is there a way to shorten this at all? (It's alright if there isn't, just looking for shortcuts)

View Replies View Related

Checking Whether Int Is Prime Or Not?

Oct 25, 2014

//checks wether an int is prime or not
public static void main(String[] args) {
int n = 17;
boolean prime = true;
if (!(n==2)) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0);

[Code] .....

View Replies View Related

Checking Values In A String

Apr 15, 2015

Suppose i have a string which has certain file names, S = "a.png, b.gif, c.xlsx, d.docx, e.xlsx, f.gif";I need to check if the string has more than one .xlsx file names,

View Replies View Related

Checking If Any Input Has Been Entered

Jan 12, 2015

I just can't find a way to check if user puts any input or not? The line is Employee Name and I need to validate that he puts something there.

import java.util.Scanner;
public class JavaMartInventorySystem {
public static void main(String[] args) {
String empName;
Scanner keyboard = new Scanner(System.in);

[Code] ....

View Replies View Related

Checking Two Strings In Java?

Feb 10, 2014

I want get a specific document from data set if a given string matches in that document.

If given string is 'what is java' and,

doc1='... what is full form of java ...'

doc2='... is java what ...'

doc3='... what is java ...'

then the output should contains doc1, and doc3.

I'm trying like this.

for(String key_word: key.split("W+")) {
for (String _word : line1.split("W+")) {
if(word.toLowerCase().equals(key_word.toLowerCase())) {
key_final=key_final+" "+key_word;
}
}
}

I'm getting wrong output containing doc1, doc2 and doc3.But I want only the exact match. Here each document is containing some paragraphs.

View Replies View Related

Checking Correctness In A Matrix

Nov 21, 2014

I have a programming assignment in which I have to make a program which loads a crossword from a Properties file and then, when the user press a button, the program checks if the letters the user has typed in the interface are the same as in the correct matrix. If all the letters from a word are correct, the program must paint every letter of the word green.

I'm using two matrix, one that is called "mundo" (I 'm from Latin America) , which has the correct letter for each box. The other one is a JtextField matrix which ='ve created using a Gridllayout. I called this one crucigrama (crossword in Spanish)

This is the code I wrote to validate: So you can understand, here is a translation:

numero = number
fila = row
column = columna
bien - a boolean I used to check if the letter I've checked before are the same as the correct ones
.darLetra() - returns a string with the correct letter

[Code] .....

View Replies View Related

Checking If Input Is Digit?

Sep 28, 2014

which method i can use so that the program checks if the input value is a digit or else outputs a message saying that `the value entered is not a number.

View Replies View Related

Checking For Perfect Numbers

Feb 10, 2015

I'm trying to write code for a program that checks numbers upto a number that the user gives. I've looked around, and the nearest matches I can see are people using for loops with predetermined amounts of looping.The only real tools I'm allowed to use are basic arithmetic, and while loops. The program is supposed to have 2 nested while loops.

import java.util.Scanner;
import java.io.*;
public class questionTwo234234 {
public static void main(String[] args){

[code]...

View Replies View Related

Checking For Five Characters Palindrome

Jan 18, 2014

I have the following code to check for five characters palindrome but it always give me incorrect answer that if i have a word like radar the answer it isn't a palindrome. and if i changed any thing in the code i got either stack overflow or index out of bounds exception

The code is as the following

import java.util.Scanner;
public class Palindrome {
int first =0;
static int n;
int last = 4;
int y = 0;
public static void main(String [] args)

[Code] .....

View Replies View Related

Checking Up For Alarms Continuously In App

Aug 5, 2014

I'm up to an app which includes Alarm clock in it. User selects the date using JSpinners and My Alarm clock class checks if the current date is equal to the selected date and pops up an alarm. That's all i've done so far. What i want is some ideas about how can i manage these alarms in my app. What i think is:

* Getting alarm dates from user and save it in database and creating a thread which runs always and check database whether there is an alarm... (will it make my app) ??

In simple words how to continuously check for upcoming alarms and popup any time any alarm comes up during my app is running

Note: My App is not only about alarms, it also contains a complete management system.

View Replies View Related

Checking SSN Without Regular Expressions

Jun 5, 2012

Prompt user to enter a social security number in the format DDD-DD-DDDD, where D is a digit. Displays "Valid SSN" for a correct ssn, and "Invalid SSN" otherwise.I have it working I am just looking for other ways to solve this with an array maybe or something simpler. I have used if statements here:

public static boolean checkSSN(String social) {
boolean valid = false;
// 9 digits and 2 hyphens. First three characters, 5 and 6, and 8, 9,
// 10, 11 are digits

[code]...

View Replies View Related

Checking ArrayList For Certain String

Feb 16, 2014

The objective of my program is to read a file called input_data.txt and to calculate an invoice in which my program will print the results in a formatted text file. I haven't advanced to this step yet because I'm having trouble checking my ArrayList for a certain string. The reason I am searching for "duration" is so that my program will know when the heading is complete.

Here is the sample input file given:

account_number start_time_of_call duration
10011A 21:50 20.2
10011A 13:23 12.3
10033C 23:00 34.0
00101B 10:23 45.1

Eventually, I must separate the fields and calculate payment prices based on the duration and time of the calls.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class spInputOutput {
 
[Code] .....

View Replies View Related







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