d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Easy Java Question > Might Take A Bit To Read Though?
Add Reply New Topic New Poll
Member
Posts: 863
Joined: Aug 3 2008
Gold: 1,178.00
May 10 2013 10:54am
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();
   }
}
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
May 10 2013 12:37pm
Shouldn't "cut" be ccb?

c = 3 = c
u = 21 = 2+1 = c
t = 20 = 2+0 = b

I condensed the code a bit, but not sure if it's what you're trying to do

Code
 public String digitalRoot( String input ) {
   String string = "";
   for ( int i = 0; i < input.length( ); i++ ) {
     string += (int) (input.charAt( i ) - 96) % 9;
   }
   int number = Integer.parseInt( string );
   List<Integer> digits = new ArrayList<Integer>( );
   String result = "";
   while ( number > 0 ) {
     digits.add( number % 10 );
     number /= 10;
   }
   ListIterator<Integer> li = digits.listIterator( digits.size( ) );
   while ( li.hasPrevious( ) ) {
     result += (char) (li.previous( ) + 96);
   }
   return result;
 }
Member
Posts: 863
Joined: Aug 3 2008
Gold: 1,178.00
May 10 2013 03:33pm
i guess that was a bad example. it's more like c+u and the t is dropped. it works better with evens i guess. cute: c+u, t+e. thanks for that though, only thing i don't really get is the iterator but i'll figure it out i think
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll