0

Say we have a bunch of char's: 's', 'w', 'e', 's', 't', 'h'

How can we see if we type a character via (char)scanner.nextLine().charAt(0); at which places the numbers are(1,2,3,4,5,6). For example, if I type w I get 2 back, or if I type s, I get 1 and 4 back.

This is the code I already have:

   Scanner letter= new Scanner(System.in);
   System.out.print("Type a character:");
   char let= letter.nextLine().charAt(0);

   char[] word = {'s', 'w', 'a', 'g', 'w' };
   System.out.print();
Erba Aitbayev
  • 4,167
  • 12
  • 46
  • 81
henk_bae
  • 25
  • 7

1 Answers1

0

you can simple do it using map

        Map<Character,List<Integer>> map = new HashMap<Character,List<Integer>>();

then iterate on the character in the array and add their positions to the map with key the character like this

         if(map.get(word[i]) == null)//i is the index of the character
                    map.put(word[i],new ArrayList<Integer>());
          map.get(word[i]).add(i);

then when you insert your character you get the related list and print its content

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
  • 1
    This is far from simple, and considering how basic the question is, I don't think the OP is ready for this, nor do I think it would be suitable for their needs. – 4castle Oct 01 '16 at 17:38
  • I respect your opinion and will vote you comment too – Amer Qarabsa Oct 01 '16 at 18:07
  • This is indeed too complicated than I can handle :p, i just started a week ago with java, thanks for the answer tho! – henk_bae Oct 01 '16 at 18:14
  • In my opinion you do not need the answer which you can understand, you need the answer which will set you in the right path , eventually you will understand it. – Amer Qarabsa Oct 01 '16 at 18:18
  • I know I have to learn the hard way but as it is right know i really can't figure out how to use your solution. Can you be kind enough to adjust it so i can use it and then understand it? greetings, Tom – henk_bae Oct 01 '16 at 18:41