Iterate Enum Type Without Instance

Sep 16, 2014

Is there anyway to iterate an enum type without an instance. As some context, consider the following code:

Java Code: public interface GenericChecker
{
public bool isValid(String str);
} mh_sh_highlight_all('java'); Java Code: public class EnumChecker<T extends Enum<T> > extends GenericChecker
{
private Class<T> enumType; //No instance

[code]....

toString method of the enum types has been overridden so that it returns the name assigned to the enum rather than the enum name itself. For example an enum might be SOME_ENUM("Assigned name"), therefore toString returns "Assigned name" rather than "SOME_ENUM". The idea is that a field from a table can be handed to the isValid(String) function on the GenericChecker base, and the derived class will then check to see if the field matches valid data as far as it is concerned.Thus, I can create a whole bunch of checkers easliy:

Java Code: GenericChecker checker1 = EnumChecker<EnumType1>();
GenericChecker checker2 = EnumChecker<EnumType2>();
GenericChecker checker3 = EnumChecker<EnumType3>();
GenericChecker checker4 = SomeOtherChecker(); mh_sh_highlight_all('java');

The problem is, if I use the EnumChecker then the expression "enumType.getEnumConstants()" in the isValid function blows up because enumType is null.

View Replies


ADVERTISEMENT

Enum Type And Array List

Feb 10, 2015

Here I have an enum class

public enum Money{

ONE_PENNY(1),
TWO_PENCE(2),
FIVE_PENCE(5),
TEN_PENCE(10),
TWENTY_PENCE(20),
FIFTY_PENCE(50),
ONE_POUND(100),
TWO_POUNDS(200);

private int coin;
Money(int c) {
coin = c;
}
int showCoin() {
return coin;
}

and for a test class, I need an array list with a couple of coins in it (i.e. ONE_POUND, TWO_POUNDS) and a loop that adds together the values of the coins in the list and prints the result. How can I do this?

View Replies View Related

JSF :: Enum Type In Parametrized Class

Dec 4, 2014

I'm developing a JSF application.I have some enums, for example

public enum Gender {
MALE("Male"),
FEMALE("Female");
private final String label

[code]....

I want to put the enum value in <h:selectOneMenu>. I use a bean:

@Named(value = "genderBean")
@RequestScoped
public class GenderBean {
/**
* Creates a new instance of GenderBean
*/
public GenderBean() {

[code]...

How can i call the method .values() of enum for a generic enum T, like in the striked code?

View Replies View Related

Enum Class - How To Pass In Faction Type In For Daunt

Mar 6, 2015

Suppose I have an enum class named Faction and one of the constants is named DAUNT. I created a class of the enum DAUNT but how can I pass in a DAUNT faction type in for Daunt?

Java Code:

public enum Faction {
ALMIGHTY, AMBITION, DAUNT, RESTLESS, CAN;
}

//new file
public class Daunt {
public Daunt() {
}
} mh_sh_highlight_all('java');

View Replies View Related

Describing The Type Of Instance Variables?

Oct 9, 2014

What is the correct way of describing the type of instance variables?

It is obvious in the case of a primitive type

Example: private int number;

The (data)type of number is integer.

What if the instance variable is an object or a wrapper?

example: private Person person;

the (data?)type of person is an object of the type/class Person?

example: private Integer number;

View Replies View Related

How To Make Sure Only One Particular Type Of Instance Is Open At A Time

Feb 23, 2015

This isn't asking to make a Singleton, where I want only one type of a type of window period.

It's not where I want only one instance of the program itself open at a time. It's more along the lines of, though I don't care if there is more than one Microsoft Word open at the same time, I don't want more than one instance of blablabla.docx to be open at once.

In other words, I'd need to scan my system or my list of open windows of a particular type to make sure that no two ones where equals is true will match up.

I was going to do it to associate it with a particular name with a window (it's sort of an addressbook, though not quite.) I'd use a JFrame subclass to make these windows that have a name go with them. For now, unless I find the great need for this project that there might be someone with the exact same name around here, I'm going to use equals() with name on this. I can type cast and whatnot.

However, how do I get to know what windows are open at once so I can check all my list of open windows, and I'd prefer to check only ones of certain types, though I may have to check them all indirectly perhaps, before I check the names.

I'm wondering is it a getChildren() of the main window or something else? How do I know what all of my open windows are?

I'd prefer to have to check as few as possible and not have to check unnecessary ones.

As for the structure of my thing that would click that would open the new window, I'm not sure yet. I'm almost leaning JLabel, though it's a bit less sophisticated, as it might be more difficult to add to a JList (maybe I"m wrong on thinking on that. It's been a little while since I last worked with JLists.)

(I think I can set list data, but the problem is, even using an ArrayList, it only takes an array for this as a param and ArrayList returns an Array of type Object, which means a lot of pesky type casting every time just to add or remove a name.)

It does have Vector, but I thought Vector was being phased out.

Anyway, without having too much code given, as I'm not sure how I'll set this up yet, how do you get access to all of the windows being opened to be able to check them for certain traits?

View Replies View Related

How To Pass Object Type To A Method / Then Create Instance

Aug 9, 2014

Essentially, the code I want is:

public void randomCreate(ParentObject obj){
int x = random(0-4); //pseudo
int y = random(0-4); //pseudo
create new ParentObj(x,y);
}

ParentObject is actually abstract, so you would only ever pass one of its children objects to it, and a child object of that type would be created. It seems like there should be a way to pass a type, rather than an object, and then create an instance later down, but I don't know if that is actually possible, or if it is poor programming style.

View Replies View Related

Game Coding - No Enclosing Instance Of Type Background Is Accessible

Dec 11, 2014

So I'm fairly new to java and have been learning game development. I've come across this error message on my StartingClass.java coding. Here is the Coding

@Override
public void start() {
bg1 = new Background(0,0); // This line is giving me the error!!!!!!!!!!!
bg2 = new Background(2160, 0);
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
robot = new Robot();

Thread thread = new Thread(this);
thread.start();

The error reads "No enclosing instance of type BackGround is accessible. Must qualify the allocation with an enclosing instance of type BackGround (e.g. x.new A() where x is an instance of BackGround)." Now I have seven classes in my in my games src folder and none of them but the StartingClass.java has the error. Once again I'm fairly new to java and have not benn able to fix it.

These are the classes{

BackGround.java
Enemy.java
Heliboy.java
package-info.java
Projectile.java
Robot.java
StartingClass.java ( errors in this class )
}

View Replies View Related

Converting Parent Instance To Child Instance?

Mar 7, 2014

I've Parent and child(extends Parent) class To initialize the constructors, I'm injecting from google.juice#injector. Let me show the code,

Parent.class

public class Parent{
private Animal animal;
@inject
Parent(Animal animal){
this.animal = animal;

[code].....

When I do this, ClassCastException is happening. Why is it so? Is there any way to convert instance of parent to child instance.

View Replies View Related

How To Get Enum Name By Value

Jan 12, 2015

I want to have a priceObject which is constructed by a price the enumtype and the name.

public class Testing {
public static void main(String[] args) {
PreisObject p1 = new PreisObject(1,Price.liquid,"TEST1");
System.out.println(p1);
PreisObject p2 = new PreisObject(2,Price('f'),"Test2");

[Code] .....

As you can see with PreisObject2 I want to check the enum by the value and not by the name as in PreisObject1.

Or do I have to use a if-else or switch statement to do something like this?

View Replies View Related

Iterate Through Objects

Feb 27, 2015

I would like to know how I can iterate through objects . I have a manually created linked list (without using the built-in method one). So in the memory my object looks like this(attachment).I would like to do a for loop or while loop to get each element under the test3.head.

View Replies View Related

How To Iterate Two Lists At A Time

Mar 25, 2015

List list1 = query1.list();
List list2 = query2.list();
 
for (Iterator itr = list1.iterator(); itr.hasNext();) {
Object[] row = (Object[]) itr.next();
}

View Replies View Related

Iterate Through Multimap By Compare

Mar 22, 2014

I've a contents in multimap like this,

key, value

{Name=[Amit, Akash, Amulya, Aparna, Angle],
Mail_Id=[amit@gmail.com, akash@gmail.com, amulya@gmail.com, aparna@gmail.com, angle@gmail.com]
Gender=[male, male, Female, female, female],
Age=[14, 15, 16, 17, 18]
}

Now, I want to display it like this,

Name = Amit
Mail_Id=amit@gmail.com
Gender=male
Age=14

Name = Akash
Mail_Id=akash@gmail.com
Gender=male
Age=15

Name = Amulya
Mail_Id= amulya@gmail.com
Gender=Female
Age=16

Name = Aparna
Mail_Id=aparna@gmail.com
Gender=Female
Age=17

Name = Angle
Mail_Id=angle@gmail.com
Gender=Female
Age=18

I mean, Each first element matches to first element of the next key. How to do this?

View Replies View Related

How To Iterate Through Linked Lists And Add Them To A Map

Mar 10, 2014

I have an XML sheet and my project is top retrieve the required elements from XML sheet. So my format of XML was like follows:

<Class>
<Employees>
<EMPLOYEE>
<ENum> Abc123</ENum>
<Ename> John<?Ename>
<EType>Mathematics</EType>

[Code] ....

I have used unmarshalling concept to retrieve the data elements... I have to check whether the elements satisfy few regulations when compared with data in Database. So, i thought of grouping the employees depending on EType. I have created a Map with linkedlist of employees. Say Map<String, LinkedList<Employe>>EmpMap=new Map<String, LinkedList<Employe>>();

I have already created a class named Employee which has all the setter and getter methods for employee.

Here am going to take Etype(Employee type) as key and linkedlist(list of employees of certain type) as value. How to iterate these linked lists and place them in my Map.

View Replies View Related

JSP :: How To Iterate Over Supplied Items

Mar 11, 2014

I facing issue with nested <c:forEach in my jsp page.I am using jstl.jar..Here is my code

in JAVA I have -->
List<ProductDefViewBean> productList = new ArrayList<ProductDefViewBean>();
productList.add(objProductDefViewBean);
request.setAttribute("ProductList", productList);
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

[code]...

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

View Replies View Related

I/O / Streams :: How To Iterate Over Files In A Directory One By One

Oct 5, 2014

Requirements - Use only standard Java API and no apache file utils for this.

Most of the answers I found on the internet either dont meet this requirement or load all file names into an array which can consume too much memory when no. of files = 20,000+. how I can do this. Is there also a way to keep track of new files that were added during the execution of the loop in this code ?

View Replies View Related

Breadth First Search - How To Iterate Through ArrayList

Aug 18, 2014

I have done this before in C++ but now I want to do it in Java. I am not sure how to iterate through a ArrayList, I have look several places because lets be honest the first stop is normally google. I tried the examples but with no luck.
 
for(auto it= foo.begin(); it!= foo.end(); foo++) {
*it //do something.
}

Here is the program:
 
package org.search.BFS;
import java.util.Queue;
public class BFS extends Graph {
private Queue<Integer> q;
BFS(int s) {
//mark all the vertices as not visited.

Also the below is giving me a Type safety: The expression of type ArrayList[] needs unchecked conversation to conform to ArrayList<Integer>[]

adj = new ArrayList[V];

What does this mean?

View Replies View Related

Iterate Over List And Get Values From Object?

Jan 22, 2015

first i looked at this example and understand this fine:

import java.util.ArrayList;
import java.util.Iterator;
public class Main {

[Code]....

View Replies View Related

JSP :: Code To Iterate Excel File?

May 6, 2014

I have a JSP code which will open the contents of a excel file.

Now i need this contents of the excel file to be added dynamically to a table in the sameJSP. How to proceed.At the maximum 10 rows can be added dynamically.

Assuming i have a table in JSP which has heading first name,last name,DOB and Gender. After opening the excel through JSP the contents needs to be iterated and added to the columns mentioned.

View Replies View Related

Array - How To Iterate Through Only Values Greater Than 0

Feb 9, 2014

I have an 46x9 array. I only have 108 values in the array for which I need to perform preliminary computations. How do I get the read to only read the 108 values whose values are greater than 0 and skip the other 495 whose values are 0?

View Replies View Related

Learning To Iterate A List And Search Through Another

May 5, 2015

I am learning iterating through lists. What I have so far is two Hash Sets and two Tree sets. Hash Set 1 and Tree set 1 include the words from Roughing it by Mark Twain. Hash set 2 and tree set 2 include the words from Adventures of Huckleberry Finn by Mark Twain. (Everything is read from a file I made).

I am stuck trying to find out how to "Iterate through the words in HashSet1 and search for these words in both TreeSet2 and in HashSet2".Here is my code:

public class UsingSets {
public static void main(String[] args) throws FileNotFoundException {
String riHashIterator = null;
HashSet<String> riHash = new HashSet<>();
Scanner input = new Scanner(new File("roughingit.txt"));
while(input.hasNext()){
String riHashWords = input.next();
riHashWords = riHashWords.toLowerCase();
riHash.add(riHashWords);

[code]....

View Replies View Related

Trying To Iterate Though Array To Display Value In Calculator

Apr 20, 2014

my getDisplayValue() method. I am trying to iterate though an Array to display a value in a calculator, but I doesn't work. I keep on getting these weird magical numbers at the end of the iteration. Note that this is done in BlueJ.

Calculator -> UserInterface -> CalcEngine and Calculator -> CalcEngine.
public class CalcEngine
{
//Instance variables used.
//These are all the instance variables I used to implement
//a complete calculator solution.
 
[code]...

View Replies View Related

Why Enum Types Not Being Recognized By Methods

Mar 17, 2014

Attempting to write a custom comparator for sorting songs, but I keep getting errors saying that the types cannot be resolved.

Java Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.*;
public class SongCollection {

[Code] .....

View Replies View Related

Creating Enum With String And Method

Nov 9, 2014

How would I go about and make an enum, that has Strings and Methods?I want to make a class called GraphicalEffects, this class and be instanstiated and it has a method to apply graphicalEffects AS methods or some type of references to methods in an ArrayList.

View Replies View Related

Method Values For Enum Types

Nov 21, 2014

What class does method Planet.values() in the code below belong to? I thought it belongs to java.lang.Enum but when I could not see it in Java API 7.

package enumeration;
public class EnumTest {
public static void main(String[] args) {
//Planet myPlanet = Planet.EARTH;
// Check arguments supplied
if (args.length != 1) {
System.err.println("Usage: java EnumTest <earth_weight>");
System.exit(-1);

[code]....

View Replies View Related

Enum Properties / Values / Fields

Jul 11, 2014

So I have an Enum file with 119 constants and each constant of that type has 20 fields that come with it. All the fields are the same type and named the same (e.g. there are 119 of Object obj, one for each constant), and I want to run the same methods over them. Since the Objects of the same type are named the same for each constant, I just have them named explicitly in get-er methods.

This worked fine when I just put all 20 fields through the constructor and set them as fields under all the constants. But I realized that if I wanted to make an instance of this Enum class, I'd have to enter in all 20 fields when they are all a set of Objects with unique values. So I then put them as fields under their own respective constant to make it easier to create instances of this enum. But now my methods don't work.

A) I don't really understand why they don't work anymore?
B) Is there a way to fix it without putting all the methods under each constant?

Example:

public enum MyEnum {
AAA {
private MyObject obj = new MyObject (3.0);
},
BBB {
private MyObject obj = new MyObject (1.5);
},
CCC {
private MyObject obj = new MyObject (6.5);
},
DDD {
private MyObject obj = new MyObject (3.5);
};

public double getObjVal() {
return obj.value(); // it can't find this obj should I move it up to where the constants are declared?
}
}

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved