How To Add Variables And Default Values To Arraylist
Mar 23, 2015
I am facing difficulties in identifying the proper way to add the selected colors of cards into an arraylist. I am having an arraylist which is:
Java Code: private List<String> selectedCars = new ArrayList<String>(); mh_sh_highlight_all('java'); And one more for the carColors:
Java Code: private List<String> carColors = new ArrayList<String>(); mh_sh_highlight_all('java');
The selectedCards array will store the selected cars ['TOYOTA','MAZDA','NISSAN'] as per the selection from the user.For certain types, there is one default color which is 'Black', however for some of them, the user can select different colors.(ex. if selection is 'Toyota')
Java Code:
String carColor="";
String toyotaColor=""; // The value will be retrieved from the form once the user selected the color
if (selectedCars.contains("MAZDA"))
{
carColor="Black";
}
else if (selectedCars.contains("TOYOTA"))
{
carColor=toyotaColor;
I just recently started learning about encapsulation, how to set variables private and only accessible to other classes through setters() and getters(). I've seen a couple of programming examples, perhaps not enough but some from here, and I sort of built the assumption that by default all variables need to be private. to make things more clear, here's a card dealer I made which simply
1- generates a fulll deck of 52 cards 2- lets user decide how many players, with 5 as Max number allowed as each player is dealt 10 cards. 3- deal cards
I approached this by making A deck , card , player and game class
import java.util.ArrayList; public class Deck { //an Object of this Class would generate a full deck ie an ArrayList of 52 Cards private String[] suits={"Spades","Diamond","Clubs","Hearts"}; private int[] number={1,2,3,4,5,6,7,8,9,10,11,12,13}; ArrayList<Cards> deck= new ArrayList<Cards>();
[code]....
I can understand why for example Deck class's suit and number arrays are set to private , as they only need to be accessed by this class only. however, both the Deck class's deck arraylist and the Player class arraylist are not private, as I need to transfer these values from one to the other and it felt to me that setting them to private would make it more difficult to deal with them, but what I did instead is to set the Game class dealCard(), which is the only method that have access to them as private. does this achieve the same effect or do I need to set both of these arrayList to private?a follow up question, this is more related to the actual card dealer program, in this code
private void dealCards(){
for(int x = 0 ; x < playerCount ; x++){ for(int y = 0 ; y < 10; y++){ playerList.get(x).pile.add(deck.deck.get(0)); deck.deck.remove(0); } } }
is there an API in ArrayList class that moves(adds to receiver and remove from giver) element across ArrayLists?
I deleted my default path value in system variables in the process of adding java location in that.. How can I get that default PATH value.. I am using windows 7 enterprise OS 32 bit in Dell laptop...
public static void main(String... args) { String i; // String i = null; that would compile if (i == null) {// this line does not compile as i was not initialized System.out.print("A"); } else { System.out.print("B"); i = "A"; main("A", "B"); } }
Why does the above code not compile although the statement String i should lead to an initialisation of i to the value of null which is the default value for Strings.
I am writing a program that accepts input from the user, I want default values displayed before the input values.
Java Code:
public surfboards() { surfboardType = "Type not assigned"; shaperName = "Shaper not assigned"; constructionType = "Construction Type not assigned"; surboardSize = 0D; } mh_sh_highlight_all('java');
I can get the output to display as shown below
(THIS IS WHERE I WANT DEFAULT CONSTRUCTOR DISPLAYED. Currently not displayed)
Type not assigned Shaper not assigned Construction Type not assigned 0 Feet
(THIS IS WHERE I WANT DEFAULT CONSTRUCTOR DISPLAYED. Currently not displayed)
input:
Java Code: Enter Surboard Type: test Enter Shaper Name: test Enter Surfboard Construction Medium: test Enter Surfboard Size: 6.5 mh_sh_highlight_all('java');
while (key != 3) { if (key == 2) { System.out.println("Input from a file"); // System.out.print("Enter file name: "); // String str = expression.nextLine(); int i = 0; File file = new File(
[code]....
What I need to happen is the file gets read in, the file is in the form:
a * ( b + c ) / 2 + ( 8 * b ) a = 5 b = 10 c = 20 ( x + y ) * z x = 13 y = 21 z = 3
And so on. My code is supposed to start at the beginning and have output like:
Infix: a * b ( b + c ) / 2 + ( 8 * b ) variable values: a = 5 b = 10 c = 20
**It then needs to store the variable names along with their values in a hash table**
postfix: a b c + * 2 / 8 b * + value: 155
And it continues reading the file in the same fashion.
My issue is I am not properly extracting the variables and their values from the file. I have the variables in an array list actually, so I have those, but don't know how to efficiently navigate to the values, then store the character and value in a hash table, while protecting against possible blank lines within the file. Thus just calling nextLine()s and next()s wouldn't work to get to values because they would lead to an error.
I am able to get output from my constructor when I place a loop inside of it. However when I try to access the private variable that I thought was set by the constructor I get nothing. When I check its size it is zero as well.
Java Code:
public class WinningHand extends PokerCalculator { private int p1Size; private int p2Size; private String[] p1Hand = new String[p1Size]; private String[] p2Hand = new String[p2Size];
I'm a total newbie to Java, and until now all I've done was draw some shapes and flags. I'm struggling to understand the code I've been given. I need to access values stored in an ArrayList within another class. Here are the two classes Seat and Mandate:
package wtf2; import java.util.*; public class Seat { public int index; public String place; public int electorate;
[Code] ....
The main class contains code that feeds data from 2 text files into Seat and Mandate. From there I managed to access the date in Seat (at the end):
package wtf2; import java.io.*; import java.util.*; public class CW2 { public static void main(String[] args)throws Exception {
[Code] ....
Now,instead of getting just the mp for Edinburgh South I need to get the vote values, compare them to each other, take the second biggest and display the associate party value. How to access data from that Array to get started at least.
I'm having an issues with adding integer values to a string list. The question is asking me "the method should iterate over runners, and for each runner generate a random number between 90 and 180 (inclusive) which should be used to set the time (in minutes) for that runner."
I have been able to get the random number and iterating over the runner arraylist but I haven't been able to figure out how to add the values generated into the runners list. I am also using BlueJ.
Here's the whole code I have at the moment:
import java.util.*; import java.io.*; import ou.*; import java.util.Random; /** * Write a description of class MarathonAdmin here. */ public class MarathonAdmin { // instance variables - replace the example below with your own
I am creating a shopping list, with each element and object called product from the Item attribute class. Each product object has a description, price, and priority. From user input, I am able to create each product object, and then .add to my arraylist. I know that it successfully adds because I have it printed in each iteration of the do-while loop. Once the user indicates they want to quit, the loop ends. I then want to print each description, price, and quantity but this for loop will only print the last element over and over. Here is what I have:
ArrayList <ItemAttribute> list = new ArrayList<ItemAttribute>(); Scanner keyboard = new Scanner(System.in); ItemAttribute product = new ItemAttribute(); public void findDescrPrice() { /** * Get the item name (description) for each list item
I am working on the Kevin Bacon - 6 degrees of bacon problem. I want to take my Array List and use the index and Values that I have saved in it as the Key and Value of a Hashmap. Below is my code.
HashMap<Integer, ArrayList<String>> actors = new HashMap<Integer, ArrayList<String>>(); ArrayList array = new ArrayList(); try (BufferedReader br = new BufferedReader( new FileReader("actors.txt"));)
[code]...
saving each one in it's own spot. I want to use the index of each one as the key to find it in the Hashmap....
I'm trying to build a monopoly like game, and atm I'm trying to find way how to build the Community and Chance chest cards. so far, my logic is
1-create an ArrayList of cards with a given order
2-for a given number of times(for loop) generate 2 random numbers ,which will be the parameters for Collection.swap().
3-swap.
here's the code for the shuffler Button
shuffler.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for(int i=0;i<shuffeledMessages.length;i++){ int randoma=(int)(Math.random()*4); int randomb=(int)(Math.random()*4); Collections.swap(myMessages,randoma,randomb); } } });
For now things seem to work pretty ok, but I'm wondering if this is a good and efficient way to shuffle a card chest especially in case of large number of cards. plus, I'm not sure what would be a good loop count for effective shuffling, in my case I used i<arraylist.size
I need assigning the selected hashmap values into a list and to display the values in a jsf page. The user will select certificates and will be stored in the below list:
private List<String> selectedCertificates = new ArrayList<String>();
The selectedCertificates will have the values ("AA","BB"). Now I will be passing the list into a hashmap in order to get the names and to display them on the jsf page.
Now my issue is that is how to display all the values of the CertificatesNames.get(key1) in the jsf page as I tried to do the below and it printed all the values when I used the #{mybean.beans} using the :
List beans = new ArrayList(CertificatesNames.values());
I am trying to write a program that will take any comma separated data set and return the values in an arraylist without any commas. Like if I input "hello, world, program, java" it would output an arraylist [hello,world,program,java].
public void run(){ String line = readLine("Enter a CSV-formatted line of data: "); int lowerBound = 0; String entry = new String(""); ArrayList<String> string = new ArrayList<String>();
I am trying to create an array list without using the built in arrayList class that java has. I need to start the array being empty with a length of 5. I then add objects to the array. I want to check if the array is full and if not add a value to the next empty space. Once the array is full I then need to create a second array that starts empty and with the length of the first array + 1.
The contents of the first array are then copied into the new array without using the built in copy classes java has and then the new array ends up containing 5 elements and one null space the first time round. The first array is then overwritten with the new array containing the 5 elements and the null space. The process then starts again adding values until the array is full which will be 1 more value then recreating the second array empty and the length of the first array + 1. This is my attempt at creating this but I have run into multiple problems where I get only the last value I added and the rest of the values are null:
public class MyArrayList { public Object arrayList[]; public MyArrayList(Object[] arrayList) { this.arrayList = arrayList;
The LocalStudent class inherits the Student class. The IDE states an error of "no default constructor in Student class".I understand that if a LocalStudent object is created, the default constructor of its superclass (aka Student class) will be called implicitly.there is no LocalStudent object being created, so why is the default constructor of Student being called ?
The default constructor of LocalStudent is also overloaded by the created no-arg constructor containining subjects = null; . So there is no call to the superclass default constructor from the default constructor of LocalStudent.
public class Student { private char year1; public Student(String name, char year){ year1 = year; } public char getYear(){ return year1;
Will I'm tying in my code to set a default number for the JTextField that when the user decide not to put any numbers. Like let say that I want the textfield set to 0 , so then the user do not file it it won't make any problem to the program because its already has a default number.
Note: This is a small part of my code. when I leave it empty it take the 0 as a value, However, when I write in text field it also take the value of a 0 and the finalTotal is also = to 0.what I'm doing wrong.