ArrayList Only Adding 1 Item When There Are Multiple Items
Oct 25, 2014
I managed to retrieve data, and set data in my own ways in which I like. But my problem is, if the file does not contain anything (fully empty), when I try to use my
set("", "");
method, it only sets the last one that is called.
I managed to retrieve data, and set data in my own ways in which I like. But my problem is, if the file does not contain anything (fully empty), when I try to use my
This is supposed to get a certain number of items, and then calculate how much every item costs. It adds up the sum of those prices and checks if it is over 150$, if it is, it returns true, if it's not, it returns false.
import java.util.*; class Cust { public static boolean DelPay (int a) { Scanner reader=new Scanner (System.in); int b,s=0; for (int i=1;i<=a;i++) { b=reader.nextInt(); s=s+b; } if (s>150) return true; return false; } }
I made my items class and I am storing my item as a String array to list them all and what I want to be able to do is type pickup, have the game read my location and display the items that are avaible to pickup at current location then type the item and store it in my playerInventory. Then, have it check the slot if it is doesn't equal null go to next.
Use a boolean value of 0 = false and 1 = true. then, have it check if the inv array = 1 or 0. if 1 go to next inv. If all are full then, ask player if they would like to replace an item. Use a 2d array for storage for slots and true or false value.
Here is my code
Player.java package com.PenguinGaming; import java.util.Random; import java.util.Scanner; public class Player { public void User(){ Commands commands = new Commands(); Map map = new Map();
I try to create a jsf project within ejb which is add new car with entering attributes listing attributes and search by make.
Add and list methods working well , but have problem of list method. I tryed many combinations (using enhanced loop, iterative loop) but i cant provide working well. Always outputText returns nothing ,when i enter attributes.
Here is ejb code for adding ,getting and listing car items :
@Stateful public class CarsBusiness implements CarsBusinessLocal { List<Car> cars; public CarsBusiness() { cars = new ArrayList<Car>();
My current lesson in Java is about ArrayLists and I'm having a tough time to understand this bit of code: This exercise is concerned with the problem of deleting a block of items from an ArrayList.
public static void deleteBlock( ArrayList<String> strings, int n ) { for ( int i = 0; i < n; i++ ) { if ( strings.size() > 0 ) strings.remove( i );
I'm having trouble completing my homework. The problem:
>Suppose that you want an operation for the ADT list that adds an array of items to the end of the list. The header of the method could be as follows: public void addAll(T[] items)
>Write an implementation of this method for the class LinearLinkedList
At first this seemed like an easy problem to me. This is the solution I came up with:
public void addAll(T[] items) { int length = items.length; for(int i = 0 ; i < length; i++){ this.addLast(items[index]); } }
However, one of the requirements for the assignment is that we're not allowed to use the addLast method. how else to insert items to the Linked List without this method. These are the user-defined LinearLinkedList and ListNode classes we made during class:
**LinearLinkedList** // Linear linked list class import java.util.NoSuchElementException; public class LinearLinkedList {
I am trying to make a text based game. the game has been working perfectly setting up the rooms, first couple of commands, and running it. I am now trying to add items to it but every time it try to run the game it returns :
java.lang.NullPointerException at Room.addItem(Room.java:107) at Game.createRooms(Game.java:133) at Game.<init>(Game.java:28) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[Code] .....
Here are the classes that matter for this particular situation
import java.util.HashMap; public class Item { private HashMap<String, Item> itemList; private String name; private String itemDescription;
[Code] ....
I know that it is the line
itemList.put(item.getItemName(), new Item(item.getItemName(), item.getItemDescription()));
In the game class that is causing the nullpointer exception i just really cant figure out why that keeps happening and how to add the values correctly....
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 need to take the value of what's typed in my JTextField, and once the submit button is clicked, I want to add that string to my JComboBox list items.
Depending which RadioButton they selected, I need to put the team in HOME or AWAY teams, and so on and so on, until they continue clicking submit.
Working on my final which is due today, its' and Inventory Program which I have been working on for the last 5 weeks. My Buttons on my GUI Add/Delete doesn't add items to my inventory. I'm not sure exactly what wrong with my code, I'm not getting any error and the Program compiles just fine, I just can get the Items to add of delete.
my Delete button starts on line 281 my Add Button begins on line 319
// /** Purpose:the purpose of this software is to display inputs as wells as the stocks and price of inputs, *as well as display a 5 % restocking fee per dvd and for the entire Invenotry of each object. *adding a GUI to the code. Adding a Previous button to the existing code allowing the user to cycle through *the Inventory list freely. Also adding a graphics logo to page. adding more buttons and allowing program to *save new items added to the inventory. **/
Basically I am trying to make a POS system and need a way to print a receipt whether there are 1 items sold or 50. So I used a Arraylist. This is the format for the printer that is needed:
printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, " 2 200 Tx Hashi Chop Sticks" + LF);
This is what I tried to do:
ArrayList<String> receiptLine = new ArrayList<>(); public void captureSales(){ receiptLine.add("Test"); System.out.println("Size of the receipt list is: "+receiptLine.size()); System.out.println("Element at index 1: " + receiptLine.get(0));
I am trying to create a GUI interface in swing which consists of four classes: a GUI class, which creates a main JPanel, a label, and a JList, which it takes from the second class, a MovesList class that contains a JList and the stuff needed to interface with it. It also has a main class, which basically just creates an object of the GUI class for the main window, and an Other class from which I would like to be able to add an item to the JList. I created methods in the MovesList class to get each component (like getMoveslist, or getMovesListScrollPane), which I then used to create the JList in the GUI class. I also created an addMove method so that I can add an item to the JList from any class through a MovesList object. However, this addMove method only works when called from the GUI or MovesList classes -- it does nothing when I collect from the Other class.
Here is my code:
//Main class public class TestProject { public static void main(String[] args) { GUI mainWindow = new GUI(); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
[Code] ....
When I run this code, I get a window with a JLabel that says "Moves," and a JList that contains five elements -- test 1-test 5, but not a sixth "test from other class." ( using the add move method ) However, when I click on the window, the addMove method is also called, and successfully adds "test 6" to the list.
I have an array list in my GUI class of an aplication , the app reads in 3 fields from the user and saves these to an array list allowing the user to enter more fields to be added to the arraylist, when displaying back out to the user the same entry displays multiple times, for example if i entered , a,b,1 then c,d,2 and then e,f,3 into the app when i press my display button it would just throw back e,f,3 three times rater than the three different entries...
Constructor and instance variables
private String name; private String number; private int year; private int count; private ArrayList<StudentData> myList; private StudentData myData;
This is supposed to be a method that adds stars after each item in the list.
Java Code:
import java.util.ArrayList; public class ClientProgram { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("the");
[Code] ...
I'm guessing it's because list.size() changes, though but it should only find that value in the beginning, so it shouldn't be a problem?
How do you remove from an ArrayList at a particular index then add back to that same index without the removal causing the ArrayList to compensate for the loss of them item and moving the empty space to the end of the array list?I've tried:
public void dischargePatient(int bedNumber) { if (bedNumber < beds.size()) { beds.remove(bedNumber); } }
But this moves the bed at bedNumber to the end of the ArrayList after removing the patient from the bed. How do I keep it at bedNumber?
and the corresponding snippets of the Java code are:
// Class member variables // ... private String product; private ArrayList<String> productValues; private ArrayList<String> productLabels; private SelectItem[] products; // ... Various properties etc. public String getLocation() { // Displayed on a page
[code]....
Most of this works correctly using only ArrayList SelectItem products without the two ArrayList and the separate SelectItem[], and the values and labels are put directly into products here. The menu works and I can select an item. However, I am unable to find the correct method for finding the index in the submit method,namely:
public void submit(ActionEvent e) { showProduct = true; prodNum = products.indexOf(product); // --- Here is the problem! updateProduct(); }
which has not been changed here. In spite of trying out various ideas, prodNum always returns with -1, which means it cannot find the index of the selected product, where product is a String. Everything else seems to work correctly, and products.get(prodNum).getLabel() works if I manually give prodNum a valid index, but because it's -1 it fails.
I currently have a datatable on the bookingList.xhtml which is supposed to list all bookings made, if first booking petName is spot it displays the booking details perfect but if another booking is made (for example one with petName fluffy) then it displays 2 lines with fluffys details and spots details are not displayed, it carries on that if a third booking is made then the 3rd bookings details are displayed 3 times and neither spot or fluffys booking details are displayed,
bookingList.xhtml code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
I've two classes: Student and ArrayListExamples (which has my main method in it).
The student class acts pretty simply at the moment with a constructor that takes a name, surname and ID number. I've tested it and it seems to be working. My issue is with adding an object to an array list, here is my effort:
public class ArrayListExamples { private static int MAX_SIZE = 50; static Scanner sc = new Scanner(System.in); public static void main(String[] args) { ArrayList<Student> students = new ArrayList();
[code]....
I know the last loop to add the student objects is wrong.I'm calling the student ArrayList add method with a student object whose constructor requires two strings and an integer. Why isn't this allowed?
So I'm trying to write a method which returns the number of vowel characters in arraylist. My idea is to convert the arraylist element by element to array each time iterating through the array counting the vowels of that element. When I started I immediately got an error(surprise, surprise). Excuse me if the problem is too simple, but I am very new to programming.
At line 9 I get the following error "Type mismatch: cannot convert from String to int". I want to get the element at this position, not to convert to int..
ublic class One { public static void main(String[] args) { ArrayList<String> bla = new ArrayList<String>(); bla.add("aaa"); bla.add("brr"); bla.add("unn"); } public static ArrayList<String> averageVowels (ArrayList<String> list){ String[] arrListWord = list.toArray(new String[list.get(0)]); return list; } }
Ive set up a list to when a user creates an element it will be added to the entire list, but when i return to the main menu the Element isnt added.
Here is the code
public void setElement(int atomicNum, Element e){ if (ElementHasData(atomicNum) == false) { if(atomicNum < MAX){ list[atomicNum] = e; atomicNum++;
[Code] ...
Here is what the code intiates
~MAIN MENU~ Please select an option you wish to activate 1) Print All Elements 2) Print One Specific Element 3) Add an Element 4) Edit an Element
3 What is the atomic number of the Element you would like to add Numbers only valid 1-113 1 Enter element name Hydrogen Enter element Symbol H What is the Elements weight 1.01 NEW ELEMENT ADDED!
[Code] ....
~MAIN MENU~
Please select an option you wish to activate 1) Print All Elements 2) Print One Specific Element 3) Add an Element 4) Edit an Element
But then after i try to print the specific element
~MAIN MENU~ Please select an option you wish to activate 1) Print All Elements 2) Print One Specific Element 3) Add an Element 4) Edit an Element 2 What is the atomic number of the Element 1 that element does not exist would u like to create this element Enter yes or no