Results 1 to 16 of 16
  1. #1
    Ulfar is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default [SOLVED] recieving an int from a nonmumeric string

    Hi,
    i've tried to do the above in a numerous amount of ways; splitting, parsing etc etc etc..

    here goes:

    int mut = Group.numOfGames();
    String[] matches = new String[mut];
    for (int i = 0; i < mut; i++){
    int t = i+1;
    matches[i] = t + " " + Group.player1.get(i) + " vs " + Group.player2.get(i);
    }

    String matchnr = (String)JOptionPane.showInputDialog(null, "Match nr", "Result",
    JOptionPane.QUESTION_MESSAGE, null,
    matches, matches[0]);
    String[] options = { "1-0", "0-1" };
    int index = JOptionPane.showOptionDialog(null, "Matchresultat", "Result",
    0, JOptionPane.QUESTION_MESSAGE, null,
    options, options[0]);


    what i want to do is to get the variable t (which is actually set in matchnr), a question field pops up and you get to choose match number (with player1 vs player2 visible) and string matchnr gets set by that choice. umm, hope my explanation is not to mixed up.

    please help me with this, any help will do.

    regards
    Ulfar.
    Last edited by Ulfar; 10-12-2008 at 10:18 AM. Reason: the code format looked awful.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What you want to do is, find the numeric value from a string? So can't you find a pattern there, so you can use a regular expression to find the value.

  3. #3
    Ulfar is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    i've tried with the pattern in matches[], did not work.
    what's the (exact) pattern in JOptionPane? (i'm rather new to this)

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile some sample code

    Quote Originally Posted by Ulfar View Post
    i've tried with the pattern in matches[], did not work.
    what's the (exact) pattern in JOptionPane? (i'm rather new to this)
    Java Code:
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    // sample code for discussion only, not for use in real program.
    public class FindUmFast
    {
        private static final String sampleData = "2d62b470efc5033030a10dcabcd183341026501536bce4ccf0a7e33024b71fed";
        private static final Pattern numerics = Pattern.compile("\\d+");
        private static Matcher matchurally;
        public static void main(String[] args)
        {
            // preliminary effort:
            matchurally = numerics.matcher(sampleData);
            StringBuffer sb = new StringBuffer();
            while(matchurally.find())
            {
                sb.append(matchurally.group());
            }
            // prints: "2624705033030101833410265015364073302471"
            System.out.println(sb.toString());
        }
    }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    Ulfar is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    so basically i just call this method and it will pick out any numeric values?
    does that include player names aswell? (e.g player1 = ulfar1), will the '1' print out aswell?
    thank you very much for this sample code.
    regards ulfar

    (edit)
    that's not at all what i did (or thought pattern was).
    thank you once again
    Last edited by Ulfar; 10-12-2008 at 01:57 PM. Reason: pure stupidity

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Regular expressions are the easiest as well as the best way to handle this kind of cases. You read more about regular expressions in this page.

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default read up on regular expressions

    Quote Originally Posted by Ulfar View Post
    so basically i just call this method and it will pick out any numeric values?
    That is an oversimplification, study eranga's link - but get ready for a long-haul study. Regular expressions do the work you need but are not easy to use at first. They get easier - it is only a matter of how long it takes. The best tutorial I have found is in the javadocs for Pattern class. That's what I used to write this.

    edit: Here, try this:
    Java Code:
    String inputValue = JOptionPane.showInputDialog("Please type in \"1-0\" or \"0-1\"");//
    Then run the above on it, you can do parseInt() to get an actual numeric as the computer understands it.
    Last edited by Nicholas Jordan; 10-12-2008 at 03:34 PM. Reason: additional information
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yep, learning about regex is not an easy task actually. But it's really well for many applications. It's too fast in responding that string operations.

  9. #9
    Ulfar is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    Nicholas Jordan;
    maybe my description of the problem was not clear enough (or maybe i'm a bit to new at java to completely understand what you wrote), but the value that i want to extract is the int value in matchnr (a question square pops up, and the user gets to choose a match(from a list) with the help of the mouse, that value(stored in an array) is then the value that matchnr gets set to).

    took a look at patterns and matching, seems to be a little bit complicated (this is due on thursday), is there a way to perhaps get around this problem while still getting a printout (in the popup square) that equals "(matchnr as an int) player1.get(number) vs player2(number)"?

    any help is appreciated.

    (worst case scenario, i'll drop the "fancy" "(matchnr as an int) player1.get(number) vs player2(number)", and replace it with "(matchnr as an int)".)

    thank you,
    Ulfar.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There's probably a lot easier way to solve your problem then all the gyrations you are going through. Why not have a JList or JComboBox or other collection of Matches (a class that you have created), and when the user selects a match, get the selected match and be done with this mess. Why try to fiddle with Strings when you don't have to?

    In other words, perhaps we have been wrong trying to answer your question without properly focusing on your problem.
    Last edited by Fubarable; 10-12-2008 at 06:43 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default

    I'd try a simpler approach:
    Java Code:
    StringBuffer sbIn = new StringBuffer(<The string>);
    StringBuffer sbOut = new StringBuffer(); // put found digits here
    for (int i =0; i < sbIn.length; i++) {
     if(sbIn.charAt(i) >= '0' && sbIn.charAt(i) <= '9') { // test if digit
       sbOut.append(sbIn.charAt(i);  // save the digit
     }
    }
    String digits = sbOut.toString();  // get string of digits
    Off top of my head. Not compiler checked.

  12. #12
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Yes, basically what you can do is have two buttons, it is just that I cannot tell how many players you will need. As Fubarable states: We need to focus on your problem. There are several approaches already here, first thing in my mind is how many players are there? Second is why are you trying to go through a String? Extracting digits from a string seems to me to collapse the problem though it is an obvious approach for some users.

    How many players and why "1-0" and "0-1" ... looks like you are placing a boolean conditional in the naming of the players .... can it just be player #1 | player #2

    ?....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  13. #13
    Ulfar is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    thank you all for your replies, and i do agree, it's time to focus on the problem.

    now, i've dropped this complicated (and frankly unnecessary) popup-window, and have decided to switch to a JList.
    i'll explain the program a little more so that the gurus* in this forum are able to more properly help me.

    i start with reading the players from a .txt-file located somewhere on the computer. using a buffered reader (br) and the following command (while((s = br.readLine())!=null){add players... to an arraylist}, i then get the size of the arraylist and use that to calculate the number of games to be played (by using numOfGames = (sizeOfArrayList - 1) * (sizeOfArrayList/2); actually i use a for-loop to calculate the same thing).

    having a playschedule (that i create using numOfGames... not necessery to write the details here), i want the user to be able to choose the desired game, and enter the score (hence the option "1-0" // "0-1"). and after that i just print the game + result and then block the ability to set a new score to the same game.

    now, i want to make a JList where the "choice string" (the text that displays in the list for each choosable item) equals (Group.player1.get(index) + " vs " + Group.player2.get(index)). (both player1 and player2 are arraylists that have stored objects of a class Player).
    is that possible? and if so, how do i do it?

    hope that helped a little, (alot of reading),

    once again, thank you all for your help (and for not giving up even though it was alot of text =) ).

    *by guru i mean everyone with more java-experience than me.

  14. #14
    Ulfar is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    JList added, almost everything is solved now, thank you all.
    mods, mark this as solved (if possible)

    Ulfar

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes I can mark this thread as solved. But it should be done by your self. I think you know how to do it.

    Click on Thread Tools on the top of the first post of this thread and click the Mark Thread Solved option there.

  16. #16
    Ulfar is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    oh, ok. i'll do that.
    the assignment is now completed, and i'm grateful for your help. (JList was one of the easiest things one can add =)).

    regards Ulfar
    (this thread is officially dead)
    Attached Thumbnails Attached Thumbnails [SOLVED] recieving an int from a nonmumeric string-first.jpg   [SOLVED] recieving an int from a nonmumeric string-second.jpg  
    Last edited by Ulfar; 10-15-2008 at 11:41 PM. Reason: forgot the result

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  2. string vs string builder??
    By j2vdk in forum New To Java
    Replies: 6
    Last Post: 09-08-2008, 09:38 AM
  3. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 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
  •