JAVA WADDOOOOOOOO warellaaa

Yahapala_Naya

Member
Apr 10, 2015
2,223
180
0
ගෙදර
eclipse daganin ban. eke karala shape eke karala auto generated comments makala submit karapan. apith ohoma thamai oya kale kale.

meke daala thiyena sarala answer eka hari.

but scanner eliyen initialize karapan.
 

SHdinesh

Member
Dec 30, 2014
36
2
0
javac
then java ?
i couldn't compile it using javac it gave me errors


Remove package declaration at the top of the code.
then create a file named as NumberToString.java and save it in a folder.
Next go to the file location using cmd and type javac NumberToString.java
then enter
if u havent got any error, type java NumberToString
hit again enter
 

firazrox

Member
Dec 26, 2009
1,054
193
0
cyberඅවකශායෙ
Remove package declaration at the top of the code.
then create a file named as NumberToString.java and save it in a folder.
Next go to the file location using cmd and type javac NumberToString.java
then enter
if u havent got any error, type java NumberToString
hit again enter

damn bro it works but the problem is after I input a number greater than 900 it says invalid and the program exists right ? but i want it to go up in the sense loop till the user enter a proper number less than 900
 

SHdinesh

Member
Dec 30, 2014
36
2
0
it got compiled
but im getting these when I try to run

C:\drop>java NumberToString
Exception in thread "main" java.lang.NoClassDefFoundError: NumberToString (wrong
name: demos/NumberToString)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

යකෝ class name එකයි file name එකයි same වෙන්න ඕන බොල..
 
  • Like
Reactions: firazrox

ජොසී

