Java Problem

Apr 6, 2011
769
101
0
ගැහැණියක උදව් ඉල්ලූ තැන පිරිමියාගේ සංවේදීකම මොනවට පැහැදිලි වෙයි.....
 
Apr 6, 2011
769
101
0
how about the other way?

habey itng help karata sanwedi ne godak pirimi gahunu tarama pirimin hith gal wage :P
අවංක සංවේදීකම සහ වලත්ත සංවේදීකම කියල දෙකක් තියනවා සේයා...;)
 

pissumalli

Member
Apr 16, 2011
12
2
0
Macha... check this out. Im still in first year at UoM. Didnt have time to double check for other base values. I hope this is correct. Anyways check several times.
/**
*
* @param base the base digit e.g., 2
* @param value the value of @base e.g., 10111
* @return the decimal value e.g. 23 (16+0+4+2+1)
*/
public static int toDecimal(int base, int value){
int deci =0 ;
char[] valueArr= (""+value).toCharArray();
for(int i=0; i<valueArr.length; i++){
int digit = Integer.valueOf(""+ valueArr[valueArr.length-i-1]).intValue();
deci+= (digit * Math.pow(base, i));
}
return deci;
}
 

da-one01

Member
Dec 5, 2010
44
3
0
මේක කෝමද කියලා බලන්න... තේරුන විදිහට කරලා බැලුවා :P තාම Java වලට අලුත්... ;)


Code:
import java.io.*;

public class NewClass {
public static Console c=System.console();
public static void main(String[] args) {

try{ System.out.println("Enter base \nKey- 2,8,10,16");
String base1 = c.readLine("Enter Base: ");
int base2= Integer.parseInt(base1);
switch (base2) {

case 2: System.out.println("you select base 2");
base2();break;
case 8: System.out.println("you select base 8");
base8();break;
case 10: System.out.println("you select base 10");
base10();break;
case 16: System.out.println("you select base 16");
base16();break;
default: System.out.println("Error");
}}
catch(Exception a) {
System.out.println("you did a wrong thing");}

}
public static void base2(){
String num1 = c.readLine("Enter Num: ");
int n1= Integer.parseInt(num1,2);
System.out.println(n1);

}
public static void base8(){
String num1 = c.readLine("Enter Num: ");
int n1= Integer.parseInt(num1,8);
System.out.println(n1);
}
public static void base10(){
String num1 = c.readLine("Enter Num: ");
int n1= Integer.parseInt(num1,10);
System.out.println(n1);
}
public static void base16(){
String num1 = c.readLine("Enter Num: ",16);
int n1= Integer.parseInt(num1,16);
System.out.println(n1);
}
}
 
  • Like
Reactions: ¤--bACarDi--¤

da-one01

Member
Dec 5, 2010
44
3
0
ඔව් මේක වැඩ කරන්නෙ පාදය 2,8,10,16 වලට මට මතක විදිහට :/