Re: Need help to convert my C++ codes to Java with GUI
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Sat, 23 Feb 2008 15:04:29 -0500
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
.
- Follow-Ups:
- Re: Need help to convert my C++ codes to Java with GUI
- From: Thomas A. Russ
- Re: Need help to convert my C++ codes to Java with GUI
- From: Katak
- Re: Need help to convert my C++ codes to Java with GUI
- References:
- Need help to convert my C++ codes to Java with GUI
- From: vampireex
- Re: Need help to convert my C++ codes to Java with GUI
- From: Roedy Green
- Re: Need help to convert my C++ codes to Java with GUI
- From: Katak
- Re: Need help to convert my C++ codes to Java with GUI
- From: Katak
- Need help to convert my C++ codes to Java with GUI
- Prev by Date: Re: Need help to convert my C++ codes to Java with GUI
- Next by Date: Re: Need help to convert my C++ codes to Java with GUI
- Previous by thread: Re: Need help to convert my C++ codes to Java with GUI
- Next by thread: Re: Need help to convert my C++ codes to Java with GUI
- Index(es):
Relevant Pages
|