Follow us on Twitter!
Few are those who can see with their own eyes and hear with their own hearts. - Albert Einstein
Friday, April 25, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 22
Guests Online: 20
Members Online: 2

Registered Members: 82906
Newest Member: ilija
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Page 1 of 2 1 2 >
Author

can anyone help me shorten this JAVA program ?

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 04-03-12 06:41
hello everyone , can anyone please help me to shorten this JAVA program ?

http://www.hellbo. . .2039_.html


p.s I'm not looking for ternary operators or an array to store the suffixes


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: can anyone help me shorten this JAVA program ?

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 04-03-12 06:48
I didn't use a Scanner class or a BufferedReader


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: can anyone help me shorten this JAVA program ?

starofale
Member



Posts: 218
Location: England
Joined: 05.12.07
Rank:
Moderate
Posted on 04-03-12 12:56
Firstly, does that program run for you? It doesn't for me (Openjdk 1.6.0_24). The "main(int i)" is the problem on my computer.

Secondly, your program misses out some numbers (0, 100, 1000, 100000) and I get "ArrayIndexOutOfBoundsException" for large numbers.


Try a new search engine
Author

RE: can anyone help me shorten this JAVA program ?

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 04-03-12 17:22
thanks for the missing nos. , i fixed them , and as for the working of the program , it works fine for me , as i use BlueJ platform .

Code
public static void main(int n)




is just for taking an integer 'n' as input from the user . you can try inserting a scanner class to take the no. as input .

and as for the larger nos. not working , this converter is for the indian no. system , 1 lakh means 100 thousands , and 1 crore = 100 lakhs = 10 million

as you can see that i commented , enter a no. less that 1 crore .




www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: can anyone help me shorten this JAVA program ?

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 05-03-12 07:35
There are many ways you can shorten this program. Take an input number and parse it toString(), keeping track of the length, then build your return value by splitting the string into it's constituent parts.

You're almost there, but you're not using what's available to you in Java's API.


G'bye y'all! I was an asshole, So korg banned me.
Author

RE: can anyone help me shorten this JAVA program ?

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 05-03-12 10:13
thanks


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: can anyone help me shorten this JAVA program ?

ellipsis
Member



Posts: 173
Location:
Joined: 13.06.09
Rank:
Uber Elite
Posted on 17-03-12 01:25
newbee wrote:
hello everyone , can anyone please help me to shorten this JAVA program ?

http://www.hellbo. . .2039_.html


p.s I'm not looking for ternary operators or an array to store the suffixes


If you mean shortening by lines, there are a few things you can do.

Code

int x = (int)a/1000;
dis = dis + convert(x) + " thousand";





can be

Code

dis = dis + convert((int)a/1000)) + " thousand";





Code

System.out.println("input : " + i);
System.out.println("Output : " + dis);





can be

Code

System.out.println("input : " + i + "\r\nOutput : " + dis);





Code

if(n < 10 && n > 0 && flag == 0)
{
    out += " " + ones[n-1];
}





can be

Code

if (n < 10 && n > 0 && flag == 0)
    out += " " + ones[n - 1];





Other than that, the code is as short as it can get.


10000101
Author

RE: can anyone help me shorten this JAVA program ?

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 22-03-12 21:22
I don't know if that is correct since i haven't tried to either compile or run it. Did everything on my notepad, so if there is any mistake, i'm sorry. I tried to shorten it as much as possible in the number of lines, and it ended up like this:

Code

