Given is the following String:
String s = "Qba'g sbetrg gb qevax lbhe Binygvar";
My desired output is:
Don't forget to drink your ovaltine
My code:
public static String decrpyt(String s){
String ans = "";
for (int i = 0; i<s.length(); i++) {
int x = s.charAt(i);
if (x <= 105) {
char c = ((char) (x + 13));
ans += c;
} else {
char c = ((char) (x - 13));
ans += c;
}
}
return ans;
}
It returns:
don4t-forget-to-drink-_our-ovaltine
What do I need to do to get the desired output?
You are changing all characters, not just the letters, as has been mentioned in a comment. Carefully consider the range of characters that are letters that you need to modify. Declare x to be a char so that you don't have to consider the actual numeric ranges.
A-M => N-Z Action: += 13
a-m => n-z Action: += 13
N-Z => A-M Action: -= 13
n-z => a-m Action: -= 13
(all others) Action: No change
This can be expressed in 3 cases:
if ((x >= 'a' && x <= 'm') || (x >= 'A' && x <= 'M')) {
// Add 13
} else if ((x >= 'n' && x <= 'z') || (x >= 'N' && x <= 'Z')){
// Subtract 13
} else {
// Use as is
}
You can add in an idea of exception characters. Basically any character in the exception list won't be translated. This would look something like:
public static String decrpyt(String s){
String ans = "";
for(int i = 0; i<s.length(); i++){
// Could use a list or whatever, just giving you the idea here
String characterAt = s.charAt(i).toString();
if(characterAt.equals("'") || characterAt.equals(" "))
{
continue;
}
int x = s.charAt(i);
if(x <= 105) {
char c = ((char) (x + 13));
ans += c;
}
else{
char c = ((char) (x - 13));
ans += c;
}
}
return ans;
}
Related
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 4 years ago.
I have been trying to find my own way of making a Rot13 algorithm in java, but when I try a phrase, it gives me this error:
java.lang.ArrayIndexOutOfBoundsException: 41
So this is my code:
:Update with whole names,
public class Rot13
{
char[] translated;
String abc = "abcdefghijklmnopjrstuvwxyzabcdefghijklmnopqrstuvwxyz";
public String ROT13(String input){
input = input.toLowerCase();
char[] sentence = input.toCharArray();
char[] ABC = abc.toCharArray();
int x = input.length();
int y = 0;
char[] translated = new char[x];
for(int i = 0; i<x;i++){
int z = 0;
if(sentence[i] == ' '){
translated[i] = ' ';
}
else {
while(y==0){
if (sentence[i] == ABC[z]){
y =1;
}
else{
z += 1;
}
}
translated[i] = ABC[z+12];
}
}
String rot13string = new String(translated);
return rot13string;
}
}
: Update 2
I just tested this version again and it translates the it. But in the wrong way, for example, "Hello" becomes "tmmmm" The first letter seems to be right but then the next ones are always 'm'.
Update 3: Thanks for your answers guys, here is my final code, I just duplicated my alphabet a "few" times. :
public class ROT13
{
char[] translated;
String ab = "abcdefghijklmnopjrstuvwxyzabcdefghijklmnopqrstuvwxyz";
String abc = String.format("%0" + 1000 + "d", 0).replace("0",ab);
public String ROT13(String input){
input = input.toLowerCase();
char[] sentence = input.toCharArray();
char[] ABC = abc.toCharArray();
int length = input.length();
char[] translated = new char[length];
for(int i = 0; i<length;i++){
int y = 0;
int h = 0;
if(sentence[i] == ' '){
translated[i] = ' ';
}
else {
while(y==0){
int z = 0;
if (sentence[i] == ABC[h]){
y +=1;
}
else{
z += 1;
h += 1;
}
}
translated[i] = ABC[h+13];
}
}
String rot13string = new String(translated);
return rot13string;
}
}
Currently, think about what happens when there is a 'z' in the sentence. Then, you make t[i] = ABC[z (26) + 12] //which is larger than ABC's length.
Personally, I would do rot13 like so:
public char rot13(char s){
if (c >= 'a' && c <= 'm') return c += 13;
else if (c >= 'A' && c <= 'M') return c += 13;
else if (c >= 'n' && c <= 'z') return c -= 13;
else if (c >= 'N' && c <= 'Z') return c -= 13;
else return c;
}
How can I add 0 in front of every single digit number? I mean 1 to 01 etc.
I have tried to add ifs like
if(c >='A' && c<= 'I')
str = "0"+str;
but it just adds 0 in front of everything like abcd converts to 00001234 not 01020304.
This is my code.
String A[] = new String[size];
for (int i = 0; i < size; i++) {
A[i] = jList1.getModel().getElementAt(i);
String[] Text = A[i].split("");
String s = jList1.getModel().getElementAt(i);
String str = ("");
for (int z = 0; z < Text.length; z++) {
for (int y = 0; y < Text[z].length(); y = y + 1) {
char c = s.charAt(z);
if (c >= 'A' && c <= 'Z') {
str += c - 'A' + 1;
} else if (c >= 'a' && c <= 'z') {
str += c - 'a' + 1;
} else {
str += c;
}
}
str = str + "";
}
}
This Worked for me
public String addZero(int number)
{
return number<=9?"0"+number:String.valueOf(number);
}``
One way to do this would be to use a StringJoiner with Java 8:
String s = "abcdABCD";
s = s.chars()
.mapToObj(i -> Integer.toString((i >= 'a' && i <= 'z' ? i - 'a' : i - 'A') + 1))
.collect(Collectors.joining("0", "0", "")));
System.out.println(s);
>> 0102030401020304
String str = "abcd-zzz-AAA";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char ch = str.toLowerCase().charAt(i);
if (ch >= 'a' && ch <= 'z') {
sb.append('0');
sb.append(ch - 'a' + 1);
} else {
sb.append(ch);
}
}
Result: abcd-zzz-AAA -> 01020304-026026026-010101
Final fix :-)
use String#chars to get a stream of its characters, then for each one do the manipulation you want.
public class Example {
public static void main(String[] args) {
String s = "aBcd1xYz";
s.chars().forEach(c -> {
if (c >= 'a' && c <= 'z')
System.out.print("0" + (c - 'a' + 1));
else if (c >= 'A' && c <= 'Z')
System.out.print("0" + (c - 'A' + 1));
else
System.out.print(c);
});
}
}
Ouput:
0102030449024025026
You can add zero in front of single digit number using String.format.
System.out.println(String.format("%02d",1));
System.out.println(String.format("%02d",999));
The first line will print 01, second line prints 999 no zero padding on the left.
Padding zero with length of 2 and d represents integer.
I hope this helps.
import java.lang.Integer;
import java.util.Arrays;
public class Decimal {
//initialize intance variables
private int decimal;
private String hex;
public static void toHex(String s) {
int decimal = Integer.parseInt(s); //converts the s string into an int for binary conversion.
String hex = null;
int[] binNum = new int[16];
int[] binNumNibble = new int[4]; //A nibble is four bits.
int nibbleTot = 0;
char hexDig = '\0';
char[] cvtdHex = new char[4];
StringBuffer result = new StringBuffer();
for(int a = 32768; a == 1; a /= 2) { //32768 is the value of the largest bit.
int b = 0;//Will top at 15 at the end of the for loop. 15 references the last spot in the binNum array.
if(decimal > a) {
decimal -= a;
binNum[b++] = 1;//Arrays have a default value of zero to all elements. This provides a parsed binary number.
}
}
for(int a = 0; a == 15; a += 3) {
//Copies pieces of the binary number to the binNumNibble array. .arraycopy is used in java 1.5 and lower.
//Arrays.copyOfRange is used in java 1.5 and higher.
System.arraycopy(binNum, a, binNumNibble, 0, 4);
for(int b = 8; b == 1; a += 3) {
int c = 0;
nibbleTot += binNumNibble[c++];
//Converts the single hex value into a hex digit.
if(nibbleTot >= 1 && nibbleTot <= 9) {
hexDig += nibbleTot;
} else if(nibbleTot == 10) {
hexDig = 'A';
} else if(nibbleTot == 11) {
hexDig = 'B';
} else if(nibbleTot == 12) {
hexDig = 'C';
} else if(nibbleTot == 13) {
hexDig = 'D';
} else if(nibbleTot == 14) {
hexDig = 'E';
} else if(nibbleTot == 15) {
hexDig = 'F';
}
cvtdHex[c++] = hexDig;
}
}
//return hex = new String(cvtdHex);
hex = new String(cvtdHex);
System.out.print("Hex: " + hex);
}
}
I can't seem to figure out why variable hex is returned as a blank variable. I've been using System.out.print(); in each for loop and none of them are used, giving me the impression that the for loops are being skipped entirely, but I don't understand why and I'm on a time limit.
Any help is much appreciated, but please don't just paste code. I need to understand this for my computer science class!
yes, your for loops ARE being skipped, since the second part of the for statement is not the break condition but the condition that has to be fullfilled for the loop to run.
So it is NOT
for(a = 0; a == 15; a += 3)
but
for(a = 0; a <= 15; a += 3)
and so on...
The for loops wont execute because of the double ==
How about
String.format("%h", 256)
(EDITED)
My problem statement: write a method that will encode the String passed to the method by adding 13 letters to each character in the String. If the letter after adding 13 exceeds 'z' then "wrap around" the alphabet. Then return the encoded String.
encodeString("hello") → "uryyb"
encodeString("pie") → "cvr"
encodeString("book") → "obbx"
this is what I have so far :
public static String encodeString (String input) {
String output;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c >= 'a' && c <= 'm')
c += 13;
else if (c >= 'n' && c <= 'z')
c -= 13;
output= (" " + (c));
}
return output;
}
now I know that I have to create a counter so that the method will continue to loop until it reaches the length of the string passed...and I know that if the charAt(index) is less than the character 'n' that I add 13 and if it is greater then I subtract 13. when I put it all together though I just get so confused and just get a bunch of compiling errors like Type mismatch: cannot convert from int to String.
note straightforward explanations/answers would be much appreciated...
***so now my problem is that it keeps telling me my output variable may not have been initialized
This code is not the most performatic but works good with Upper and Lower characters.
hElLo → uRyYb
pIe → cVr
bOoK → oBbX
private static String encodeString(String string) {
char[] ret = new char[string.length()];
for (int i = 0; i < string.length(); i++) {
ret[i] = rot13(string.charAt(i));
}
return String.valueOf(ret);
}
public static char rot13(char c) {
if (Character.isLetter(c)) {
if (Character.compare(Character.toLowerCase(c), 'a') >= 0
&& Character.compare(Character.toLowerCase(c), 'm') <= 0)
return c += 13;
else
return c -= 13;
}
return c;
}
You have to initialize your output variable as an empty String. Furthermore you are always replacing the contents of the output variable with the last char you've just encoded. So you have to add every char to the output with += instead of =.
So here is the fixed solution:
public static String encodeString(String input) {
String output = ""; // initialize as empty String
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c >= 'a' && c <= 'm') {
c += 13;
} else if (c >= 'n' && c <= 'z') {
c -= 13;
}
output += " " + c; // add all chars to the String instead of replacing the whole String with "="!
}
return output;
}
I beautified your code a bit, so everybody can see what it really does.
Use an IDE!
I want to check if every character in a string is the inverse of the other character in the second. By inverse I mean uppercase and lowercase.
For example these strings:
Ahh7h
aHH7H
The result will be true
I wrote this code but the result is always false. Why?
public boolean checkString(String serverString, String clientString) {
if (serverString.length() != clientString.length())
return false;
else
for (int i = 0; i < clientString.length(); i++) {
if ((clientString.charAt(i) >= '0' && clientString.charAt(i) <= '9')
&& (clientString.charAt(i) != serverString.charAt(i)))
return false;
else if (clientString.charAt(i) >= 'A'
&& clientString.charAt(i) <= 'Z') {
if ((int) clientString.charAt(i) != ((int) serverString
.charAt(i) + 32))
return false;
} else if (clientString.charAt(i) >= 'a'
&& clientString.charAt(i) <= 'z') {
if ((int) clientString.charAt(i) != ((int) serverString
.charAt(i) - 32))
return false;
}
}
return true;
}
You could "invert" one string using: How can I invert the case of a String in Java? and then use Strings .equals method to compare them.
Method from How can I invert the case of a String in Java? included for completeness:
public static String reverseCase(String text)
{
char[] chars = text.toCharArray();
for (int i = 0; i < chars.length; i++)
{
char c = chars[i];
if (Character.isUpperCase(c))
{
chars[i] = Character.toLowerCase(c);
}
else if (Character.isLowerCase(c))
{
chars[i] = Character.toUpperCase(c);
}
}
return new String(chars);
}
You've switched the + 32 and the - 32.
By the way, it's a lot easier to use methods like:
Character.isDigit
Character.isLowerCase
Character.isUpperCase
Character.toLowerCase
Character.toUpperCase