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; 
}
3
  • String class has constructor new String(char[] value). Just use it. Commented Feb 20, 2016 at 4:01
  • 2
    What is the error that you are getting? Commented Feb 20, 2016 at 4:01
  • 1
    refer this stackoverflow.com/questions/10904911/… Commented Feb 20, 2016 at 4:04

4 Answers 4

2
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);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
You don't need Integer.toString(). str=str+arr[i] will do.
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"

Comments

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.

1 Comment

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.
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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.