Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: String to Enum problems...

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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.
    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);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

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

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by alex13809; August 2nd, 2011 at 11:16 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: String to Enum problems...

    a way that I can change during runtime.
    Arrays or some classes from the Collections framework.

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

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String to Enum problems...

    Quote Originally Posted by Norm View Post
    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

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

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: String to Enum problems...

    My idea takes two loops and an array.
    Last edited by Norm; August 2nd, 2011 at 11:57 AM.

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by alex13809; August 2nd, 2011 at 11:56 AM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,306
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: String to Enum problems...

    Can you assign values to arrays in runtime?
    Yes. Very important concept in programming.
    assign characters or words to arrays
    Yes. You can have arrays of any data type.

  11. The Following User Says Thank You to Norm For This Useful Post:

    alex13809 (August 2nd, 2011)

Similar Threads

  1. [SOLVED] Enum types, how does he know %s value?
    By beer-in-box in forum Java Theory & Questions
    Replies: 11
    Last Post: September 17th, 2011, 01:47 PM
  2. ClassCastException with Enum Combo Box
    By Diplo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2011, 02:22 PM
  3. [SOLVED] Enum problem
    By pajfilms in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2011, 08:26 AM
  4. enum, value of: Why is this code line structured the way it is?
    By SPACE MONKEY in forum Java Theory & Questions
    Replies: 5
    Last Post: March 28th, 2011, 10:15 AM
  5. Having problems with String out of bounds errors
    By Bill_H in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2009, 02:47 PM