String to Enum problems...
(So Im just strating out with Java, I've already read a book, and almost done a second). Anyway I'm trying to create a program that create word lists using the users input and then writing them to a file. I have most of the code done, im just the most important part! Below is the code, it has some annotations describing the problems. I have two main problems: A) How do I convert the user's String input into the value of my enum, right now i have it set to "{hi, hey, hello, yellow, yo, back}" but in order for the program to work outside of the IDE where i can change that value I need to be able to assign it any value on the fly. and B) How do I have to go about in order to also have numbers in the Symbol object (in order to allow numbers to be added to the combination). Any help is appreciated, and very much welcomed.
Code :
import static java.lang.System.in;
import static java.lang.System.out;
import java.util.Scanner;
public class dic {
enum Symbol {hi, hey, hello, yellow, yo, back}; // these are just values I used in order to demonstrate what the program is supposed to do,
// I need to find a way to get the user to input
public static void main(String args[]) {
String choises = ""; //This line and the input line I want the user to use in order to get letters and words for the 'for' loops below
Scanner input = new Scanner(in);
out.print("Please type the words and/or letters you with to use, and sepparate each of them with a comman and a space.");
out.println("i.e. 'apples, orange, pear, etc."); // I then need a way to that this imput and give it to my enum 'Symbol.
choises = input.nextLine();
for (Symbol ONE: Symbol.values()) {
for (Symbol TWO : Symbol.values()) {
for (Symbol THREE : Symbol.values()) {
for (Symbol FOUR : Symbol.values()) {
for (Symbol FIVE : Symbol.values()) {
for (Symbol SIX : Symbol.values()) {
out.print(ONE);
out.print(TWO);
out.print(THREE);
out.print(FOUR);
out.print(FIVE);
out.println(SIX);
}
}
}
}
}
}
out.println(choises);
}
}
Re: String to Enum problems...
I think enums are compile time constants. I don't know that you can change them at execution time.
Have you read the tutorial? Go to this site and Find enum:
The Really Big Index
Is a Map what you are trying to do?
Re: String to Enum problems...
I just realized that a Map is something in Java, i though you were referring to a phisical map xD. I haven't learned about maps yet, (they're later on in the book...) If enums are compile time constants, I'm gonna have to find a way to do this in a way that I can change during runtime.
Re: String to Enum problems...
Quote:
a way that I can change during runtime.
Arrays or some classes from the Collections framework.
Quote:
creates all the possible combinations with these pieces
Is that like counting, say with 6 digits.
To generate all possible combinations of the digits from 000000 to 999999 is like counting by adding 1 at each iteration: 000001 000002 ...
Re: String to Enum problems...
Quote:
Originally Posted by
Norm
Is that like counting, say with 6 digits.
To generate all possible combinations of the digits from 000000 to 999999 is like counting by adding 1 at each iteration: 000001 000002 ...
Yeah, except I have to mix words and letters and numbers together...
i.e. (If i get 'red', 'blue', '1', 't') and lets say a length of 4 it would go:
redredredred
redredredblue
redredred1
redredredt
redredbluered... etc.
Re: String to Enum problems...
Yes that looks exactly like counting. Except instead of using the 10 digits: 0-9 you are using n Strings.
Re: String to Enum problems...
I just haven't figured out a way to do it, I think I should be able to get it running after I finish this book.
Re: String to Enum problems...
My idea takes two loops and an array.
Re: String to Enum problems...
Can you assign values to arrays in runtime? Also I wasn't aware that you could assign characters or words to arrays, I though it you could only do numbers.
Re: String to Enum problems...
Quote:
Can you assign values to arrays in runtime?
Yes. Very important concept in programming.
Quote:
assign characters or words to arrays
Yes. You can have arrays of any data type.