Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #3197
| From | "Lew" <lew@THRWHITE.remove-dii-this> |
|---|---|
| Subject | Re: Need help to convert |
| Message-ID | <14-dnfLeruzT4V3anZ2dnUVZ_tKinZ2d@comcast.com> (permalink) |
| Newsgroups | comp.lang.java.gui |
| References | <6c7359f1-70b1-4857-92c1-539f5aa40f0e@71g2000hse.googlegroups.com |
| Date | 2011-04-27 15:43 +0000 |
| Organization | TDS.net |
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
Back to comp.lang.java.gui | Previous | Next — Next in thread | Find similar | Unroll thread
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
csiph-web