-1

After reading a file in and assigning each letter to a specific integer value, how would I then convert the read-in string into the initialized integers as seen below?

A = 20
B = 30
C = 40
D = 50

The file has the following data: B D C A C A D B

How do I then convert the letters read in from the file into the numbers? For example, if I read B in, how would I convert it into 30?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Matt
  • 11
  • 1

1 Answers1

3

I suggest you use a Map.

Map<String, Integer> map = new HashMap<>();
map.put("A", 20);
map.put("B", 30);
map.put("C", 40);
map.put("D", 50);

The file.txt has the following data: B D C A C A D B

String item1 = inputFile.next();
Integer value1 = map.get(item1);  // turns B => 30.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130