Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3197 > unrolled thread

Re: Need help to convert

Started by"Lew" <lew@THRWHITE.remove-dii-this>
First post2011-04-27 15:43 +0000
Last post2011-04-27 15:43 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.gui

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Need help to convert "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
    Re: Need help to convert "tar" <tar@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000

#3197 — Re: Need help to convert

From"Lew" <lew@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
SubjectRe: Need help to convert
Message-ID<14-dnfLeruzT4V3anZ2dnUVZ_tKinZ2d@comcast.com>
  To: comp.lang.java.gui
Katak wrote:
> I wrote these codes below in Java and theres some problems passing the
> string to do the conversion. Can anyone help me out? The c++ codes are
> on the first post above. Thanks.
> 
> import java.util.Scanner;
> public class BinaryToDecimal {
> 
> 
> 	public static void main(String[] args) {
> 
> 		   String bin;
> 		       result += (( bin[i] - 48)*(cubed));

Review the Javadocs for String, the type of 'bin', at
<http://java.sun.com/javase/6/docs/api/java/lang/String.html>

Strings are not arrays, and cannot use the square-bracket notation ([]) to 
represent parts of a String.  A String is an object that must be accessed 
through its defined methods.

It seems that you are looking for String#charAt()
<http://java.sun.com/javase/6/docs/api/java/lang/String.html#charAt(int)>
but be aware that character encoding can differ, and '48' might not be the 
magic number you're looking for.  Also, what if the /i/th character is not a 
digit?

P.S., please use less aggressive indentation for Usenet posts.  Up to four 
spaces (not TAB characters) per indent level is fine.  You can cut back on the 
blank lines, too.  Easier-to-read listings are easier to answer.

-- 
Lew

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#3217

From"tar" <tar@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
Message-ID<ymitzjv33wi.fsf@blackcat.isi.edu>
In reply to#3197
  To: comp.lang.java.gui
Lew <lew@lewscanon.com> writes:

> Katak wrote:
> > public class BinaryToDecimal {
> > 	public static void main(String[] args) {
> > 		   String bin;
> > 		       result += (( bin[i] - 48)*(cubed));
...
> It seems that you are looking for String#charAt()
> <http://java.sun.com/javase/6/docs/api/java/lang/String.html#charAt(int)>
> but be aware that character encoding can differ, and '48' might not be
> the magic number you're looking for.  Also, what if the /i/th character
> is not a digit?

And to help out with those problems, it would be wise to look at the
Character class, which has some nice useful methods like "isDigit" and
"digit" which will handle all of those particular issues for you.

As a general rule, there are a gazillion classes that come standard with
Java compilers, and you can save a lot of time and aggravation by using
them.  Porting your C++ algorithm can be a useful initial exercise for
gaining some familiarity with the language, but you will eventually want
to learn to write Java code, rather than C++ code in Java.

So, looking at some of the other built-in classes, you might discover
that the entire problem of BinaryToDecimal conversion is already handled
for you by the language itself:

  Integer.parseInt(inputString, 2).toString();



-- 
Thomas A. Russ,  USC/Information Sciences Institute

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web