Solved

converts a string binary number to a decimal

Posted on 2002-03-20
3
196 Views
Last Modified: 2010-05-18
import java.io.*;
import java.math.*;

class yk {
 
  static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
  static PrintWriter screen = new PrintWriter(System.out, true);
 
  public static void main(String[] args) throws IOException {
     
      String str;
      char[] charArray;
      String[] subs;
      int[] q;
      int count = 0;
      char separator = ' ';  
      int index = 0;
      index = 0;
      int endIndex = 0;
      int sum = 0;
      int weight = 1;
     
      screen.println("Try System");
      screen.println("Enter a sentences");
      screen.println();
      str = keyboard.readLine();
      //str = str.toLowerCase();        
      char[] array = new char [str.length()];
      StringBuffer buf = new StringBuffer();
      int dis = 1 << 7;
     
      for ( int i = 0; i < str.length(); i++ ) {

          if (str.length() % 2 == 1) {
             for (int sh1=1; sh1<=8; sh1++)
                buf.append('0');
          }
         
          array[i] = str.charAt(i);
         
          for (int sh = 1; sh <= 8; sh++) {
             
              buf.append(( array[i] & dis ) == 0 ? '0' : '1');
              array[i] <<= 1;
          }

          i++;

          if (i < str.length()) {
            array[i] = str.charAt(i);
             
            for (int sh2 = 1; sh2 <= 8; sh2++) {
               
              buf.append(( array[i] & dis ) == 0 ? '0' : '1');
              array[i] <<= 1;
            }
          }

         buf.append(' '); // added for debug
      }                                                                      
      String s = buf.toString();                                              
      System.out.println(s);
     
      do
      {
          ++count;
          ++index;
          index = s.indexOf( separator, index );
      }
      while ( index != -1);
     
      String[] subStr = new String[count];
      index = 0;

      for ( int i = 0; i < count; i++ )
      {
          endIndex = s.indexOf( separator, index );
         
          if ( endIndex == -1 )
              subStr[i] = s.substring ( index );
          else
              subStr[i] = s.substring( index, endIndex );
         
          index = endIndex + 1;
      }
     
      //DISPLAY THE SUBSTRINGS
      for ( int i = 0; i < subStr.length; i++ )
          System.out.println(subStr[i]);
      //System.out.print(" " + Integer.parseInt(subStr[i], 2));
  }
}

At the final results, i want to convert each binary string to become a decimal but i getting some error message with this code: System.out.print(" " + Integer.parseInt(subStr[i], 2)); Then each decimal with do some math calculation. For example math.pow(decimal, x) and mod by a value. Can some one help me to solve this probelm!!  
0
Comment
Question by:dxding
3 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 300 total points
ID: 6882671
You have to change these 2 things:
the for loop should be:
for ( int i = 0; i < str.length();)

the buf.append(' '); line must be within the if (i < str.length()) block
if (i < str.length()) {
  array[i] = str.charAt(i);
  for (int sh2 = 1; sh2 <= 8; sh2++) {
    buf.append(( array[i] & dis ) == 0 ? '0' : '1');
    array[i] <<= 1;
  }
  buf.append(' '); // added for debug
}
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6882736
You just need to change the following:

     //DISPLAY THE SUBSTRINGS
     for ( int i = 0; i < subStr.length; i++ )
         System.out.println(subStr[i]);
     //System.out.print(" " + Integer.parseInt(subStr[i], 2));

to

     //DISPLAY THE SUBSTRINGS
     for ( int i = 0; i < subStr.length; i++ ) {
        // add the following two lines
        if (subStr[i].equals(""))
          break;

         System.out.println(subStr[i]);
     //System.out.print(" " + Integer.parseInt(subStr[i], 2));
 }

And that should work.
0
 

Author Comment

by:dxding
ID: 6882949
Thanx for help :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 75
Unexpected HttpURLConnection connection behavior 2 84
eclipse formatting 6 70
Java Server Faces parameter pass? 6 37
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now