JSF :: Finding Index Of Entry In ArrayList - Select Item
Oct 2, 2014
I have a JSF application with this code in the xhtml page:
<h:selectOneListbox id="selectProduct" size="10" style="width:10em; font-family:monospace" value="#{mybean.product}">
<f:selectItems value="#{mybean.products}" />
</h:selectOneListbox>
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.
View Replies
ADVERTISEMENT
Apr 28, 2014
I'm making an application that will allow users to view several displays simultaneously. I'm trying to make it so that the user should be able to select one of the items from a list then hit a radio button add them to the display. And there will be a second button to remove them from the display. Oh and there will always be alteast one display.
View Replies
View Related
Jun 24, 2014
I am trying to have a user select from a printed out array list, instead of having the user type in the "bill type" each time there is a bill to avoid user error as much as possible. For example I would like to have it print out like this:
"Select bill type from list:
1. Rent
2. Car
3. etc..."
and I would like the user to choose a number and not type in the "bill type". I don't want to use "Switch case" because it would need to be an expanding and I don't think "switch case" can do that.
Here is the code:
package homebudget;
class Spending
{
//Do i need a totalAmount variable?
String type;
double amount;
int year, month, day;
public Spending()
[Code] ....
case 2:
//Give option to enter a new expense or pick from list.
//How to do this? If Statement that doesn't list duplicates, or a while search?
resp = JOptionPane.showInputDialog("Enter the type of expense:");
type = resp;
resp = JOptionPane.showInputDialog("Enter the amount of the expense:");
amount = Double.parseDouble(resp);
[Code] .....
View Replies
View Related
Oct 31, 2014
I have a case where I have a TreeView and I use it to select items from the tree. After I select each item I then clear the TreeView selection. Then, when I select outside of the window, the setFocused is called which in turn selects item 0 from the tree. I see in the TreeView.java the code,
private InvalidationListener focusedListener = observable -> {
// RT-25679 - we select the first item in the control if there is no
// current selection or focus on any other cell
MultipleSelectionModel<TreeItem<T>> sm = getSelectionModel();
FocusModel<TreeItem<T>> fm = getFocusModel();
[Code] .....
Is there anyway to suppress this behavior?
View Replies
View Related
Jun 11, 2014
I have a large JPanel which uses a GridBagLayout in which I put other JPanels inside the cells. The main JPanel is then added to a JScrollPane. What I'm looking to provide the user a way to automatically scroll to a particular item in the scrollable area (i.e. each of the child JPanels would represent - let's say a person for argument sake - and an user would like to click a button or bring that JPanel into focus / view.
The JPanels are added within a loop to the grid and references of these JPanels at the moment aren't store in an array or anything, so it's a case of being added and that's it. Also there's the case of where was the object exactly added and also how to have the scrollbar move to the proper focus location (both horizontally and vertically).
View Replies
View Related
Sep 4, 2012
I have to alter my Sentence class to find the index of the substring "sip" in Mississippi but I'm really not sure where to begin. This is what I have...
Sentence.java
public class Sentence {
private boolean outcome;
private String sentence;
public Sentence(String aSentence) {
sentence = aSentence;
[Code] ....
I know that I need to change public boolean find(String t) to public int indexOf(String t) but I'm not sure what to start doing to get the index of "sip".
View Replies
View Related
Jan 29, 2014
int[] a = new int[7];
a[0] = 0;
a[1] = 10;
a[2] = 256;
a[3] = 57;
a[4] = 33;
a[5] = -154;
a[6] = 168;
[code]....
What program needs to find is the most biggest number. It does the job, but another task of the program is to find the index of that number . The second loop should do just that, but for some reason, as the loop goes further, it passes through the if statement even though answer "a[i]" is not equal to "answer". The idea is that if a[i] and answer are equal, the "i" should represent the index number.
View Replies
View Related
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
Aug 14, 2014
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;
[Code] .....
View Replies
View Related
Nov 27, 2014
Creating an Arraylist and adding item to that array, refer below code
ArrayList<String> sjarr = new ArrayList<String>();
Statement1:
String arritem1 = new String("First array item");
sjarr.add(arritem1);
Statement2:
String num = "text will decide";
sjarr.add(num);
Both adds the String item to array list but puzzling what makes the difference....
View Replies
View Related
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.
Example:
set("section1", "section1Item");
set("section2", "section2Item");
set("section3", "section3Item");
Only section3 would get set, and not the others.
Otherwise, if the file contained a section already, then each section (section1, section2, section3) would get set.
Here's how I set the data to the file:
public static void set(String section, String data) {
files.openFileWriter();
files.file.delete();
reCreateFile();
String beforeItem = section + ":" + files.dataList.get(section);
if(files.hasReadData) {
[Code] ....
And here is how a retrieve the data and set them to my arraylist/hashmap:
public void getData() {
String line = null;
openFileReader();
StringBuffer sb = new StringBuffer();
[Code] ....
View Replies
View Related
Dec 2, 2014
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?
View Replies
View Related
Nov 15, 2014
So I am working on an assignment and ran into an annoying bug. Basically i have a menu that accesses an ArrayList of Videos which may or may not be read from a file, one of the options of the menu is to edit an existing Video. For this I ask the user for the number of the video and it is checked against the list of video numbers if it returns a match, the method gets the index of the Video object and stores it in a temporary variable the user is allowed to edit the details and the object is put back into the ArrayList using the variable and the ArrayList's set() method
My problem is that once i finish editing the details of a video it gives me a indexOutOfBounds exception
On further investigation using a method that goes through the index of every object in the ArrayList using indexOf() i found out that every single object has been given the index of -1 and not 0,1,2,3 etc.. This is my first error and have not expierenced any other before.
The objects do exist because I have read them from a file. I can also add new Objects and view them successfully but they still have the same index . I have checked my syntax and everything and no errors, this happens at run time only.
I have even created some other ArrayLists seperately and debugged them and their index order is fine. I am too far into this project to start over. I've also tried cleaning the project(my IDE is Eclipse).
View Replies
View Related
Nov 12, 2014
How could I check if an index is exist in an array list? I mean, I should enter an integer and it should return me a boolean result that saying whether if that entered value is an index or not.
View Replies
View Related
Feb 1, 2015
I am just studying over ArrayLists and i have encountered a problem, i'm trying to get the index of an element but it keeps returning -1. Here's the code
Java Code:
import java.util.ArrayList;
public class PhoneEntry {
String name;
String phone;
public PhoneEntry(String name, String phone) {
[Code] ...
I know the list is not empty because it returns a size.
View Replies
View Related
Apr 27, 2015
I would like to know how to set a int and a string to the same index in Array list. For example index 0 would have 5 and "Apple".
View Replies
View Related
Sep 3, 2014
I am not sure how to go about doing this, An ItemEvent.Selected constant is the hint given to me but i dont know how to start it.
My Array is : private ArrayList billItems = new ArrayList();
My method is : private void beverageJComboBoxItemStateChanged( ItemEvent event )
View Replies
View Related
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.
Example:
set("section1", "section1Item");
set("section2", "section2Item");
set("section3", "section3Item");
Only section3 would get set, and not the others.
Otherwise, if the file contained a section already, then each section (section1, section2, section3) would get set.
Here's how I set the data to the file:
public static void set(String section, String data) {
files.openFileWriter();
files.file.delete();
reCreateFile();
String beforeItem = section + ":" + files.dataList.get(section);
[code]....
And here is how a retrieve the data and set them to my arraylist/hashmap:
public void getData() {
String line = null;
openFileReader();
StringBuffer sb = new StringBuffer();
[code]....
View Replies
View Related
Apr 6, 2014
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....
View Replies
View Related
Apr 15, 2014
I have been trying to get this method to work for a few hours now, for some reason I get an IndexOutOfBounds exception every time. Basically I am trying to read a txt file and add the words to an ArrayList as strings separated by a space .
private ArrayList<String> readLinesFromFile(BufferedReader inputFile) throws IOException
{
String value = null;
ArrayList<String> result = new ArrayList<String>();
while((value = inputFile.readLine()) != null){
String[] values = value.split(" ");
for (int i = 0; i < values.length; i++){
result.add(values[i]);
}
}
return result;
}
View Replies
View Related
Feb 5, 2015
Is it possible to check to see if a what a user has entered is a string or a other variable type, if I'm asking this the right way?
View Replies
View Related
Apr 8, 2014
because of the need to implement a certain software watermarking technique I have to manipulate the exception table.
(Link to the paper that describes the technique: [URL] .... (Section "Extension to Java Bytecode")
So far I could manually add the exception table entry with Javassist:
MethodInfo minfo = (MethodInfo) aclasscf.getMethods().get(0);
CodeAttribute ca = minfo.getCodeAttribute();
ExceptionTable et = ca.getExceptionTable();
[Code]....
View Replies
View Related
Mar 4, 2015
I'd like to know is there a way to restrict a user from inputting certain characters into a TextField? Is there something like isNumeric() available?
In Visual Basic, I was able to prevent the user entering certain characters of my choice into each textbox, however I've looked and I haven't been able to find a solution. I've tried using the following else if as part of an if statement:
else if (txtEmployeeName.getText().contains("[0-9]+"){ }
but it doesn't seem to work.
View Replies
View Related
Jan 9, 2009
I have an env-entry defined in my web.xml and when I attempt to print it from a simple servlet all I get is null. And of course if I attempt to invoke any methods on the object I get a NullPointerException.I read somewhere that this can happen if you're using version 2.4 instead of 2.5, so I changed to 2.5 and I still have the same issue. Here's the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<display-name>ResourceTest</display-name>
<servlet>
[code]....
View Replies
View Related
Dec 24, 2013
I'm using Ubuntu. After unpacking the tar file for JSE 1.7 version 45, in the the bin directory for adding an entry to PATH variable, I typed
javac
and I get the message
The program 'javac' can be found in the following packages:
* default-jdk
* ecj
* gcj-4.6-jdk
* gcj-4.7-jdk
* openjdk-7-jdk
* openjdk-6-jdk
Try: sudo apt-get install <selected package>
Do I have to install these ?
View Replies
View Related
Apr 7, 2014
I use Javassist to manually add a exception table entry:
Java Code:
MethodInfo minfo = (MethodInfo) aclasscf.getMethods().get(0);
CodeAttribute ca = minfo.getCodeAttribute();
ExceptionTable et = ca.getExceptionTable();
et.add(26, 30, 40, 0);
System.out.println("exception table size: " + et.size()); mh_sh_highlight_all('java');
If I get the bytecode with javap it seems to work:
Java Code:
Exception Table
from to target type
26 30 40 any mh_sh_highlight_all('java');
But the problem is that I can't run the file anymore. Simply running it with the java command results in "java.lang.VerifyError: Expecting a stackmap frame at branch target 40". After some research this seems to be a problem with java 7 and a stricter verifier. I read several times that java -XX:-UseSplitVerifier should be used instead.
This really fixed the stackmap error, but then another error appeared: "java.lang.VerifyError: (class: AClass, method: signature: ()V) Inconsistent stack height 2 != 1". Are there any further steps required to insert a new exception in the exception table?
Additional info: Later I want to remove gotos (in this case the goto at line 27) and add a function call instead. When this function finishes it will throw a exception and propagate it back to the caller (which will be at line 27). Then the program should go on as if nothing happend, that is also the reason why the target of the exception is the same as the target of the goto (40)
Here is the bytecode:
Java Code:
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: iconst_0
5: istore_1
6: goto 19
9: getstatic #10 // Field java/lang/System.out:Ljava/io/PrintStream;
12: iload_1
13: invokevirtual #16 // Method java/io/PrintStream.println:(I)V
[Code] .....
Exception table:
from to target type
26 30 40 any mh_sh_highlight_all('java');
View Replies
View Related