2
package com.nusecond.Code; import java.util.*; public class Code1 { public static void main(String args[]){ HashMap<Integer, Character> hmap = new HashMap<Integer, Character>(); hmap.put(0, 'D'); hmap.put(1, 'G'); hmap.put(2, 'B'); hmap.put(3, 'Q'); hmap.put(4, 'P'); hmap.put(5, 'M'); hmap.put(6, 'R'); hmap.put(7, 'Y'); hmap.put(8, 'Z'); hmap.put(9, 'I'); Iterator<Integer> keySetIterator = hmap.keySet().iterator(); while(keySetIterator.hasNext()){ int key=keySetIterator.next(); System.out.println("key:" + key + " value: " +hmap.get(key)); } for(int j=1001;j<9999;j++); Random random=new Random(); int res=random.nextInt(100); System.out.println(res); } } 

output:

key:0 value: D key:1 value: G key:2 value: B key:3 value: Q key:4 value: P key:5 value: M key:6 value: R key:7 value: Y key:8 value: Z key:9 value: I 12 

Here i'm trying to convert generates 2 digit random number to alphabets for example in the above in place of 12 i want GB....... like that.... any help.....thanks in advance.... i have a deadline to finish this.... so plzz help me.......

3
  • Forget that you are generating a random number between 0 and 99 for a minute, and pretend it is between 0 and 9 - do you know how you would get the single letter for that number? Commented Dec 22, 2015 at 8:48
  • user defined characters storing in integers between 0 to 9 using hashmap Commented Dec 22, 2015 at 9:59
  • if i use the random generator method it'll generate any 2 digit number between 10 to 99 (ex if output is 23 i want that 23 in alphabet format like you see in the map 2 means B and 3 means Q so final reselt must be BQ) Commented Dec 22, 2015 at 10:02

5 Answers 5

1

Please use the below code,

HashMap<Integer, Character> hmap = new HashMap<Integer, Character>(); hmap.put(0, 'D'); hmap.put(1, 'G'); hmap.put(2, 'B'); hmap.put(3, 'Q'); hmap.put(4, 'P'); hmap.put(5, 'M'); hmap.put(6, 'R'); hmap.put(7, 'Y'); hmap.put(8, 'Z'); hmap.put(9, 'I'); Iterator<Integer> keySetIterator = hmap.keySet().iterator(); while(keySetIterator.hasNext()){ int key=keySetIterator.next(); System.out.println("key:" + key + " value: " +hmap.get(key)); } for(int j=1001;j<9999;j++); Random random=new Random(); int res=random.nextInt(100); System.out.println(res); String s = ""; s = String.valueOf(res); String result=""; for(int i=0;i<s.length();i++) { result+=hmap.get(Integer.valueOf(String.valueOf(s.charAt(i)))); } System.out.println(result); } 
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure, I understood your idea clear. But try this:

String s = ""; s = String.valueOf(res); System.out.println(hmap.get(Integer.valueOf(String.valueOf(s.charAt(0)))) + "" + hmap.get(Integer.valueOf(String.valueOf(s.charAt(1))))); 

If res is 12, the result GB etc. Declare s before and put the second and third row of my code inside your for

3 Comments

But notice it doesn't work for 0<=res<10 But I don't know what are you going to do with one digit numbers.
Converting an int to a string to extract a character to convert it back to an int... If only there were a better way. Also: doesn't work if res < 10. Also: no need to assign "" to s.
Yep. Agree totally. That was my first idea but not the best.
0

you can use i % 10 for getting ones and i / 10 for getting decimal, for example:

int i = 12; System.out.println(i / 10); // would be 1 System.out.println(i % 10); // would be 2 

then use it to feed your hashmap.

Comments

0

Try this

 Random random = new Random(); IntStream.generate(() -> random.nextInt(100)) .limit(1000) .mapToObj(i -> "" + hmap.get(i / 10) + hmap.get(i % 10)) .forEach(System.out::println); 

Comments

0

Try this program as it gives the output as mentioned. Hope this helps.

 int [] digit_face; int digits=0; HashMap<Integer, Character> hmap = new HashMap<Integer, Character>(); hmap.put(0, 'D'); hmap.put(1, 'G'); hmap.put(2, 'B'); hmap.put(3, 'Q'); hmap.put(4, 'P'); hmap.put(5, 'M'); hmap.put(6, 'R'); hmap.put(7, 'Y'); hmap.put(8, 'Z'); hmap.put(9, 'I'); for(int j=1001;j<9999;j++); Random random=new Random(); int res=random.nextInt(100); for(int i=res;i>0;i=(i/10)){ digits++; } digit_face=new int[digits]; digits=0; for(int i=res;i>0;i=(i/10)){ digit_face[digits]=i%10; digits++; } for(int i=(digits-1);i>=0;i--){ System.out.print(hmap.get(digit_face[i])); } 

2 Comments

key:0 value: D key:1 value: G key:2 value: B key:3 value: Q key:4 value: P key:5 value: M key:6 value: R key:7 value: Y key:8 value: Z key:9 value: I 32 The Key values are:-
i don't want all that stuff in my console i need only two alphabets like if the generated random number is 34 i want QP only in my console

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.