D. Collins
D. Collins

Reputation: 31

Java - How to add to characters in in a string

I am new to Java and coding in general, and am working on a project on cryptography for a class I'm in. I have made a code that first turns letters into numbers e.g. a=1, b=2, etc. The next part jumbles the numbers by assigning numbers to add or subtract to the next character in the coded message. (See picture attatched) Example Image

Example of Letter Number and Jumble Number

Character       Letter Number           Jumble Number
_______________|_______________________|_______________
A               1                       +3
B               2                       -1
C               3                       +2
D               4                       +5
E               5                       -3

So, as an example, say the message that needs to be sent is "abcde". So the number for "a" is 1, but it also comes with a "+3", this will be added onto the number for the next character, so instead of being 2, it will be 5, and so on. The final message will read "1,5,2,6,10."

So far, I have been able to convert the letters to numbers, but do not know how to continue on to adding/subtracting numbers to the numbers already converted from letters. Here's what I have so far.

  import java.util.HashMap;
  import java.util.Map;


  public class CodeApp
  {
  public static void main(String[] args) 
  {
final Map<Character, Integer> map;
final String str = "hello world";
// Message to be encoded goes here  

map = new HashMap<>();  

map.put('a', 1);
map.put('b', 2);
map.put('c', 3);
map.put('d', 4);
map.put('e', 5);
map.put('f', 6);
map.put('g', 7);
map.put('h', 8);
map.put('i', 9);
map.put('j', 10);
map.put('k', 11);
map.put('l', 12);
map.put('m', 13);
map.put('n', 14);
map.put('o', 15);
map.put('p', 16);
map.put('q', 17);
map.put('r', 18);
map.put('s', 19);
map.put('t', 20);
map.put('u', 21);
map.put('v', 22);
map.put('w', 23);
map.put('x', 24);
map.put('y', 25);
map.put('z', 26);
// This code is inefficient like this because I eventually plan to scramble the order of the numbers in relation to the letters, like a=23, b=5, c=15, etc.
for(final char c : str.toCharArray())
{
  final Integer val;

  val = map.get(c);

  if(val == null)
  {   
    // some sort of error
  }
  else
  {
    System.out.print(val + " ");
  }
}

System.out.println();
  }
}

Any and all help is much appreciated!

Upvotes: 2

Views: 646

Answers (4)

mmuzahid
mmuzahid

Reputation: 2270

You could try to do this using two HashMap as bellow:

String str = "abcde";

Map<Character, Integer> charMap = new HashMap<Character, Integer>();// value of character
charMap.put('a', 1);
charMap.put('b', 2);
charMap.put('c', 3);
charMap.put('d', 4);
charMap.put('e', 5);
// put more entry for rest of the characters

Map<Character, Integer>  jumblMap = new HashMap<Character, Integer>();// value to add
jumblMap.put('a', 3);    
jumblMap.put('b', -1);
jumblMap.put('c', 2);
jumblMap.put('d', 5);
jumblMap.put('e', -3);
// put more entry for rest of the characters

char c = str.charAt(0);
int val = charMap.get(c);// No jumble value for first character
System.out.print(val + " ");
char prevC;
for(int i = 1; i < str.length(); i++) {
  c = str.charAt(i);
  prevC = str.charAt(i - 1);// previous character
  val = charMap.get(c) + jumblMap.get(prevC);// evaluate with jumble value of previous char
  System.out.print(val + " ");
}

Output:

1 5 2 6 10

Here, initially stored all character values and jumble values in two HashMap.

charMap - holds each character value

jumblMap - holds jumble values

charMap.get(c) + jumblMap.get(PrevC) - evaluates value assuming all character has entry in each above map.

Upvotes: 1

user5896972
user5896972

Reputation:

Not an answer:

A tricky approach to get alphabet serial is

String str = "AbCdEf";
for (char c : str.toLowerCase().toCharArray()) {
   int val = Character.toLowerCase(c) - 'a' + 1;
   System.out.print(val + " ");
} 

Output: 1 2 3 4 5 6

Upvotes: 0

Haniyeh Khaksar
Haniyeh Khaksar

Reputation: 804

you have a bad code here, define your variable out of for loop. below code is my suggestion, first you can define your hashmap like below:

final Map<Character, String> map;
map.put('a', "1");

and do it in loop:

 String val="";
    for(final char c : str.toCharArray()) {
       if(map.get(c)!=null) {
           val = val + map.get(c).toString();
    } else {
     // some sort of error
     }
 }

Upvotes: 1

christutty
christutty

Reputation: 952

As a conceptual solution I suggest adding each value to a list of codes and then transforing them for output. Something like:

List<Integer> codes = new ArrayList<Integer>();

for(final char c : str.toCharArray())
{
  final Integer val;

  val = map.get(c);

  if(val == null)
  {   
    // some sort of error
  }
  else
  {
    // don't try to output here
    //System.out.print(val + " ");

    // add this code to the list
    codes.Add(val);        
  }
}
// transform codes list into string delimited text and output

Upvotes: 0

Related Questions