1

I am a beginner trying writing a program for a Caesar cipher assignment for class. Currently I am stuck trying to create a function that will do the opposite of the first function, take in an integer array and return a String. I'm completely lost on how to do so at this point and could really really use some help.

public static int[] string(String str) {
    int [] arr = new int [str.length()];

    for (int i = 0; i < str.length(); i++) {   
        str.toUpperCase();
        arr[i] = str.charAt(i)-65;
        // System.out.println(arr[i]);--> check to see if stringTo was working
    }
    return arr;
}



public static String symbol(int[] symbols) { 
    String message = new String();

    char[] letters = new char[symbols.length];



    for (int i = 0; i < symbols.length; i++) {

        symbols[i] = letters[i];

        message.toUpperCase();
        message =  message.toString();


        System.out.print(message);

    }    

    return message; 
}
Yaz
  • 23
  • 2
  • 6

4 Answers4

1
int[] a = {1,2,3,4,5,6};

String str = "";

for(int i=0;i<a.length;i++)
{
    str = str + Integer.toString(a[i]);
}
System.out.println(str);
Deepak Sharma
  • 456
  • 4
  • 15
  • Quick follow up question: What if I need to change the int arr to chars before it becomes a string. I think that was what I was having the most trouble with. – Yaz Feb 20 '16 at 05:23
  • You don't need `Integer.toString()`. `str=str+arr[i]` will do. –  Mar 25 '16 at 16:29
1

Approach using Java 8 Streams:

 int[] intArray = new int[] {1, 2, 3, 4};

 String result = IntStream.of(intArray)
                          .mapToObj(String::valueOf)
                          .collect(Collectors.joining(","));
 System.out.println(result); // "1,2,3,4"
user3734782
  • 137
  • 2
  • 11
0

If I understand the code in your first function correctly then you want something that converts [1,2,3,4,5] into "12345" right?

Then your function could look like this:

public static String arrayToString(int[] array) {
    String result = "";
    for(int i : array) {
        result += i;
    }
    return result;
}

But this can have terrible performance for long arrays since each += creates a new String so actually this would be better:

public static String arrayToString(int[] array) {
    StringBuilder builder = new StringBuilder();
    for(int i : array) {
        builder.append(i);
    }
    return builder.toString();
}

javac or the JVM might actually convert the first code snippet into the second one automcatically.

MartinS
  • 2,759
  • 1
  • 15
  • 25
  • Yes, the compiler might. This is an easy append that I think java 5 or 7.. don't remember which will probably try to convert it into one String and it'll have the same impact as using a StringBuilder. – pompanoSlayer Feb 20 '16 at 04:19
0

Here is a snippet of code to help you.

public String changeIntToString(int aNumber){
    String theString = "";
    theString += aNumber;
    return theString;
}

Anything added to a String will turn into a String. So here's another example.

String lotOfInt = "" + 1 + 2 + 3;

That will get turn into a string of "123". I think you get the picture. Now you just need to loop through that array and combine it into a string.

Bonus and right way to do it... Use StringBuilder class. Search what it is and how to use it. ;)

Edited: Ah. MartinS beat me to it. :)

pompanoSlayer
  • 163
  • 1
  • 11