Results 1 to 12 of 12

Thread: Java: turning a while loop of tokens into a for loop?

  1. #1
    Panzer Division Marduk PanzerKnight's Avatar
    Join Date
    Jul 2003
    Location
    Surrey
    Posts
    555
    Thanks
    6
    Thanked
    0 times in 0 posts

    Question Java: turning a while loop of tokens into a for loop?

    currently have this code:


    while (tokenizer.hasMoreTokens()) {

    int aid = Integer.parseInt(tokenizer.nextToken());

    int type = Integer.parseInt(tokenizer.nextToken());

    int pos1 = Integer.parseInt(tokenizer.nextToken());

    int pos2 = Integer.parseInt(tokenizer.nextToken());

    int pos3 = Integer.parseInt(tokenizer.nextToken());

    }

    but need it as a for loop but note sure how a for loop with tokens would look like. Basically extracting this out of a text file, and after this need to run calculations on pos1, 2 and 3.

    Hopefully someone can help me out or point me in the right direction,

    Thanks!

    Panzer

  2. #2
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Well the iterator for that is contained in the hasMoreTokens() method. So what you need to do is find a method which will provide the index value of the maximum number of tokens in that reference variable and then do for(int i = 0; i < value; i++) where the value is the number of tokens as i stated before. It's pretty straightforward, i've never used tokenizer though

  3. #3
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    for(int loop = 1; loop < tokenizer.countTokens(); loop++)


    Off the top of my head, maybe it's what you're looking for.
    To err is human. To really foul things up ... you need a computer.

  4. #4
    bored out of my tiny mind malfunction's Avatar
    Join Date
    Jul 2003
    Location
    Lurking
    Posts
    3,923
    Thanks
    191
    Thanked
    187 times in 163 posts
    • malfunction's system
      • Motherboard:
      • Gigabyte G1.Sniper (with daft heatsinks and annoying Killer NIC)
      • CPU:
      • Xeon X5670 (6 core LGA 1366) @ 4.4GHz
      • Memory:
      • 48GB DDR3 1600 (6 * 8GB)
      • Storage:
      • 1TB 840 Evo + 1TB 850 Evo
      • Graphics card(s):
      • 290X
      • PSU:
      • Antec True Power New 750W
      • Case:
      • Cooltek W2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Dell U2715H
    Erm... Why? Do you just want to count the tokens? tokenizer.countTokens() will do that for you. And do you realise that you've declared local variables inside the while loop that can't be used outside of it (so none of the tokens you're getting are able to be used anywhere outside of the loop)... Also if you run out of tokens inside the loop it will throw an exception (NoSuchElementException) so you should use if statements and tokenizer.hasMoreTokens() at each step (or tertiary operators)

  5. #5
    Panzer Division Marduk PanzerKnight's Avatar
    Join Date
    Jul 2003
    Location
    Surrey
    Posts
    555
    Thanks
    6
    Thanked
    0 times in 0 posts
    hey thanks for the replies and the countTokens() method, using that.

    Trying to extract data from text file, then run calculations on it. I have lines like this:

    1 3 -2.0566 -1.8459 -1.1465d
    2 27 -8.4587 1.4545 2.7890w

    trying to extract the 3d coordinates (the last 3 columns) and caclulate the distance between em.

    creating a multidomensional array and using for loops, so should be able to use the tokens within that?

    if i use: for ( int j = 0; j <= tokenizer.countTokens(); j++);

    it should just stop when i reach the last token.

    hopefully it will work out as planned

    thanks for your help so far

  6. #6
    bored out of my tiny mind malfunction's Avatar
    Join Date
    Jul 2003
    Location
    Lurking
    Posts
    3,923
    Thanks
    191
    Thanked
    187 times in 163 posts
    • malfunction's system
      • Motherboard:
      • Gigabyte G1.Sniper (with daft heatsinks and annoying Killer NIC)
      • CPU:
      • Xeon X5670 (6 core LGA 1366) @ 4.4GHz
      • Memory:
      • 48GB DDR3 1600 (6 * 8GB)
      • Storage:
      • 1TB 840 Evo + 1TB 850 Evo
      • Graphics card(s):
      • 290X
      • PSU:
      • Antec True Power New 750W
      • Case:
      • Cooltek W2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Dell U2715H
    Quote Originally Posted by PanzerKnight
    hey thanks for the replies and the countTokens() method, using that.

    Trying to extract data from text file, then run calculations on it. I have lines like this:

    1 3 -2.0566 -1.8459 -1.1465d
    2 27 -8.4587 1.4545 2.7890w

    trying to extract the 3d coordinates (the last 3 columns) and caclulate the distance between em.

    creating a multidomensional array and using for loops, so should be able to use the tokens within that?

    if i use: for ( int j = 0; j <= tokenizer.countTokens(); j++);

    it should just stop when i reach the last token.

    hopefully it will work out as planned

    thanks for your help so far
    I'm assuming you're new to java so bookmark this:

    http://java.sun.com/j2se/1.5.0/docs/api/index.html

    Has all you need to know about the API (replace the 1.5.0 with an earlier version number if you need to use an earlier SDK).

    It's not really important for a first go at a new language but you might want to think about creating a "Point3D" class to represent your points and give your Point3D class a method that returns the distance between itself and another point (and / or a static method that accepts two points)... And some kind of collection object ("Shape", "Shape3D"?)

  7. #7
    Panzer Division Marduk PanzerKnight's Avatar
    Join Date
    Jul 2003
    Location
    Surrey
    Posts
    555
    Thanks
    6
    Thanked
    0 times in 0 posts
    yes, keep coming across that url on google

    To use Point3D i need to download Java 3D API right?
    shall explore that method as well i think

    Thanks for your help mate!

  8. #8
    Sam
    Sam is offline
    Registered+
    Join Date
    Oct 2003
    Posts
    22
    Thanks
    0
    Thanked
    0 times in 0 posts
    Try something like this (untested so no moaning if its uncompilable )

    // Use a buffered reader to read in your file line by line
    BufferedReader reader = new BufferedReader(new FileReader("file.text"));
    String string = null;
    // get each line and assign to a temporary string
    while ((string = reader.readLine()) != null) {

    // split the string up on spaces
    for (String coords : string.split("\\s")) {

    if (coords.length != 5)
    throw new IOException("Should be 5 coords per line");

    System.out.println("Coord 0 = " + coords[0]);
    // or convert to a double
    Double.parseDouble(coords[1]);
    }

    }

  9. #9
    bored out of my tiny mind malfunction's Avatar
    Join Date
    Jul 2003
    Location
    Lurking
    Posts
    3,923
    Thanks
    191
    Thanked
    187 times in 163 posts
    • malfunction's system
      • Motherboard:
      • Gigabyte G1.Sniper (with daft heatsinks and annoying Killer NIC)
      • CPU:
      • Xeon X5670 (6 core LGA 1366) @ 4.4GHz
      • Memory:
      • 48GB DDR3 1600 (6 * 8GB)
      • Storage:
      • 1TB 840 Evo + 1TB 850 Evo
      • Graphics card(s):
      • 290X
      • PSU:
      • Antec True Power New 750W
      • Case:
      • Cooltek W2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Dell U2715H
    Quote Originally Posted by PanzerKnight
    yes, keep coming across that url on google

    To use Point3D i need to download Java 3D API right?
    shall explore that method as well i think

    Thanks for your help mate!
    Heh heh... Yes... I was hinting. Though if you're just starting out learning the language you might want to create your own classes just to get a feel for Java and OO programming in general (assuming you're new to both)

  10. #10
    Panzer Division Marduk PanzerKnight's Avatar
    Join Date
    Jul 2003
    Location
    Surrey
    Posts
    555
    Thanks
    6
    Thanked
    0 times in 0 posts
    have written some basic classes before.

    How can i view the Point3d class btw?

  11. #11
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    view them? javadocs of course!

  12. #12
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    I seem to recall if you go into like the Enumerations or the StringTokenizer docs there are snippets of code on the use of those classes which I mostly just copy and paste and tweak to my using. You should really keep that Javadoc link handy. Good luck.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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