Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 2 <= base <= 16 converter.
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MysticVegeta




PostPosted: Sun Feb 26, 2006 12:44 pm   Post subject: 2 <= base <= 16 converter.

My apologies for being a noob at math. I dont know how to directly convert bases so I go from currentBase to 10 to desiredBase. The code is straight for ward.

The input file is attached. Its format is:

Number numsBase theBaseYouWantIn

Just changing the variable numberOfInputs would make it read how many linesyou want it to.

code:
import java.io.*;
import java.util.*;
public class Problem5
{
    public static void main (String [] Args)
    {
        try {
        BufferedReader in = new BufferedReader (new FileReader ("DATA51.txt"));
        BufferedWriter out = new BufferedWriter (new FileWriter ("OUT51.txt"));

                int numberOfInputs = 5;
       
                String [] line;
                String [] Tenplus = new String [6];
                for (int i = 10; i < 16; i++) Tenplus[i-10] = i+"";
                String alphas = "ABCDEF";
               
                for (int i = 0; i < numberOfInputs; i++) {
                        if (i!= 0) out.newLine();
                        line = in.readLine().split(" ");
                        String currentNum = line[0];
                        int currentBase = Integer.parseInt(line[1]);
                        int desiredBase = Integer.parseInt(line[2]);
                        int base10Total = 0;
                        int po = currentNum.length()-1;
                       
                        //NUMBER IS 0
                        if (currentNum.charAt(0) == '0' && currentNum.length() == 1) {
                                out.write ("0");
                                continue;
                        }
                       
                        //CONVERTING INTO BASE 10
                        for (int j = 0; j < currentNum.length(); j++) {
                                int index = alphas.indexOf(currentNum.charAt(j));
                                if (index >= 0) {
                                        base10Total += Integer.parseInt(Tenplus[index]) * Math.pow(currentBase, po);
                                }
                                else base10Total += (currentNum.charAt(j) - '0') * Math.pow (currentBase, po);
                                po--;
                        }
                       
                        //CONVERTING INTO DESIRED BASE
                        String ans = "", reverseAns = "";
                        int rem = 0;
                        while (true) {
                                rem = base10Total % desiredBase;
                                base10Total = (int) Math.round(base10Total/desiredBase);
                                if (base10Total == 0 && rem == 0) break;
                                if (rem >= 10) ans += alphas.charAt(rem-10);
                                else ans += rem;
                        }
                       
                        //REVERSING THE STRING OF MODS
                        for (int j = ans.length()-1; j >= 0; j--) reverseAns += ans.charAt(j);
                       
                        //FINAL ANSWER
                        out.write (reverseAns);
                }

                in.close();
                out.close();
                }
        catch (IOException e){
                }
    }
}



PS: I know tis a noobie submission, but its my first sub. in java =\



DATA51.txt
 Description:
Put it in the same folder as the program. =)

Download
 Filename:  DATA51.txt
 Filesize:  55 Bytes
 Downloaded:  258 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
mckhatri1




PostPosted: Wed Jul 05, 2006 8:22 am   Post subject: Easier Way

Mind if I help simplify:

code:
import java.io.*;
public class Problem5
{
    public static void main (String [] Args) throws Exception
    {
        BufferedReader in = new BufferedReader (new FileReader ("DATA51.txt"));
        BufferedWriter out = new BufferedWriter (new FileWriter ("OUT51.txt"));
       
      for (int i = 0; i < 5; i++) {
      String[] stuff=in.readLine().split(" ");
      int origBase=Integer.parseInt(stuff[1]);
      int newBase=Integer.parseInt(stuff[2]);

      int numInDec=Integer.parseInt(stuff[0],origBase);
      out.println(Integer.toString(numInDec,newBase).toUpperCase());} 

    out.close();
    }

}
Krabjuice




PostPosted: Wed Jul 05, 2006 10:21 am   Post subject: (No subject)

How about this? Make sure you download both classes.


Hex.java
 Description:
Hex-change Library Class

Download
 Filename:  Hex.java
 Filesize:  3.64 KB
 Downloaded:  261 Time(s)


BaseChange.java
 Description:
Usuable Class

Download
 Filename:  BaseChange.java
 Filesize:  7.15 KB
 Downloaded:  378 Time(s)

wtd




PostPosted: Wed Jul 05, 2006 11:52 am   Post subject: (No subject)

Your indentation is all wrong. You're putting starting braces on their own lines. You have spaces between open parens and class names as well as in method calls in places. You have spaces between array names and subscripts. You sometimes use cryptic variable names like "po". You lack proper spacing around some operators.

Now, non-style issues.

code:
         if (currentNum.charAt(0) == '0' && currentNum.length() == 1) {
            out.write ("0");
            continue;
         }


If the string is empty, charAt(0) will throw an exception. You don't catch this, so the end result is that your program will die horribly. If we reverse the two conditions, then short-circuiting saves your bacon.

code:
         if (currentNum.length() == 1 && currentNum.charAt(0) == '0') {
            out.write ("0");
            continue;
         }


Oh, and use methods. This much functionality should not be directly in main. Smile
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: