Reputation: 197
I need to convert a set of chars into a string. The problem is, I don't know how to do it without an array (it's forbidden to use them, because we didn't see the this subject yet).
So the method starts with asking the user to type in a word (String type). In this case I use the word "programma" as an example. The first step is too count +4 to every character of that word. In this example it must change from "programma" --> "tvskveqqe".
I split up the input string "programma", into seperate chars, and added +4 in the alfabet. Afterwards I made sure if the letters "wxyz" are used, that they are converted to w --> a, x -->b, y --> c and z --> d.
But now I'm stuck at the part, where I need to put the chars 't''v''s''k''v''e''q''q''e' into a string "tvskveqqe", and use that as return statement.
Thanks!
public char coderen() {
String str; //input string
char c, e = ' ';
int a = 4, b, d;
System.out.println("Geef een woord in: ");
str = Input.readString(); //input
for (int i = 0; i < str.length(); i++) { // Splits up the string into separate chars
b = (int) str.charAt(i) + a; // +4 in ASCII
c = (char) b;
if (c >= 'e' && c <= 'z') {
e = c;
System.out.println(e);
}
else if (c >= '{' && c <= '~') { // converts 'w''x''y''z' into 'a''b''c''d'
d = (int)c - 26;
e = (char) d;
System.out.println(e);
}
else {
System.out.println("fout!");
}
}
return e;
}
Upvotes: 2
Views: 1568
Reputation: 1454
You have various ways to do it.
There's String.concat()
for example
String str = "";
str.concat(new String(myChar));
it's pratically the same as doing
str += mychar;
But generally its a bad practice because Strings are immutable, they consume memory and here you're creating a lot of them.
The best you can use is a StringBuilder:
StringBuilder builder = new StringBuilder();
builder.append(myChar1);
builder.append(myChar2);
...
builder.toString();
As a side note I would say that if you're using a String
you are actually using a char array. String
is a wrapper class for a char[]
structure:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
...
Upvotes: 1
Reputation: 14716
It's a bit strange that you should not use arrays as in the end a string is always represented as an array of characters, so you could argue that any approach that you apply is "using an array"...
Having said that, you could try different things:
Using a StringBuilder:
StringBuilder sb = new StringBuilder();
for .... { sb.appendChar(c); }
Using String concatenation:
String result = "";
for .... { result = result + c; }
Using String replace: (However this is not based on index but on matching characters):
for (char c = 'a'; c <= 'z'; c++) {
result = result.Replace(c, c + 4);
}
Upvotes: 0
Reputation: 39457
Try this. You'll get the idea. It does not use any arrays.
public static void main(String[] args) throws Exception {
char ch = 'a';
String s = ch + "";
for (int i=1; i<=10; i++){
s += (char)(ch + i);
}
System.out.println(s);
}
But now I'm stuck at the part, where I need to put the chars 't', 'v', 's', 'k', 'v', 'e', 'q', 'q', 'e' into a string "tvskveqqe", and use that as return statement.
You can use an ArrayList
also (provided you are disallowed to use an array).
Then loop through it and do what I did above.
Upvotes: 0