JSP :: ForEach ArrayList Of Objects
Feb 11, 2014
I am trying to access an array of "Movie" objects in my jsp. The array is loaded via
org.springframework.web.servlet.ModelAndView.addObject().
Here is my jsp code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
[Code] .....
The System.out.println("jsp page: .... &> results in the output: "jsp page: movielist - [Title: Die Hard; Budget: 20000000, Title: two days in paris; Budget: 1000000]" so I am confident the objects are being loaded into the ModelAndView correctly. However the output of the block is "${movie.name}" instead of the list of movies. My movie object has a getName() method to return a string (and a setName() method). I am not sure why the System.out.println statement can find the movielist attribute, but ${movie.name} is being treated a plain text. There are no execptions thrown or other indications of errors.
View Replies
ADVERTISEMENT
Mar 15, 2015
I have a ArrayList of objects of class called HockeyPlayer (ArrayList<HockeyPlayer>). A HockeyPlayer has a String name and int number of goals.
This is my current work for comparing each object in the list to every other and printing them to screen:
Iterator<HockeyPlayer> it = hockeyPlayersList.iterator();
while (it.hasNext()) {
HockeyPlayer singleHockeyPlayer = it.next(); //the first one encountered
// HockeyPlayer nextHockeyPlayer = it.next(); //the next one encountered
[Code]...
This of course produces some duplication in the print-out: These are the equal hockey players: Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
These are the equal hockey players: Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
How do I remove the duplication in the print-out?
View Replies
View Related
Jun 20, 2014
I want to clone some Arraylist, but the compiler apparently are just referencing the values to it's original ArrayList. I don't know what should it be:
package projetoteste;
import java.util.ArrayList;
import java.util.List;
public class TestFood {
public static void main(String[] args) {
List food=new ArrayList();
[code]....
Notice that ArrayList dailyMeal should be untouchable, but it return the changes that I made in local for-loop iteration although I didn't added nothing to it, just to it's clone.
Output:
[[[Suco, 1 , 2], [Suco, 1 , 2]], [[Suco, 1 , 2], [Suco, 1 , 2]]]
[[[►, ►, Suco, 1 , 2], [►, ►, Suco, 1 , 2]], [[►, ►, Suco, 1 , 2], [►, ►, Suco, 1 , 2]]]
View Replies
View Related
Mar 23, 2014
Okay, I will be as succinct as possible. I am writing a rudimentary book store program for homework. The program consists of two class files and a main. The main issue I am having is on getting the search portion of my program to work. I will post as little as I can and still make sense. This is the block of code belongs to my public Book getBook() method, which is of the BookStore class.
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
Oct 17, 2014
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?
View Replies
View Related
Jan 23, 2014
I'm having trouble getting new objects to add to an ArrayList. Instead, every time a new object is created and added, it overwrites the first object. So if I read in four lines from a text file and make them into four objects, each overwrites the previous and only the last object is printed. I can print out the contents correctly if I move the println statement within the if-loop; however, I need all the objects to saved to the list for searching (later implementation). i believe it has something to do with the instances of the object, but I'm a little confused about how to fix it. Here's the portion of the code I'm looking at:
Java Code:
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
Scanner scanner = new Scanner(new FileReader(file));
String currentLine;
System.out.printf("%-20s %-20s %-20s
[Code] .....
View Replies
View Related
Feb 25, 2014
How would you display an ArrayList of Objects in a JSP page from a Servlet. Any general example would be fine as long as the example is thorough.
Each created object has more than maybe 6 or 7 properties that all need to print to the JSP.
Would you also include ways of printing it like should it be printed by using a table structure in the JSP and if so how? Things of that nature so that way it is each object has its own line and looks good. I can format it myself, I just need to know how to get it in a table for each object to print on its own row and so on...
View Replies
View Related
Jan 9, 2014
I have a problem with my program. My aim is to display in a JPanel, 2 rectangles, and resize them by clicking. I want to give the user the ability to change the height of the rectangles (independently) by moving the top and/or bottom edges. Currently, it displays the 2 rectangles, but impossible to resize and I can not find the solution.
Java Code:
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.*;
public class Resizing extends JPanel {
private static final int PREF_W = 600;
[Code]...
View Replies
View Related
Jan 10, 2014
I have a problem with my program. My aim is to display in a JPanel, 2 rectangles from an arraylist, and resize them by clicking. I want to give the user the ability to change the height of the rectangles (independently) by moving the top and/or bottom edges. There is errors in my code but where ? Netbeans underlines 2 lines (in bold).
My code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Path2D;
[Code]....
View Replies
View Related
Jun 14, 2014
I've come across something that i'm not overall sure about regarding the static keyword in Java.I'm making a vertical scrolling game where the player simply shoots enemies and they shoot back as they fall, dropping items if they die such as power ups and coins. I have an enemy called Bat and this is the bullet creation code in the update method:
if(oldPlayerY + 220 > posY && getBulletDelay > 0.90f){
batBullets.add(new Bullet(posX + 10, posY - 10));
getBulletDelay = 0;
}
The method is creating a new bullet object and it then adds that to the arraylist called batBullets, which is simple enough. I then need to access this arraylist in the main game update class so I can render those bullets on the screen, even if the bat dies. I was always taught that you use the static keyword when you need to access something from the class that doesn't require an object. Because of this, I have the following code.
for(Bullet bullet : Bat.batBullets){
bullet.setY(bullet.getY - 5); // Set the bullet to fall
renderMap.getSpriteBatch().draw(bullet.batBullet(), bullet.getX(), bullet.getY()); // render the bullets
}
This seems perfectly fine to me because I need to access the batBullet arraylist and it doesn't make sense to create a new bat object as I already have random spawning in place for them.
View Replies
View Related
Oct 14, 2014
I have a requirement to find the greatest/maximum of the given list of Calendar objects in Java.
i.e., 2013/01/26
2014/03/03
2012/02/27
2014/01/15
So the above list of calendar objects are in Arraylist. I need to get the max/greatest among these. Eventually, the answer should be 2014/03/03
View Replies
View Related
Mar 18, 2014
I have to create a method with the following header :
public static <E extends Comparable<E> > void sort ( ArrayList<E> list , int left, int right)
i also had to create a swap cells method and position of max integer method. and also had to read the preserved data file in with a scanner. I implemented the comparable interface I am having difficulty sorting my list by the area. It has to be in descending order.
Geometric Object class: since it has comparator also am interested if i need to change this?
CODE:
Driver:
public static void main(String[] args) throws IOException, ClassNotFoundException {
Circle c1 = new Circle (4, "red", false);
Circle c2 = new Circle (2, "blue", true);
Circle c3 = new Circle (10, "blue", true);
Rectangle r1 = new Rectangle (10, 6, "yellow", true);
Rectangle r2 = new Rectangle ( 5, 11, "green", true);
ArrayList <GeometricObject> list = new ArrayList();
[Code] ....
View Replies
View Related
Nov 4, 2014
I am trying to create a method for my "ticketmachine" object that counts ticket objects in an arraylist of "ticket" objects with a specified field "route". The method works if i change the field type of the ticket class to public, however according to the homework task "route" of the ticket class needs to remain a private field.
Here is my code for the method.
public int countTickets(String route) //HAD TO CHANGE Ticket class FIELD "ROUTE" TO PUBLIC!!!!
{
int index = 0; //returns the number of Ticket objects in the machine with the specified "route".
int ticketCounter = 0;
while (index < tickets.size())
[Code] ....
Is my general approach to the problem wrong? also is there a way to access a private field?
View Replies
View Related
Jan 13, 2014
I have a code to resize a single rectangle. I would like to display and resize multiple rectangles independently and according to my research on the net, the best way is to use an arraylist. So I modified the code in this sense, except ...when I run the code, the rectangles appear good but impossible to resize. I think the problem comes from mousePressed, Released, Dragged and Moved methods. When I use the mouse to resize the rectangle, nothing happens. The code does not interact with the arraylist, and therefore with rectangles it "contains". Resizing is my main class, the MouseAdapter is in the Resizer class (below).
Resizing component; boolean dragging = false; // Give user some leeway for selections.
final int PROX_DIST = 3; Path2D.Double selectedPath; public Resizer(Resizing rz) {
component = rz; component.addMouseListener(this);
component.addMouseMotionListener(this);
[Code] .....
View Replies
View Related
May 11, 2012
I'm doubted regarding the implementation of Collections.binarySearch() method on an ArrayList of objects of a custom class Movie.
Here is the movie class :
public class Movie implements Comparable<Movie> {
String movieName;
String rating;
String director;
String theme;
[Code] .....
The sort/binarySearch in searchByMovieName function is done with natural sorting (and Comparable Interface in Movie class). I mean no comparators involved here. And the comparator that I used for sorting/binarySearching on Movies Director attribute in searchByMovieDirector function is :
public class MovieDirectorComparator implements Comparator<Movie> {
public int compare(Movie movie1, Movie movie2) {
return movie1.getDirector().compareToIgnoreCase(movie2.getDirector());
}
}
But I was not able to implement binarySearch ?? How to implement the binarySearch here. I have google to see only binarySearch working on Arrays or probably ArrayList of String only, but not on ArrayList of custom objects.
View Replies
View Related
Oct 7, 2014
I've implemented Stack on the base of the LinkedList. It works as i expect it to work, but then i tried to made it compatible with foreach loop. I've implemented Iterable and Iterator. Code compiles and works fine, but does not return any output. It looks like while working with foreach, next() is not called at all. If i`m getting iterator from instance and try to do iterator.next(), i get output as expected.
public class genericStack<T> implements Iterator<T>, Iterable<T> {
private LinkedList<T> LL ;
protected genericStack() {
this.LL = new LinkedList<T>();
}
public static void main(String[] args) {
[Code] ....
View Replies
View Related
Mar 25, 2015
I am trying to use an iterator instead of a foreach loop to go through a list of inventories to see if a car is rearDrive and count how many are rearDrive but somehow I seem to be missing something that the counter isn't working as expected.
public int howManyAreRearWheelDrive(){
int i = 0;
int counter = 0;
int inventSize = inventory.size()-1;
while(i < inventSize){
boolean wheelDrive = inventory.get(i).getIsRearWheelDrive();
if( wheelDrive == true){
counter++;
}
}
return counter; // returning here doesn't give me anything
}
View Replies
View Related
Nov 4, 2014
I have a ace:dataTable which contains a variable number of columns. For this I use the c:forEach tag. All generated in the c:forEach are not displayed. In the sample, only columnLibelle and columnSum are displayed.
<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
[Code] ....
View Replies
View Related
Apr 26, 2015
I am trying display the result set from the database.I am getting the following exception :
Caused by: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:286)
My Jsp code :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
[Code] ....
View Replies
View Related
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
Feb 28, 2014
I'm trying to create my own arraylist using Collection. My program doesn't do anything. Obviously, I haven't a clue.
import java.util.Collection;
import java.util.Iterator;
public class MyArrayList<T> implements java.util.Collection<T> {
private int size = 4;
private T[] mArray;
public MyArrayList(String[] args) {
[Code] ....
View Replies
View Related
Jan 23, 2015
how String objects are different from other objects
part 1:
// creating two objects
Dog mydog1 = new Dog();
Dog mydog2 = new Dog();
// comparing the reference variables
if( mydog1 == mydog2){
System.out.println(" The reference variables refer the same object ");
}
else {
System.out.println(" They refer to different objects ");
}
The above code works as I understand objects , it prints "They refer to different objects " to the screen.
Part - 2
// creating two objects ( I beleive, pls correct me if i am wrong )
String a = "haai";
String b = "haai";
if( a == b){
System.out.println(" Reference variables refer to same object");
When i run the above code it prints that a and b refer same object , I don't understand how they refer to same object when i didn't assign " String b = a; ". My question is did java just create one object and stored the same reference values to a and b .
View Replies
View Related
Jul 8, 2014
I have stumbled onto a problem with ArrayLists (not sure if nested ArrayList objects would be a more accurate description) ....
As simply put as I can describe it, I have an Inventory class which creates an ArrayList of a Product superclass, which has two subclasses, PerishableProduct and ItemisedProduct.
By default, the program is meant to have a starting inventory, which is why I have added them in the constructor
public class Inventory {
private List<Product> products;
public Inventory() {
addProduct(new Product("Frying pan", 15, 20));
addProduct(new PerishableProduct("Apple", 5.8, 30, 7));
addProduct(new ItemisedProduct("Cereal", 5.8, 0));
// this is where I am having problems. Wanting to add
// objects to the ItemisedProduct's ArrayList for cereal.
}
Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers.
public class ItemisedProduct extends Product {
private ArrayList<String> serialNumbers = new ArrayList();
public ItemisedProduct(String name, double price, int qty) {
super(name, price, qty)
[Code] .....
My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.
View Replies
View Related
Feb 26, 2015
I think it's a problem with my if then else statements but I'm not sure?
package Hw1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class ScrapWork {
public static void main(String[] args){
String[]Dalton={"Joe","William","Jack","Averell"};
[Code] .....
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of type
at Hw1.ScrapWork.main(ScrapWork.java:16)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
View Replies
View Related
Nov 14, 2014
I have an ArrayList with String, String, Double.
From another class I would like to print minimum and maximum including the first String name.
I have tried with import java.util.Collections; but that didn't work.
Java Code:
ArrayList<List>list = new ArrayList<List>();
list.add(new List("Abc","Cba",1));
list.add(new List("Bca","Bca",5));
list.add(new List("Cba","AcB",2)); mh_sh_highlight_all('java');
I would like the result to look like:
Minimum: Abc 1
Maximum: Bca 5
View Replies
View Related