I just started learning java, first programming language too, and I've been messing around with things to get a better understanding of it.
I made a program that takes 1 token of input and converts it to a new string by taking the digital roots of each letter's numerical value and converting them back to those number's respective letters.
ex: cut -> c(3) + u(21)
c(3) + u(3)
6
t(20)
2 + 0
2
62 = fb
It works fine. the thing is, i'm pretty sure the way i approached it is some kind of overcomplicated, and I want to start by learning more efficient ways to do things. are there any classes i could use to replace this process that would shorten it / is there anything i did that's redundant?
Code
import javax.swing.JOptionPane;
public class digitalRoot {
String output="";
String output2="";
public digitalRoot(){
String userInput = JOptionPane.showInputDialog("Enter string").toLowerCase();
for(int i=0; i<userInput.length(); i+=2){
if (userInput.length()%2!=0 && i==userInput.length()-1){
if (Integer.toString(userInput.charAt(i)-96).length()>1){
output+= dR(Integer.toString(userInput.charAt(i)-96).charAt(0)-48, Integer.toString(userInput.charAt(i)-96).charAt(1)-48);
break;
}else{
output+= (Integer.toString(userInput.charAt(i)-96).charAt(0)-48);
}
}else if(userInput.length()%2!=0){
output+= dR((int)userInput.charAt(i)-96,(int)userInput.charAt(i+1)-96);
}else if(userInput.length()%2==0){
output+= dR((int)userInput.charAt(i)-96,(int)userInput.charAt(i+1)-96);
}
}
for(int i=0; i<output.length(); i++){
int x = (int)(output.charAt(i)+48);
output2+=(char)x;
}
System.out.println("Input: " + userInput + "\n");
System.out.println("Result: " + output2);
}
public String dR(int one, int two){
int aa=one;
int bb=two;
while(Integer.toString(aa).length()>1){
aa = Integer.toString(aa).charAt(0)-48 + Integer.toString(aa).charAt(1)-48;
}
while(Integer.toString(bb).length()>1){
bb = Integer.toString(bb).charAt(0)-48 + Integer.toString(bb).charAt(1)-48;
}
int three = aa + bb;
while(Integer.toString(three).length()>1){
three = Integer.toString(three).charAt(0)-48 + Integer.toString(three).charAt(1)-48;
}
return Integer.toString(three);
}
}
class digitalRootT {
public static void main(String[] args) {
digitalRoot lol = new digitalRoot();
}
}