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
ADVERTISEMENT
Jun 2, 2014
So I have an applet and in the first ScrollPane you have a number of options of businesses
The Centre
IMAX
etc ..
and when you click one , the other scrollpane shows office
so when i choose "The Centre" for example , in the right scrollpane it shows : "Head Str." "Downtown" etc
i can fill in the first scrollpane with:
void myInit() {
DefaultListModel model = (DefaultListModel)businessList.getModel();
model.removeAllElements();
try {
ArrayList<Business> bussinesses = contr.getBusiness();
[code]....
but this gives me a NullPointerException on
ArrayList<Office> offices= business.getOffices(name);
which i don't understand, since business isn't null .. it's the selected value.
View Replies
View Related
May 31, 2014
I am trying to do the fallowing ,
Jtable cell with search (% like) depends on the value entered
if i enter "mi" it should show all data begin with "mi" from my data set
View Replies
View Related
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
View Related
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
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
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
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
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
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
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
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
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
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
Jun 19, 2014
So from what iv learnt in Java and programming in general is that using a case statement is far more efficient that using multiple IF statements. I have an multiple IF statements contained within a method of my program, and would like to instead use a case statement.
public String checkPasswordStrength(String passw) {
int strengthCount=0;
String strengthWord = "";
String[] partialRegexChecks = { ".*[a-z]+.*", // lower
".*[A-Z]+.*", // upper
".*[d]+.*", // digits
".*[@#$%!]+.*" // symbols
[code].....
View Replies
View Related
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
Nov 18, 2014
So I want to make a simple Java that ask the user to pick a powers and it has two options.If the user picks magic then execute the first if statement then ask the user again which type of magic the user wants.I can't make it work it keeps printing the else statement. Why is that?
import java.util.Scanner;
public class Variable {
static Scanner zcan = new Scanner(System.in);
public static void main(String[] args)
[code]....
View Replies
View Related
Mar 28, 2014
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)
So far I have:
public boolean equals(Object O){
if(O instanceof Geocache){
Geocache j=(Geocache) O;
if (this.equals(j)) //I know this is wrong... but I can't figure it out
return true;
}
else return false;
}
I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
View Replies
View Related
Oct 7, 2014
I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. I have an Array of objects that contains strings. How can I get the object's strings to print in a list so that the user can select that object to manipulate its attributes? For example, the user can select "Guitar 1" from a list and manipulate its attributes like tuning it, playing it, etc. I have a class called Instruments and created 10 guitar objects.Here is the code:
Instrument [] guitar = new Instrument[10];
for (int i = 0; i < 10; i++) {
guitar[0] = new Instrument("Guitar 1");
guitar[1] = new Instrument("Guitar 2");
guitar[2] = new Instrument("Guitar 3");
guitar[3] = new Instrument("Guitar 4");
guitar[4] = new Instrument("Guitar 5");
guitar[5] = new Instrument("Guitar 6");
[code]...
View Replies
View Related
Mar 27, 2015
can a keyevent in java make the space ship and the laser too ?
View Replies
View Related
Apr 15, 2014
For example I create an object like this:
BankAccount b = new SavingsAccount();
Now lets say that I want to access a method 'addInterest()' that is in the 'SavingsAccount' class I would have to do: '((SavingsAccount)s).addInterest();'
The question I have is why do I have to cast 'b' to SavingsAccount? Isn't the actual object reference of 'b' already an instance of 'SavingsAccount' class? How does the 'BankAccount' affect the object itself? I'm really confused as to what class is truly getting instantiated and how BankAccount and SavingsAccount are both functioning to make the object 'b'.
View Replies
View Related
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
Nov 19, 2014
I am trying to get this to where I can type in a name and it will search through each object and print back the corresponding object info.
Java Code:
import java.util.Scanner;
public class MyPeople {
public static void main(String[] args) {
Person[] p = new Person[] {
new Person("Chris", 26, "Male", "NJ", "Single"),
new Person("JoAnna", 23, "Female", "NJ", "Single"),
new Person("Dana", 24, "Female", "NJ", "Single"),
new Person("Dan", 25, "Male", "NJ", "Single"),
new Person("Mike", 31, "Male", "NJ", "Married") };
[code]....
View Replies
View Related
Apr 9, 2014
Task:The main method of the class Things below creates an object called printer deriving from the class PrintingClass and uses that object to print text. Your task is to write the PrintingClass class.
Program to complete:
import java.util.Scanner;
public class Things {
public static void main(String args[]) {
String characterString;
Scanner reader = new Scanner(System.in);
PrintingClass printer = new PrintingClass();
System.out.print("Type in the character string for printing: ");
characterString = reader.nextLine();
printer.Print(characterString);
}
}
// Write the missing class here
Note: In this exercise the solution is part of a conversion unit where many classes have been declared. Because of this the classes are not declared as public using the public attribute.
Example output
Type in the character string for printing: John Doe
John Doe
My Class:
class PrintingClass {
public void print(){
System.out.println(characterString);
}
}
View Replies
View Related
Mar 23, 2015
If I set a Jlist to contain an array of objects, how can I display properties of each object instead of the object array list itself. Ex:
Instead of:
Person1
Person2
Person3
display values such as each person name within the Jlist:
Mike
Paul
Andrew
View Replies
View Related
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