public class word_converter_fn
{
    public static String convert(int n) // this function converts numbers from 1 to 99 into words
    {
        String out = "";
   int flag = 0;
        String tens[] = {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
        String elevens[] = {"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
        String ones[] = {"one","two","three","four","five","six","seven","eight","nine"};

        if (n >= 10)
        {
            if(n < 20 && n > 10)
            {
                out += " " + elevens[(n%10)-1];
                flag = 1;
            }
            else
            {
                out += " " + tens[(n/10)-1];
                n %= 10;
            }
        }

        return (n < 10 && n > 0 && flag == 0) ? out + " " + ones[n-1] : out;
    }

    /* enter no. to be written in words (less than 1 crore)*/
    public static void main(int i) // this function takes input from user , sends it to convert() and adds suffixes like lakh, thousand ,etc.
    {
        int a = i;
        String dis = "";

        if(a == 0)
   {
            dis = "zero";
          }
   else
   {
            if(a > 100000)
            {
                dis = dis + convert((int)a/100000) + " lakh";
                a %= 100000;
              }
            if(a >= 1000 && a < 100000)
            {
                dis = dis + convert((int)a/1000) + " thousand";
                a %= 1000;
            }
            if(a >= 100 && a < 1000)
            {
                dis = dis + convert((int)a/100) + " hundred";
                a %= 100;
            }
   }

        dis += (a >= 0 && a < 100) ? (dis + convert(a)) : "";
        System.out.println("input : " + i + "\r\nOutput : " + dis.trim());
    }
}





Most of the code lines' shortening is due to ternary operators :)

edit: did put the trim on the wrong place, now i changed it

Edited by chess_rock on 22-03-12 21:26
Author

RE: can anyone help me shorten this JAVA program ?

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 24-03-12 09:57
I appreciate your help.


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: can anyone help me shorten this JAVA program ?

ellipsis
Member



Posts: 173
Location:
Joined: 13.06.09
Rank:
Uber Elite
Posted on 25-03-12 05:13
newbee wrote:
I appreciate your help.


Code

public class word_converter_fn {
    public static String convert(int n) {   // this function converts numbers from 1 to 99 into words
        String out = ""; int flag = 0;
        String tens[] = {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
        String elevens[] = {"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
        String ones[] = {"one","two","three","four","five","six","seven","eight","nine"};
       
   if (n >= 10) {
            if(n < 20 && n > 10) {
                out += " " + elevens[(n%10)-1];
                flag = 1;
            } else {
                out += " " + tens[(n/10)-1];
                n = n%10;
            }
        } else if(n < 10 && n > 0 && flag == 0)
            out += " " + ones[n-1];

        return out;
    }
    /* enter no. to be written in words (less than 1 crore)*/
    public static void main(int i) {  // this function takes input from user, sends it to convert() and adds suffixes like lakh, thousand ,etc.
        int a = i; String dis = "";

        if (a == 0)
            dis = "zero"; 
        else if (a > 100000) {
            dis = dis + convert((int)a / 100000) + " lakh";
            a = a % 100000;
        } else if (a >= 1000 && a < 100000) {
            dis = dis + convert((int)a / 1000) + " thousand";
            a = a % 1000;
        } else if (a >= 100 && a < 1000) {
            dis = dis + convert((int)a / 100) + " hundred";
            a = a % 100;
        } else if (a >= 0 && a < 100)
           dis = dis + convert(a);
 
        dis = dis.trim();

        System.out.println("input : " + i + "\r\nOutput : " + dis);
    }
}





Merely cosmetic.


10000101
Author

RE: can anyone help me shorten this JAVA program ?

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 25-03-12 07:12
i meant that i appreciated the fact that they tried to help me .


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: can anyone help me shorten this JAVA program ?

elmiguel
Member



Posts: 161
Location: Your Computer
Joined: 12.12.07
Rank:
God
Posted on 28-03-12 15:18
Just thought I play around with this program and noticed flaw

Had to arrange the main since I use PSPad to run java

Code

  public static void main(String args[]) {
...
    for(String arg : args){
       int i = Integer.parseInt(arg.toString());

...code...





after running the program in the console...

Code


C:\wamp\www\hbh\javaHelp>java word_converter_fn 1 45 17
input : 1
Output : one
input : 45
Output : forty
input : 17
Output : seventeen

C:\wamp\www\hbh\javaHelp>





You will notice that the second parameter did not parse correctly

Code

C:\wamp\www\hbh\javaHelp>java word_converter_fn 45
input : 45
Output : forty

C:\wamp\www\hbh\javaHelp>






Now unless you only intended to output the group in which the number belongs, this is not 100%, if you did only intend for this, then ok.

As for even more shortening,
this:
Code

dis = dis + convert((int)a / 100000) + " lakh";





can be this:
Code

dis += convert((int)a / 100000) + " lakh";





no need to recreate the variable.


this:
Code

dis = dis.trim();
System.out.println("input : " + i + "\r\nOutput : " + dis);





can be this:
Code
 
System.out.println("input : " + i + "\r\nOutput : " + dis.trim());





There is more but I will rewrite it and then post it back.

Please note that there are as many ways to write a program as there are programmers to look at it. These suggestions are only my opinion.


The philosophy of one century is the common sense of the next. -Fortune Cookie

I would like to thank a few friends that I have made here that helped me and deserve to be mentioned:
System_Meltdown, Futility, nvrlivenvrdie, Mastergamer, TrueHacker, S1L3NTKn1GhT, Reelix, ynori7, Demons Halo, kryptor

www.hellboundhackers.org/sig/r/24963.png

www.hellboundhackers.org/sig/hbh2.png
<script>alert('XSS');</script>
Author

RE: can anyone help me shorten this JAVA program ?

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 28-03-12 18:08
it works perfectly fine on my PC (i'm using BlueJ) , for input 45 it gives output "forty five" .

and thanks for the help



www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png



Edited by newbee on 28-03-12 18:08
Author

RE: can anyone help me shorten this JAVA program ?

ellipsis
Member



Posts: 173
Location:
Joined: 13.06.09
Rank:
Uber Elite
Posted on 29-03-12 03:34
newbee wrote:
it works perfectly fine on my PC (i'm using BlueJ) , for input 45 it gives output "forty five" .

and thanks for the help


Blue J is the worst Java "IDE" ever. Just saying. :right:


10000101
Author

RE: can anyone help me shorten this JAVA program ?

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 29-03-12 03:49
if you're going to use Java, just use Eclipse. There's really no sense in using anything else for it :/


G'bye y'all! I was an asshole, So korg banned me.
Author

RE: can anyone help me shorten this JAVA program ?

ellipsis
Member



Posts: 173
Location:
Joined: 13.06.09
Rank:
Uber Elite
Posted on 29-03-12 04:42
Arabian wrote:
if you're going to use Java, just use Eclipse. There's really no sense in using anything else for it :/


It's a tough choice between Eclipse or NetBeans. Personally, NetBeans has everything you need. It's possibly the best Java IDE on the interweb.


10000101
Author

RE: can anyone help me shorten this JAVA program ?

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 29-03-12 13:45
i have to use bluej , coz our indian education system recognizes only that as a JAVA IDE .


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: can anyone help me shorten this JAVA program ?

elmiguel
Member



Posts: 161
Location: Your Computer
Joined: 12.12.07
Rank:
God
Posted on 29-03-12 16:07
Still working on the new program; fun little project. Enhancing the program to produce precise output. Working with a colleague to make it more efficient. Complete rewrite, but I think you will enjoy the result.

BlueJ is more a CType (**Using this lightly; meaning that the main can accept any data type**) Java IDE it is not a real production environment. If you are going to explore more of Java, I suggest some IDEs that have been already mention: Eclipse and/or NetBeans. There several more out there that link into the Java JDK and are good for specifics. I myself use PSPad, a well diverse Text Editor that has the capability to run compilers. Has great syntax highlighters, lightweight, and customizable.


** BlueJ does not recognize Java standards :

Java Standard main:
public static void main(String[] args){...}


BlueJ main method:

public static void main(<T> var){...}

Although this is great for cross language support, it does not follow traditional Java standard. This software is mainly used for introductory use to get students/beginners a generic feel of Java and OOP.


If you read over the why section on bluej.org: number 4, you will understand why some educational institutions use BlueJ as a introductory to Java.


Java is a reasonably clean language, but by no means free of problems for introductory teaching. For teachers, it is important to know those problems and to have a way to deal with them. Two of the problems most often reported in the literature (for example in [1] and [4]) are:

problems with the main function
problems with text I/O

The main function: The main function has to be written with the signature

public static void main (String[] args)

This causes several problems. Firstly, several concepts are used here which cannot be explained at this stage (static functions, arrays, void return types). So teachers have to use some hand-waiving ("just believe me and write it like this") and students feel frustrated. Secondly, this has nothing to do with object-orientation! The main function does not logically belong to any object. Thus, students start off with seeing an exception rather than a good example of object-orientation.

I/O: Text I/O, especially input, is not simple and causes regular problems. In addition, it forces students to deal with exceptions at a very early stage.

BlueJ solves both of these problems!

Because objects can be created interactively, and methods can be called interactively, there is no need for a main function. Students can write classes and call their methods without a need to have a main. And because parameters can be passed to interactive method calls and results are displayed, there is no immediate need for I/O. Students can write and test very sophisticated code without the need for any I/O operations!



http://www.bluej.. . .t/why.html

In the real world, traditional Java is 90% of time is used, if not more.



The philosophy of one century is the common sense of the next. -Fortune Cookie

I would like to thank a few friends that I have made here that helped me and deserve to be mentioned:
System_Meltdown, Futility, nvrlivenvrdie, Mastergamer, TrueHacker, S1L3NTKn1GhT, Reelix, ynori7, Demons Halo, kryptor

www.hellboundhackers.org/sig/r/24963.png

www.hellboundhackers.org/sig/hbh2.png
<script>alert('XSS');</script>
Author

RE: can anyone help me shorten this JAVA program ?

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 29-03-12 18:34
ok man i will definitely try out eclipse and netbeans


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: can anyone help me shorten this JAVA program ?

tuere816
Member

Your avatar

Posts: 41
Location: In your closet, in your head
Joined: 27.11.11
Rank:
Apprentice
Posted on 29-03-12 18:40
newbee wrote:
i have to use bluej , coz our indian education system recognizes only that as a JAVA IDE .



QUOTING OUT OF OP's context ,BUT, the fact remains that Mumbai university does accept Eclipse as an IDE at engineering level Smile


www.hellboundhackers.org/sig/hbh2.png

THE SWEETEST KID ALIVE ..... in Virtual world
img255.imageshack.us/img255/5292/2566.png
kulkarniabhishek94@yahoo.co.in
Page 1 of 2 1 2 >