One minor problem need help
Hi all, I have one small problem with this code that I am trying to get the program to do and I have racked my brain. Run the program and look at the last two outputs JavaClass is: J+a+v+a+C+l+a+s+s+and
ClassJava is C+l+a+s+s+J+a+v+a+ .Both of these outputs have a + after the last letter and I don't want that plus sign there. What adjustment do I need to make to my code to get rid of that last plus after the "s" in JavaClass and also get rid of the last plus after the "a" in ClassJava for a total of 8 plus signs in between the letters instead of 9?
Here is my code:
import java.lang.String;
import javax.swing.JOptionPane;
public class Garuvian {
public static String createGaruvian(int num) {
String Garuvian = "";
for (int b = 0; b < num; b++) {
Garuvian += "1 + ";
}
Garuvian += "0";
return Garuvian;
}
public static String createGaruvian(String str) {
String Garuvian = "";
String Character = "";
int big = str.length();
for (int c = 0; c < big; c++) {
Character = str.substring(c, c + 1);
if (c > 1) {
if (c == (big - 1)) {
Garuvian += Character;
} else {
Garuvian += Character + "+";
}
} else {
Garuvian += Character + "+";
}
}
return Garuvian;
}
public static String add(int num1, int num2) {
String Garuvian = "";
Garuvian = createGaruvian(num1) + " + \n" + createGaruvian(num2);
return Garuvian;
}
public static String subtract(int num1, int num2) {
String Garuvian = "";
Garuvian = createGaruvian(num1) + " - \n(" + createGaruvian(num2) + ")";
return Garuvian;
}
public static String multiply(int num1, int num2) {
String Garuvian = "";
for (int d = 0; d < num2; d++) {
if (d!=num2-1){
Garuvian += createGaruvian(num1) + " + \n";
}
else {
Garuvian += createGaruvian(num1) + "\n";
}
}
return Garuvian;
}
public static String remainder(int divider, int divisor) {
String Garuvian = "";
Garuvian += createGaruvian(divider);
while (divider > divisor) {
Garuvian += " - \n (" + createGaruvian(divisor) + ")";
divider -= divisor;
}
return Garuvian;
}
public static String concatString(String string1, String string2) {
int length1, length2;
String Garuvian = "";
String Garuvian1 = "";
String Garuvian2 = "";
String Character = "";
length1 = string1.length();
length2 = string2.length();
for (int f = 0; f < length1; f++) {
Character = string1.substring(f, f + 1);
Garuvian1 += createGaruvian(Character);
}
for (int g = 0; g < length2; g++) {
Character = string2.substring(g, g + 1);
Garuvian2 += createGaruvian(Character);
}
Garuvian = Garuvian1 + Garuvian2;
return Garuvian;
}
public static void main(String[] args) {
int sum = 0;
int k = 3, j = 5, t = 6;
String str1 = "Java", str2 = "Class";
String output = "";
output += "\nThe number " + k + " in Garuvian is:\n" + createGaruvian(k);
output += "\nThe string \"" + str1 + "\" in Garuvian is:\n" +
createGaruvian(str1);
output += "\nAdd " + j + " and " + k + ":\n" + add(j, k);
output += "\nAdd " + k + " and " + j + ":\n" + add(k, j);
output += "\nSubtract " + j + " minus " + k + ":\n" + subtract(j, k);
output += "\nSubtract " + t + " minus " + j + ":\n" + subtract(t, j);
output += "\nMultiply " + j + " and " + k + ":\n" + multiply(j, k);
output += "\nMultiply " + k + " and " + j + ":\n" + multiply(k, j);
output += "\nRemainder of " + t + " divided by " + k + ":\n" +
remainder(t, k);
output += "\nRemainder of " + j + " divided by " + k + ":\n" +
remainder(j, k);
output += "\n" + str1 + str2 + " is:\n" + concatString(str1, str2);
output += "\n" + str2 + str1 + " is:\n" + concatString(str2, str1);
JOptionPane.showMessageDialog(null, output);
}
}