Well-known member
  • May 29, 2014
    5,206
    4,453
    113
    Padavi Sripura
    Code:
    [FONT="Courier New"][SIZE="3"][B]
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    class NumberToString{
    public enum hundreds {OneHundred, TwoHundred, ThreeHundred, FourHundred, FiveHundred, SixHundred, SevenHundred, EightHundred, NineHundred}
    public enum tens {Twenty, Thirty, Forty, Fifty, Sixty, Seventy, Eighty, Ninety}
    public enum ones {One, Two, Three, Four, Five, Six, Seven, Eight, Nine}
    public enum denom {Thousand, Lakhs, Crores}
    public enum splNums { Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen}
    public static String text = "";
    
    public static void main(String[] args){
    JOptionPane.showMessageDialog(null," Welcome to my maths app");
    long num=0;
    System.out.println("input number between one and nine hundred");
    Scanner sc;
    
    [COLOR="Red"]Boolean isValid = false;
    while(!isValid){
    	sc = new Scanner(System.in);
    	num = sc.nextInt();
    	if(num < 900 && num > 100){
    		isValid = true;
    		break;
    	}else{
    		isValid = false;
    		System.out.println("input not in range");
    	}
    }[/COLOR]
    
    int rem = 0;
    int i = 0;
    long r = num;
    
    long hundreds = r/100;
    long Tens = (r-hundreds*100)/10;
    long Ones = r-(hundreds*100+Tens*10);
    
    System.out.print("Hundreds : ");
    for(int h=0; h<hundreds; h++){
    System.out.print("o");
    }
    System.out.println("");
    System.out.print("Tens : ");
    for(int h=0; h<Tens; h++){
    System.out.print("o");
    }
    System.out.println("");
    System.out.print("Ones : ");
    for(int h=0; h<Ones; h++){
    System.out.print("o");
    }
    System.out.println(""); 
    while(num > 0){
    if(i == 0){
    rem = (int) (num % 1000);
    printText(rem);
    num = num / 1000;
    i++;
    }
    else if(num > 0){
    rem = (int) (num % 100);
    if(rem > 0)
    text = denom.values()[i - 1]+ " " + text;
    printText(rem);
    num = num / 100;
    i++;
    }
    }
    if(i > 0)
    System.out.println(text);
    else
    System.out.println("Zero");
    }
    
    public static void printText(int num){
    if(!(num > 9 && num < 19)){
    if(num % 10 > 0)
    getOnes(num % 10);
    
    num = num / 10;
    if(num % 10 > 0)
    getTens(num % 10);
    
    num = num / 10;
    if(num > 0)
    getHundreds(num);
    }
    else{
    getSplNums(num % 10);
    }
    }
    
    public static void getSplNums(int num){
    text = splNums.values()[num]+ " " + text;
    }
    
    public static void getHundreds(int num){
    text = hundreds.values()[num - 1]+ " " + text;
    }
    
    public static void getTens(int num){
    text = tens.values()[num - 2]+ " " + text;
    }
    
    public static void getOnes(int num){
    text = ones.values()[num - 1]+ " " + text;
    }
    }[/B][/SIZE][/FONT]
    මේක බලහං.
    මගෙ එකේ නම් හොඳට වැඩ.
    111 ගැහුවම error උඹේ code එක.
     
    Last edited:
    • Like
    Reactions: firazrox

    firazrox

    Member
    Dec 26, 2009
    1,054
    193
    0
    cyberඅවකශායෙ
    ජොසී;18368964 said:
    Code:
    [FONT="Courier New"][SIZE="3"][B]
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    class NumberToString{
    public enum hundreds {OneHundred, TwoHundred, ThreeHundred, FourHundred, FiveHundred, SixHundred, SevenHundred, EightHundred, NineHundred}
    public enum tens {Twenty, Thirty, Forty, Fifty, Sixty, Seventy, Eighty, Ninety}
    public enum ones {One, Two, Three, Four, Five, Six, Seven, Eight, Nine}
    public enum denom {Thousand, Lakhs, Crores}
    public enum splNums { Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen}
    public static String text = "";
    
    public static void main(String[] args){
    JOptionPane.showMessageDialog(null," Welcome to my maths app");
    long num=0;
    System.out.println("input number between one and nine hundred");
    Scanner sc;
    
    [COLOR="Red"]Boolean isValid = false;
    while(!isValid){
    	sc = new Scanner(System.in);
    	num = sc.nextInt();
    	if(num < 900 && num > 100){
    		isValid = true;
    		break;
    	}else{
    		isValid = false;
    		System.out.println("input not in range");
    	}
    }[/COLOR]
    
    int rem = 0;
    int i = 0;
    long r = num;
    
    long hundreds = r/100;
    long Tens = (r-hundreds*100)/10;
    long Ones = r-(hundreds*100+Tens*10);
    
    System.out.print("Hundreds : ");
    for(int h=0; h<hundreds; h++){
    System.out.print("o");
    }
    System.out.println("");
    System.out.print("Tens : ");
    for(int h=0; h<Tens; h++){
    System.out.print("o");
    }
    System.out.println("");
    System.out.print("Ones : ");
    for(int h=0; h<Ones; h++){
    System.out.print("o");
    }
    System.out.println(""); 
    while(num > 0){
    if(i == 0){
    rem = (int) (num % 1000);
    printText(rem);
    num = num / 1000;
    i++;
    }
    else if(num > 0){
    rem = (int) (num % 100);
    if(rem > 0)
    text = denom.values()[i - 1]+ " " + text;
    printText(rem);
    num = num / 100;
    i++;
    }
    }
    if(i > 0)
    System.out.println(text);
    else
    System.out.println("Zero");
    }
    
    public static void printText(int num){
    if(!(num > 9 && num < 19)){
    if(num % 10 > 0)
    getOnes(num % 10);
    
    num = num / 10;
    if(num % 10 > 0)
    getTens(num % 10);
    
    num = num / 10;
    if(num > 0)
    getHundreds(num);
    }
    else{
    getSplNums(num % 10);
    }
    }
    
    public static void getSplNums(int num){
    text = splNums.values()[num]+ " " + text;
    }
    
    public static void getHundreds(int num){
    text = hundreds.values()[num - 1]+ " " + text;
    }
    
    public static void getTens(int num){
    text = tens.values()[num - 2]+ " " + text;
    }
    
    public static void getOnes(int num){
    text = ones.values()[num - 1]+ " " + text;
    }
    }[/B][/SIZE][/FONT]
    මේක බලහං.
    මගෙ එකේ නම් හොඳට වැඩ.
    111 ගැහුවම error උඹේ code එක.

    amboo uba nam deyyek newei dewalayak <3 :D:D:D:yes::yes::yes: thanksssssss machan
     

    NO_MeRcY

    Well-known member
  • Jun 14, 2010
    5,423
    449
    83
    Singapore
    Options කීපයක් තියනවා මල්ලී මම දෙකක් කියන්නම්

    Code:
        public static void main(String[] args) { 
    
            Scanner sc = new Scanner(System.in);
            int input = 0;
    
            while (true) {
                System.out.print("Enter number:");
                input = sc.nextInt();
                if (input < 900) {
                    System.out.print(input);
                    break;
                } else {
                    System.out.println("Invalid Input");
                }
            }
        }


    Code:
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            int input = 0; 
            do {
    
                System.out.print("Enter number:");
                input = sc.nextInt();
    
                if (input > 900) {
                    System.out.println("Invalid Input");
                }
    
            } while (input > 900);
    
            System.out.println(input); 
        }

    මේ දෙකෙන් 2nd එක යූස් කරන්න මල්ලී. මොකද while loop එකකට එකක් break; දාන එක java best practice එකක් නෙමේ

    exceptions එහෙම handle කරල code එක හදා ගනින්.

    Apart from the above

    මම logic එක නම් කියෙව්වේ නැහැ. පේන විදියට එකත් තව fine tune කරන්න පුලුවන්.

    උබ දාල තියෙන code එකේ පොඩි වෙනස් කම් ටිකක් කරපන් මෙහෙම

    > concatenation වලට String literals use කරන්න එපා. StringBuilder යූස් කරන්න.නැත්නම් performance ප්‍රශ්නයක් වෙයි. Modern JRE ඔය වැඩේ අපිට auto කරන දෙනවා ඒත් best practice එකක් විදියට StringBuilder යූස් කරන්න.

    >
    Code:
           StringBuilder sb = new StringBuilder();
            sb.append(denom.values()[i - 1]).append(" ").append(text);


    > class variables public විදියට යූස් කරන්න එපා (උබ වෙන තැනක access කරන්නෙ නැත්නම්)

    > try catch දාල exceptions handle කරන්න.
    E.g. උබගේ මෙන්න මේ line එකෙන් java.lang.ArrayIndexOutOfBoundsException එක පනිනවා
    Code:
     text = tens.values()[num - 2] + " " + text;


    > Enum වල elements වලට CAPITAL LETTERS use කරන්න

    > long Tens should be long tens
     
    • Like
    Reactions: firazrox