Results 1 to 17 of 17
- 11-10-2010, 01:20 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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);
}
}
-
Please edit your post to use code tags (see the first link in my signature below), and please define "isn't working".
Luck!
- 11-10-2010, 01:33 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
It doesn't say there is any errors in the code really..It just says there is an exceptionJava 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); } }
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)
-
- 11-10-2010, 01:42 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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:
Every fiber in my body screams, "use an array!!"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()); .....
- 11-10-2010, 01:57 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
-
- 11-10-2010, 02:17 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
-
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.,
Make sense?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....
- 11-10-2010, 02:25 AM #11
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
yeah I'll try it
- 11-10-2010, 02:38 AM #12
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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)
-
What does your data file look like, the one that causes the error?
- 11-10-2010, 02:56 AM #14
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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
-
We need to see that actual file, the one causing the error.
- 11-10-2010, 03:11 AM #16
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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
-
Similar Threads
-
while loop not working
By RBNSN83 in forum New To JavaReplies: 6Last Post: 06-21-2010, 07:29 AM -
Been working on a code for days
By Link01 in forum Java AppletsReplies: 5Last Post: 05-19-2010, 03:55 PM -
Why is the code not working
By sanox in forum New To JavaReplies: 12Last Post: 09-08-2009, 11:28 AM -
Next Page Code is not working
By Java.child in forum AWT / SwingReplies: 2Last Post: 02-18-2009, 05:26 PM -
JNI: Why this code not working?
By playwin2 in forum Advanced JavaReplies: 5Last Post: 11-25-2008, 01:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks