Forums Register Login

how do I get the next line in the file and tokenize it

+Pie Number of slices to send: Send
//here is my code but whenever I compile and run the program, it gives me the first line in the //file
// here is the file:
//1,2,3,4,5,6,7
//2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36
//2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36
//so how do I read the next line in this file


import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

public class Sales
{
double lab = 0;

private String FileName;

// getting the file
public Sales(String inFileName)
{
FileName = inFileName;
}

private Scanner keyboard = new Scanner(System.in);

public Sales ()
{
//calling the topOfPageInfo method
topOfPageInfo();
}
public static void topOfPageInfo()
{
final String MY_NAME = "sheharyar Alam";
final String DASHES = "---------------------------------------------------";
System.out.println ("\t" + DASHES + "\n\t\t\tName: " + MY_NAME);
System.out.println ("\t\t\tlab #: 1");
System.out.println ("\t\t\tDue Date: 01/31/2008");

GregorianCalendar today = new GregorianCalendar();
DecimalFormat twoDigits = new DecimalFormat ("00");
System.out.println ("\n\t\t\tToday's Date: "
+ twoDigits.format(today.get(Calendar.MONTH)+1)
+ '/' +twoDigits.format(today.get(Calendar.DAY_OF_MONTH))
+ '/' +twoDigits.format(today.get(Calendar.YEAR)));
System.out.println("\t\t\tAnd Time Now is: " + twoDigits.format(today.get(Calendar.HOUR))
+ ':' + twoDigits.format(today.get(Calendar.MINUTE))
+ ':' +twoDigits.format(today.get(Calendar.SECOND)));
System.out.println("\t" + DASHES + "\n");
}

public void readData() throws IOException
{
int i=0;
// opening the file

String inFile = "SalesData.txt";
try
{
Scanner x = new Scanner(new File (inFile));
String week1 = x.next();
// token(sales1) printed with dilimeters

StringTokenizer st = new StringTokenizer(week1,",");
week1 = x.next();
// tokens(sales1) printed without dilimeters
StringTokenizer str = new StringTokenizer(week1,",");

Scanner y = new Scanner(new File (inFile));
String week2 = y.next();

StringTokenizer stg = new StringTokenizer(week2,",");
week2= y.next();

// tokens(sales2) printed without dilimeters
StringTokenizer sti = new StringTokenizer(week2,",");


while( str.hasMoreTokens())
{ String totsales = str.nextToken();

lab = lab + Double.parseDouble(totsales);

i++;
}

System.out.println(" Week #1 Data ");
System.out.println(" --------------");
System.out.println("");
System.out.println("Sales for Week 1:" + week1);
System.out.println("");
System.out.println("Total Sales for Week 1 = "+"$"+lab);
System.out.println("Average Daily Sales of Week 1 = "+"$"+lab/7);
System.out.println("");

System.out.println("----------------------------------------------------------------------");

System.out.println(" Week #2 Data ");
System.out.println(" --------------");
System.out.println("");
System.out.println("Sales for Week 2:" + week2);
System.out.println("");
System.out.println("Total Sales for Week 2 = "+"$"+lab);
System.out.println("Average Daily Sales of Week 2 = "+"$"+lab/7);
System.out.println("");
System.out.println("------------------------------------------------------------------------------");
System.out.println(" Summary ");
System.out.println(" ---------");
System.out.println("");
System.out.println(" Number of Weeks Data Processed ="+"$");
System.out.println("");
System.out.println(" Total Sales for all Week ="+"$");
System.out.println(" Average Weekly Sales ="+"$");

}
catch(Exception a)
{
System.out.println("File not found");

}
}


}
+Pie Number of slices to send: Send
Please use code tags; they make the quoted code much easier to read.

Why are you using StringTokenizer in the first place? You should be able to get the String already parsed into tokens from a Scanner. And StringTokenizer is classed as legacy code in the API.
You can save yourself a lot of bother with DecimalFormat objects by using the printf() method of System.out (PrintStream).
If you are using Scanner, it consumes Exceptions, so you can simply call
if(myScanner.IOException() != null) myScanner.IOException.printStackTrace(); which is better than simply catching Exception and then assuming it is a FileNotFoundException, as you do. BTW: If you insist on catching Exception, then your declaration of throws IOException is unnecessary.

You can use the nextLine() method of Scanner to read a line; you simply call it as many tiems as you like. Probably safest in a while(myScanner.hasNextLine()) loop. But if you are using double values, go through the Scanner class API for a method which reads doubles directly.
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1835 times.
Similar Threads
Reading Objects and Formatting Ouput
I am supposed to call two service classes from one client(main) class?
Object Arrays
Using JOptionPane in NoSalesException
I am supposed to call two service classes from one client(main) class?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:24:50.