Results 1 to 13 of 13

Thread: Java question

  1. #1
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default Java question

    Hello,

    I'm new to the JAVA programming world and I have a few question.

    I want to make a program that translates numbers in words.
    Example:

    You typ in: 580
    It comes out in words: five hundred eighty

    My question is how to split these numbers, because I have an array filled with numbers comes from the words. But I have to make JAVA split 580 in 500 and 80, or a 5 and 8 because in the array it is saved as those numbers...

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Threre are different possibilities (as usual). If 580 is a String you can use the split() or subString() methods to get the single digits. If it's an int you can hack around with modulo(%) and integer division.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    Yes, it is a String...
    Can you tell me how the Split or the Substring work in this case?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What part of the API doc didn't you understand?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by PhHein View Post
    What part of the API doc didn't you understand?
    That you should actually read it, maybe? That it actually has a purpose, maybe?

  6. #6
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    Well, how to do it in this case, I think that te API documents is very confusing for beginners..

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by masijade View Post
    That you should actually read it, maybe? That it actually has a purpose, maybe?
    What, why, since when? :confused:
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by TGH View Post
    Well, how to do it in this case, I think that te API documents is very confusing for beginners..
    Ok, what have you tried and what went wrong?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    OK,

    I have everythinking accept for the actionPerformed.
    First I thought that Integer.parseInt could work but then I got stuck at the array point, I couldnt get the Integer get split.
    Now I wanted to work with the Substring, but I haven't got any Idea how to do it, because if I want to get the numbers working with the array's you must get integers back right.

    Here is some of my code:

    String[] klein = {"nul", "een", "twee", "drie", "vier", "vijf", "zes", "zeven", "acht", "negen", "tien", "elf", "twaalf", "dertien", "veertien", "vijftien", "zestien", "zeventien", "achttien", "negentien"};
    String[] groot = {"nul", "tien", "twintig", "dertig", "veertig", "vijftig", "zestig", "zeventig", "tachtig", "negentig"};

    public void actionPerformed(ActionEvent e)
    {
    int nr = Integer.parseInt(invoerVeld.getText());
    if(nr<100)
    {
    //?????? must split my Integer at this point
    //88 for example is groot[8] and klein[8]

    if(nr<20)
    {
    uitvoerVeld.setText(klein[nr]);
    }
    }


    }

    I get stuck at the ActionPerformed method...
    Last edited by TGH; 11-24-2009 at 02:19 PM.

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    ok, so you've got an integer:
    Java Code:
    int k = 0;
    int g = 0;
    if(nr<20)
    {
      uitvoerVeld.setText(klein[nr]);
    }else{
      k = nr%10;
      g = (nr - k)/10;
      uitvoerVeld.setText(groot[g] + klein[k]);
    }
    Something like that, it's untested.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  11. #11
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    Yes, Thank you
    I can work futher with this assignment, Its stupid that I didn't saw that earlier.

  12. #12
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    I have a new question,

    How can you put more buttons in the BorderLayout"?
    I want to put three buttons there, one to make something smaller, a button that makes that something go down and a button to make it bigger...

    How can I do it?

    (Now I only get 1 button, bigger or smaller or the 'go down' button...)

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TGH View Post
    I have a new question,

    How can you put more buttons in the BorderLayout"?
    I want to put three buttons there, one to make something smaller, a button that makes that something go down and a button to make it bigger...

    How can I do it?

    (Now I only get 1 button, bigger or smaller or the 'go down' button...)
    You have to think of 'layered' components, say a JPanel with a FlowLayout that stores three buttons; that panel is stored as the 'south' component in another JComponent (also a JPanel?) with a BorderLayout etc.

    kind regards,

    Jos

Similar Threads

  1. Java Question Bank
    By mgm2010 in forum New To Java
    Replies: 2
    Last Post: 07-31-2009, 06:45 PM
  2. question about java rmi
    By hakimade in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2009, 07:15 AM
  3. Java Architecture Question
    By ronakppatel in forum Web Frameworks
    Replies: 1
    Last Post: 10-01-2008, 12:13 PM
  4. Question regarding Java Certification
    By Ms.Ranjan in forum New To Java
    Replies: 11
    Last Post: 06-25-2008, 04:37 AM
  5. Java Question
    By Jay-1.1 in forum New To Java
    Replies: 11
    Last Post: 05-01-2008, 04:04 PM

Posting Permissions

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