It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

HELP please!!?! How to make this work?

HELP please!!?! How to make this work?

Postby amityville9889 on Mon Apr 23, 2007 12:49 am

I'm trying to write a program that converts from binary to decimal and vice versa. Here's what I have so far:


import java.io.*;
public class binMain {

/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub

BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));



double input = 0;

char choice = ' ';





while(choice!='Q' && choice!= 'q')
{ //start of while
System.out.println("He110!! Ready make a conversion?!?");
System.out.println("Make a selection!!!!!!!");
System.out.println("a - Input the number you want to convert.");
System.out.println("b - Convert from Binary to Decimal");
System.out.println("c - Convert from Decimal to Binary");
System.out.println("q - quit!!!!!!!no wai--");


choice = keyboard.readLine().charAt(0);

switch(choice)
{//start of switch
case'A':
case'a':
{

}
break;

case'B':
case'b':
{

}
break;

case'C':
case'c':
{

}
break;

case'Q':
case'q':
{System.out.println("Thanks converting!! ;-) ");}
break;

}//end of switch
}//end of while
}



---------------------------------------------------------------
i know i have to use an array and i'm lost on the math. Any help would be greatly appreciated. Thanks
amityville9889
 
Posts: 0
Joined: Mon Apr 23, 2007 12:41 am

OK

Postby ggomez on Mon Apr 23, 2007 5:02 am

mmm

forums are for doubts not to get homeworks

Code: Select all
public class Convertion {

    public Convertion() {
    }
   
    public int toDec(String x)
    {
       int b=1;
       int s=0;
       int e=0;
       for(int a=x.length()-1;a>=0;a--)
       {
          e=Integer.valueOf(String.valueOf(x.charAt(a)));
          s=s+(e*b);
          b=b*2;
       }
       return s;
    }
   
    public String toBin(int x)
    {
       return Integer.toBinaryString(x);
    }
   
    public static void main(String arg[])
    {
       Convertion c=new Convertion();
       System.out.println(c.toDec("100"));
       
    }
   
}
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡


Who is online

Users browsing this forum: No registered users and 0 guests