Ok, I am stumped here. I really looked into this and cant seem to find specifically what I am trying to do. I am two weeks into Java in school and new to programming so please pardon any errors I may make.
I am working on a string manipulation program and among other tasks in it I am to create a sting called names that holds SunMonTueWedThuFriSat, now I need to figure out how to grab and display in JOptionPane the 3 characters in that string that go with the numbers I am supposed to associate with them (0=Sun, 1=Mon, etc.) So if the user inputs 2 it should display Tue. I have a basic understanding on how to display those characters in a string, but for the life of me i cant seem to figure out how to associate those numbers with those characters. Every time I try to work something I keep getting errors and frustration.
Thanks for all your help!!!
05 Answers
If the days of the week has to be in a single string, you can parse the string for each 3 character substring, and store then in an ArrayList. Then, the provided number will match the index of the Day of the Week in the List.
4You should be able to just use substring, since all the abbreviations are three characters.
String s = "SunMonTueWedThuFriSat"; Prompt the user to enter a number (0-6):
String in = JOptionPane.showInputDialog("Please input a number (0-6)"); Now convert it to a number
int choice = Integer.parseInt(in); And select the right portion of the string:
String day = s.substring(choice*3, choice*3+3); Now just show this day in a JOptionPane:
JOptionPane.showMessageDialog(null, "Information", "You chose: " + day, JOptionPane.INFORMATION_MESSAGE)); 4Check HashMap.
For example:
HashMap<int, String> aWeekMap = new HashMap(); aWeekMap.put(0, "Mon"); aWeekMap.put(1, "Tue"); System.out.println(aWeekMap.get(0)); // prints Mon. In memory you can visualize as following:

If you can use an array of string, it will be easier:
String[] names = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; int input = 2; System.out.println("The day is " + names[input]); 4use the follwing code
private ArrayList arr=new ArrayList(); arr.Add(0,names.subString(0,2); arr.Add(0,names.subString(3,5); arr.Add(0,names.subString(6,8); arr.Add(0,names.subString(9,11); arr.Add(0,names.subString(12,14); arr.Add(0,names.subString(15,17); arr.Add(0,names.subString(18,20); and
JOptionPane.showInputDialog(null, "Please choose a name", "Example 1", JOptionPane.QUESTION_MESSAGE, null, arr);