Results 1 to 17 of 17
  1. #1
    GreenTea is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default Why isn't this while loop code working

    //While loops and file input

    import java.io.*;
    import java.util.StringTokenizer;

    public class While
    {

    private static FileInputStream inFile;
    private static InputStreamReader inReader;
    private static BufferedReader reader;
    private static StringTokenizer strTkn;
    private static String line,word,word2;
    private static double num1,num2,num3,num4,num5,num6,num7,num8,num9,num10 ,num11,total,wage,pay;

    public static void main (String args[]) throws IOException
    {
    initFile();
    getData();
    inFile.close();
    }

    // preparing the file for input

    public static void initFile() throws IOException
    {
    inFile = new FileInputStream ("c:\\!!VHSAPCSData\\VHSP35data.txt");
    inReader = new InputStreamReader(inFile);
    reader = new BufferedReader(inReader);
    }



    public static void getData() throws IOException
    {
    line = reader.readLine(); //Seeding the while condition. Variable line
    // must either hold a valid string
    // or an empty (null) string;

    while(line != null)
    {
    strTkn = new StringTokenizer(line);
    word = strTkn.nextToken();
    word2 = strTkn.nextToken();
    wage = Double.parseDouble(strTkn.nextToken());
    num1 = Double.parseDouble(strTkn.nextToken());
    num2 = Double.parseDouble(strTkn.nextToken());
    num3 = Double.parseDouble(strTkn.nextToken());
    num4 = Double.parseDouble(strTkn.nextToken());
    num5 = Double.parseDouble(strTkn.nextToken());
    num6 = Double.parseDouble(strTkn.nextToken());
    num7 = Double.parseDouble(strTkn.nextToken());
    num8 = Double.parseDouble(strTkn.nextToken());
    num9 = Double.parseDouble(strTkn.nextToken());
    num10 = Double.parseDouble(strTkn.nextToken());
    num11 = Double.parseDouble(strTkn.nextToken());
    calcPay(); // calls to processing and printing methods
    printCalcs();
    line = reader.readLine(); // IMPORTANT updating of the while condition
    // by acquiring a new line of information
    }
    }

    // processing the average
    public static void calcPay()
    {
    total = num2 + num3 + num4 + num5 + num6 + num7+ num8 + num9 + num10 + num11;
    pay = total * num1;

    }

    // Results
    public static void printCalcs()
    {

    System.out.print(word+ " " + wage + " " + num1 + " " + num2+ " " + num3+ " " + num4+ " " + num5+ " " + num6+ " " + num7+ " " + num8+ " " + num9+ " " + num10+ " ");
    System.out.println (num11 + " " + pay);
    }
    }

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

    Default

    Please edit your post to use code tags (see the first link in my signature below), and please define "isn't working".

    Luck!

  3. #3
    GreenTea is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Java Code:
    //While loops and file input
    
    import java.io.*;
    import java.util.StringTokenizer;
    
    public class While
    {
    
    private static FileInputStream inFile;
      private static InputStreamReader inReader;
      private static  BufferedReader reader;
      private static StringTokenizer strTkn;
      private static String line,word,word2;
      private static double num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,num11,total,wage,pay;
    
      public static void main (String args[]) throws IOException
      {
        initFile();
        getData();
        inFile.close();
      }
    
     // preparing the file for input
    
      public static void initFile() throws IOException
      {
        inFile = new FileInputStream ("c:\\!!VHSAPCSData\\VHSP35data.txt");
        inReader = new InputStreamReader(inFile);
        reader = new BufferedReader(inReader);
      }
    
    
    
      public static void getData() throws IOException
      {
         line = reader.readLine();  //Seeding the while condition.  Variable line
                                    // must either hold a valid string
                                    //  or an empty (null) string;
    
    while(line != null)
        {
          strTkn = new StringTokenizer(line);
          word = strTkn.nextToken();
          word2 = strTkn.nextToken();
          wage = Double.parseDouble(strTkn.nextToken());
          num1 = Double.parseDouble(strTkn.nextToken());
          num2 = Double.parseDouble(strTkn.nextToken());
          num3 = Double.parseDouble(strTkn.nextToken());
          num4 = Double.parseDouble(strTkn.nextToken());
          num5 = Double.parseDouble(strTkn.nextToken());
          num6 = Double.parseDouble(strTkn.nextToken());
          num7 = Double.parseDouble(strTkn.nextToken());
          num8 = Double.parseDouble(strTkn.nextToken());
          num9 = Double.parseDouble(strTkn.nextToken());
          num10 = Double.parseDouble(strTkn.nextToken());
          num11 = Double.parseDouble(strTkn.nextToken());
          calcPay();     // calls to processing and printing methods
          printCalcs();
          line = reader.readLine();   // IMPORTANT updating of the while condition
                                      // by acquiring a new line of information
        }
     }
    
       // processing the average
      public static void calcPay()
      {
        total = num2 + num3 + num4 + num5 + num6 + num7+ num8 + num9 + num10 + num11;
        pay = total * num1;
    
      }
    
       // Results
    public static void printCalcs()
      {
    
        System.out.print(word+ " " + wage + " " + num1 + "  " + num2+ "  " + num3+ "  " + num4+ "  " + num5+ "  " + num6+ "  " + num7+ "  " + num8+ "  " + num9+ "  " + num10+ "  ");
        System.out.println (num11 + "  "  + pay);
      }
    }
    It doesn't say there is any errors in the code really..It just says there is an exception

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
    at While.getData(While.java:56)
    at While.main(While.java:19)

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

    Default

    Thanks for formatting your code! :)


    Quote Originally Posted by GreenTea View Post
    It doesn't say there is any errors in the code really..It just says there is an exception

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
    at While.getData(While.java:56)
    at While.main(While.java:19)
    Hm, then perhaps your tokenizer has run out of tokens when you didn't expect it to. Time to debug things with either a debugger or a lot of println statements.

  5. #5
    GreenTea is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    So you mean, I should split it up a bit, instead of having that long line of numbers?

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

    Default

    Quote Originally Posted by GreenTea View Post
    So you mean, I should split it up a bit, instead of having that long line of numbers?
    Actually I didn't say that. What I do know is that you have to take absolute care not to call nextToken when one doesn't exist, and that's one reason that StringTokenizer has a hasMoreTokens method.

    But having said that, I will also tell you that I'm not that familiar with StringTokenizer as I've taken the recommendations Java has in this class's API and use the String#split(...) method:

    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

    Also, any time I see something like this:

    Java Code:
          ....
          num2 = Double.parseDouble(strTkn.nextToken());
          num3 = Double.parseDouble(strTkn.nextToken());
          num4 = Double.parseDouble(strTkn.nextToken());
          num5 = Double.parseDouble(strTkn.nextToken());
          num6 = Double.parseDouble(strTkn.nextToken());
          num7 = Double.parseDouble(strTkn.nextToken());
          num8 = Double.parseDouble(strTkn.nextToken());
          .....
    Every fiber in my body screams, "use an array!!"

  7. #7
    GreenTea is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Actually I didn't say that. What I do know is that you have to take absolute care not to call nextToken when one doesn't exist, and that's one reason that StringTokenizer has a hasMoreTokens method.

    But having said that, I will also tell you that I'm not that familiar with StringTokenizer as I've taken the recommendations Java has in this class's API and use the String#split(...) method:




    Also, any time I see something like this:

    Java Code:
          ....
          num2 = Double.parseDouble(strTkn.nextToken());
          num3 = Double.parseDouble(strTkn.nextToken());
          num4 = Double.parseDouble(strTkn.nextToken());
          num5 = Double.parseDouble(strTkn.nextToken());
          num6 = Double.parseDouble(strTkn.nextToken());
          num7 = Double.parseDouble(strTkn.nextToken());
          num8 = Double.parseDouble(strTkn.nextToken());
          .....
    Every fiber in my body screams, "use an array!!"
    Oh we have not learned that yet. I also know that it is a 'legacy code', but we have to use it anyway.

    Thanks though. I will have to submit it messed up I guess

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

    Default

    Quote Originally Posted by GreenTea View Post
    Oh we have not learned that yet. I also know that it is a 'legacy code', but we have to use it anyway.

    Thanks though. I will have to submit it messed up I guess
    So you're not going to use println and try to debug it, try to figure out why you're getting the error?

  9. #9
    GreenTea is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    So you're not going to use println and try to debug it, try to figure out why you're getting the error?
    No I don't know how. My textbook only covered that very briefly.

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

    Default

    Quote Originally Posted by GreenTea View Post
    No I don't know how. My textbook only covered that very briefly.
    Break out of the box -- you can do this as it isn't hard. I'm only suggesting that you add println statements to see what your variables are holding. For e.g.,

    Java Code:
          while (line != null) {
             System.out.println("line: " + line);
             strTkn = new StringTokenizer(line);
             word = strTkn.nextToken();
             System.out.println("word: " + word);
             word2 = strTkn.nextToken();
             System.out.println("word2: " + word2);
             wage = Double.parseDouble(strTkn.nextToken());
             System.out.println("wage: " + wage);
             num1 = Double.parseDouble(strTkn.nextToken());
             System.out.println("num1: " + num1);
             /..... etc....
    Make sense?

  11. #11
    GreenTea is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    yeah I'll try it

  12. #12
    GreenTea is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    I don't know..It just shows the values in the file that are supposed to be there, and I just tried it on the sample program I used as reference I was given that works..and what shows for that one is the same in relation to what shows in mine (different values though)

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

  14. #14
    GreenTea is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    It's in notepad and looks like this

    "Name name # # # # # # # # # # #
    Name Name # # # # # # # # # # #
    Name Name # # # # # # # # # # #
    Name Name # # # # # # # # # # #"

    Only it has actual numbers like 7.8 0.0 etc. and actual names rather than what I substituted them with

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

    Default

    We need to see that actual file, the one causing the error.

  16. #16
    GreenTea is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    I give up..Sorry for wasting your time. I need to have it in around now though so I either submit what I have or get a zero. It's a virtual class..But the println screen thing helped me and taught me something so it wasn't in vain

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

    Default

    Quote Originally Posted by GreenTea View Post
    I give up..Sorry for wasting your time.
    You didn't waste our time, but next time, you will want to give us more time to help you.



    ..But the println screen thing helped me and taught me something so it wasn't in vain
    That's the spirit!

    Best of luck!

Similar Threads

  1. while loop not working
    By RBNSN83 in forum New To Java
    Replies: 6
    Last Post: 06-21-2010, 08:29 AM
  2. Been working on a code for days
    By Link01 in forum Java Applets
    Replies: 5
    Last Post: 05-19-2010, 04:55 PM
  3. Why is the code not working
    By sanox in forum New To Java
    Replies: 12
    Last Post: 09-08-2009, 12:28 PM
  4. Next Page Code is not working
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 02-18-2009, 06:26 PM
  5. JNI: Why this code not working?
    By playwin2 in forum Advanced Java
    Replies: 5
    Last Post: 11-25-2008, 02:12 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
  •