Strings With Digits Not Permitted Enum Values?

May 28, 2013

If you have an enumeration type with instances whose publicly accepted names are numbers, e.g. a Chevy 396 and I'm sorting this car according to makes and models that I've already laid out within enum types for each. Here the make instance is "Chevy" and the model instance is just "396".

I know I could make this "396" into "Ch396" and this would work programmatically. But this might confuse some people who just expect to see "396".

In short, why are enum instances not allowed to be of String type rather than just standard Java identifiers ?

View Replies


ADVERTISEMENT

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

How To Find Maximum Of Two Enum Values

Oct 6, 2014

I have a set of enum values (let's call then ONE, TWO, THREE.....). I want to find the larger of two of them. But max(ONE,THREE) gives a compile error as MAX isn't defined for type-safe enums. Fair enough.
 
I also agree that one shouldn't be able to use arithmetic functions on enums.
 
But as Enum implements Comparable, one can write a function which implements max and min, rather inefficiently I assume.
 
Is there a better way of getting the max/min of an enum? And if not, can the Java team be persuaded to implement it?

View Replies View Related

User To Input Integer And Then Outputs Both Individual Digits Of Number And Sum Of Digits

Feb 2, 2015

Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.

First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.

import javax.swing.JOptionPane;
public class DigitsAndSum {
public static void main (String[] args) {
String str1;
String str2;
int int1 = 0;
int int2 = 0;

[Code] ....

View Replies View Related

Multiplying Two Strings - Float Values

Jul 1, 2014

I am currently learning about JOption pane. Using Strings I am accepting an input from the user and by using the Interer.ParseInt(variable) option I am able to multiply this two strings using the code below.

String Length;
Length = JOptionPane.showInputDialog("Enter the Length");
String Breadth;
Breadth = JOptionPane.showInputDialog("Enter the Breadth");
System.out.println(" Area is " + (Integer.parseInt(Breadth) * Integer.parseInt(Length)));
System.exit(0);

Now My question is... How Do I make my code accept Decimal values. E.g. My Code should accept 10.02 as Length and 20.42 as Breadth and give the product a Decimal. How Do I do this???

View Replies View Related

Subtracting Strings - Parsing Two Values To Ints

Nov 5, 2014

I have started learning Java and have some across some difficulties. I'm trying to subtract two strings.

for example, with these strings;"032"&&"100". I want to be able to subtract each number individually so that the answer would be "032". 0-1;3-0;2-0;. if i get a negative number, the number would be zero. hence, the 0-1 staying zero in the answer.

I have tried using substring, and parsing the two values to ints, but don't know what to do next. I have also tries using a for loop, to go through each arrays of the strings. I am not allowed to use StringBuilder and I need to return "032".

Here's what i've done so far:
 
public static String appliquerCoup( String combination, String coup ) {
String nouveauCoup=""; 
if(combination!=null&&coup!=null){
for(int i=0;i>combinaison.length();i++){

[Code] ....

View Replies View Related

How To Find Whether Strings Contained In Array Of Strings Constitute A Similar Character Or Not

Aug 25, 2014

I am having an array of strings and i want to find out whether these strings contained in the array contain a similar character or not.For example i am having following strings in the array of string:

aadafbd
dsfgdfbvc
sdfgyub
fhjgbjhjd

my program should provide following result: 3 because i have 3 characters which are similar in all the strings of the array(f,b,d).

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

Sorting Lowercase Strings Before Uppercase Strings

Mar 26, 2014

I can sort strings in a collection by uppercase and then lowercase though I was wondering if there is any way of doing it in reverse, sorting by lowercase then by uppercase.

View Replies View Related

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

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 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

Getting Error - Class Interface Or Enum Expected

Aug 7, 2014

import java.io.*;
class addition {
public static void main(String[] args) {
int num1,num2,sum;
try {
DataInputStream x=new DatainputStream(system.in);

[Code] ,,,,,

View Replies View Related

Constructor Needs To Import Enum Blackjack Game

Jun 21, 2014

I am just not importing the Enum value properly. The error I am getting with the DECK class is

import java.util.ArrayList;
public class Deck {
ArrayList<Card> deckList = new ArrayList<Card>();
String[] value = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
String[] suit = {"Hearts","Clubs","Spades","Diamonds"};

[Code] ....

1 error found:
File: C:UsersFouadDesktopSchooCSC 162 SUMMER 14lackjack1Deck.java [line: 19]
Error: constructor Card in class Card cannot be applied to given types;
required: CardEnum.Rank,CardEnum.Suit
found: java.lang.String,java.lang.String
reason: actual argument java.lang.String cannot be converted to CardEnum.Rank by method invocation conversion

I have also tried this with the Deck Class

import java.util.ArrayList;
public class Deck {
ArrayList<Card> deckList = new ArrayList<Card>();
String[] value = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
String[] suit = {"Hearts","Clubs","Spades","Diamonds"};

[Code] ....

Resulting ERROR message

2 errors found:
File: C:UsersFouadDesktopSchooCSC 162 SUMMER 14lackjack1Deck.java [line: 19]
Error: cannot find symbol
symbol: variable value
location: class CardEnum
File: C:UsersFouadDesktopSchooCSC 162 SUMMER 14lackjack1Deck.java [line: 19]
Error: cannot find symbol
symbol: variable suit
location: class CardEnum

THE FULL CODE!(BELOW)

public class BlackJack{
BlackJack() {
Deck deck = new Deck();
deck.createDeck();
System.out.println(deck.deckList);
}
public static void main(String[] args) {
new BlackJack();

[Code] ....

View Replies View Related

Dynamic Enum - How To Make DIME(10) False

Apr 20, 2015

I wrote a simple enum and a test class. The enum represents us coins and my test class uses the enum to calculate the change with the least number of coins. For example, I pass in 95 cents, I get back 3 quarters, 2 dimes. It all works just fine, easy to implement and I see why I should use an enum. Then I thought, what if the person is out of dimes. How could I tell the enum to skip dimes in it's switch statements? How would I make DIME(10) false?

View Replies View Related

Enum Class Throwing Illegal Argument Exception?

Mar 4, 2015

I have a enum class which contains some string which i am comparing to a string i get from a user

Java Code:

public enum Compare {
a, b, c, d, e, f
} class SomeClass {
String method(String letter){
Compare word= Compare.valueOf(letter);
}
} mh_sh_highlight_all('java');

Everything works fine but when I added a new word to it like "g" it throws the IllegalArgumentException ?

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

Calendar (Days Enum) - Determine If Day Is Weekday Or Weekend

Oct 17, 2014

The instructions state: Create the following values for the enum (and make sure that they are spelled correctly, case matters):

a. SUN
b. MON
c. TUE
d. WED
e. THU
f. FRI
g. SAT

Create a private constructor for the Day enum that takes a boolean value. This boolean will determine if the day is a weekday (if it is true) or weekend (if it is false). Document it with a JavaDoc. This value will need to be stored as part of the object. Update the enum values so that they correctly call the constructor. Saturday and Sunday are the only days considered part of the weekend.

Implement the following methods:
a. boolean isWeekday()
i. Returns true if the day is a weekday.

b. Boolean isWeekend()
i. Returns true if the day is a weekend. (How could you determine that?)

c. String toString()
i. Returns the full name of the day for the enum value (i.e. Monday or Tuesday). Use either a set of nested ifs or a switch statement.

Hint: You will want to compare the this reference against the possible values in the enum.

View Replies View Related

Creating Enums Without Using Enum Class And Private Static Final Keywords?

Jun 11, 2014

I am wondering if there is a way in jave to use enums WITHIN a class (without creating a separate enum class) without using private static final. Something like as folows:

class My Class {
myEnum {ACTIVE, INACTIVE, PENDING};
}

is there something like this available?

View Replies View Related

How To Sum Digits

Jan 30, 2015

I have to seperate a number 9876 to 6 7 8 9, to 9 8 7 6. I need to know how to sum the digits. I have gotten to this point ::

import java.util.*;
public class week4program
{
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
int num1;
int digit;
int sum = 0;

[code]....

To this point its gives me the seperate integers. OK but how do I get the variable integers into seperate containers and then add them up?My assignment is to do that, and what I have above gets me to where I have seperate digits, but how do I catch them into seperate entities to be added to a sum?

View Replies View Related

Sum Of Odd Digits Of Input

Oct 14, 2014

I have it so it gives me the sum of any digit, i just cant figure out how to get only the odd digits to add up. for example if I were to type 123. The odd numbers = 4 but using this program i get 6

class SumOfDigits {
public static void main(String args[]) {
int sum = 0;
System.out.println("Enter multi digit number:");

[Code] ....

View Replies View Related

Sum Of Digits In Java

Feb 3, 2014

import java.util.Scanner;
public class CubesSum { 
public static void main (String [] args){
int input;
System.out.println("Enter a positive integer:");
Scanner in = new Scanner(System.in);
input = in.nextInt();

As you can see I'm using a while loop. For part B which is to modify to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits. So for example, 371 = 3³+7³+1³. How to do it? I need to wrap a for loop around my while loop...

View Replies View Related

Digit Sum Of Integer Is Sum Of All Digits

Oct 13, 2014

Digit sum of an integer is the sum of all its digits. For example, the digit sum of 123 is 1+2+3 = 6

and the digit sum of 99 is 9 + 9 = 18.

But how to write the programm that will count those numbers?

View Replies View Related







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