View Poll Results: How are my works ??

Voters
2. You may not vote on this poll
  • Awesome !!

    0 0%
  • Very Good !!

    0 0%
  • Good !!

    0 0%
  • Fair

    0 0%
  • Improvement needed

    2 100.00%
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Post Java programmes -Arrays,String,etc and related output Arena

    This is one of the most interesting programmes in java

    Number-Word


    The programme give the alphabet output of a no. inserted between 1 and 10,000
    Example :
    Input:Enter a word less than 10000
    5457
    Output:Five Thousand Four Hundred And Fifty Seven


    Code:--


    import java.util.*;

    Java Code:
    class num2word
    {
        public static void main(String args [])
        {
            num2word ob = new num2word();
            ob.check();
        }
        void check ()
        {
            Scanner sc = new Scanner (System.in);
            System.out.println("Enter a number less than 10,000");
            int n = sc.nextInt();
            
            int w[]= new int [4];
            String lib[]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Tweleve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
            int i=4-1,p,x; 
            String c="",d1;
            
            while(n>0 && i>=0)
            {
                x=n%10;
                if(x>=1)
                {
                  w[i]=x;
                }
                else if(x<1)
                {
                    w[i]=0;
                }
                n=n/10;
                i=i-1;
            }
            for(int j=0;j<4;j++)
            {
                p=w[j];
                if(j==0 && p!=0)
                {
                  c=c+lib[p-1]+" Thousand ";  
                }
                if((j>=1 && j<=3) && (p!=0))
                {
                    if (j==1)
                    {
                        c=c+lib[p-1]+" Hundred ";
                    }
                    else if (j==2 && p==1) 
                    {
                        c=c+lib[9+w[j+1]]+" ";
                    }
                    else if ((j==2 && p>1) && (w[j+1] >0))
                    {
                        c=c+lib[p+17]+" "+lib[w[j+1]-1]+" ";
                    }
                    else if ((j==2 && p>1) && (w[j+1]==0))
                    {
                        c=c+lib[p+17]+" ";
                    }
                    else if ((j==3) &&(w[j-1]==0))
                    {
                        c=c+lib[p-1];
                    }
                }
                if(p==0)
                {
                  c=c+"";
                }
            }
            System.out.println(c);
            System.out.println("Continue ?");
            d1=sc.next();
            if(d1=="yes")
            {
                num2word ob1 = new num2word();
                ob1.check();
            }
        }    
    }
    Java Code:
      
    Last edited by sachin@kolkata; 04-03-2014 at 03:29 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java programmes -Arrays,String,etc and related output Arena

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Java programmes -Arrays,String,etc and related output Arena

    Funny how there are no "not good" and "this just totally stinks" options to choose from :) I think someone can't take criticism.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: Java programmes -Arrays,String,etc and related output Arena

    IF u dont comment below

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: Java programmes -Arrays,String,etc and related output Arena

    Ofcourse this is not production code, it's not meant to power a nuclear installation, but fragments like these can get confusing very quickly:
    Java Code:
    if (j==1)
                    {
                        c=c+lib[p-1]+" Hundred ";
                    }
                    else if (j==2 && p==1) 
                    {
                        c=c+lib[9+w[j+1]]+" ";
                    }
                    else if ((j==2 && p>1) && (w[j+1] >0))
                    {
                        c=c+lib[p+17]+" "+lib[w[j+1]-1]+" ";
                    }
                    else if ((j==2 && p>1) && (w[j+1]==0))
                    {
                        c=c+lib[p+17]+" ";
                    }
                    else if ((j==3) &&(w[j-1]==0))
                    {
                        c=c+lib[p-1];
                    }
    c? i? p? w? j? lib[]? c=c+lib[p+17]+" "+lib[w[j+1]-1]+" "?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Java programmes -Arrays,String,etc and related output Arena

    too many magic numbers, too many single character variables. Unreadable code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java programmes -Arrays,String,etc and related output Arena

    Where is the valid input test?

    Use of == vs equals() for a String.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java programmes -Arrays,String,etc and related output Arena

    Just my personal opinion:

    I find it quite hard to read:

    My reasons for this is:
    - I dislike defining multiple variables in one line. That might be ok, but at least not if there is some kind of initialization (int i=4-1,p,x;)
    - Use of magic numbers. Instead of having a "4" in code which is used multiple times, I would prefer a constant e.g. MAX_DIGITS or something like that.
    - You have a if (x>=1) and in the else part you ask if (x<1) - that check is simply not required. If the value is not >=1 then it must be smaller than 1!
    - Variable names - these are simply saying nothing. i, x, n, p, .... Why don't you use variable names that express what is stored by them?
    - Your check function does not use the class instance. It could simply be static. Then you no longer need an instance of num2word at all.
    - Comparing strings should be done with a call to equals and not with ==.

    That was the main points I saw. But I would try to not use recursion when the user wants to do it again. Instead I would use a do while loop. But that is a minor thing.
    Also the core design is not object oriented.

    But the application is working quite fine. So good job so far even that there are the mentioned points! Keep up building stuff!

    Konrad

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Java programmes -Arrays,String,etc and related output Arena

    On top of what's said above.
    Plus points for getting out of main() quickly.

    Minus points for sticking all the logic in a 60+ line method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Formatting output string in Java GUI?
    By dojob in forum New To Java
    Replies: 3
    Last Post: 09-17-2013, 08:16 PM
  2. String related java question
    By Katnopic in forum New To Java
    Replies: 3
    Last Post: 09-07-2012, 03:31 PM
  3. Replies: 1
    Last Post: 04-27-2012, 08:14 AM
  4. Output String Handling in Java
    By merry in forum Eclipse
    Replies: 0
    Last Post: 02-17-2010, 12:56 AM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •