Link to home
Start Free TrialLog in
Avatar of Midwest
MidwestFlag for United States of America

asked on

Scanner equivalent JDK 1.2.2

I have written a program assuming I was using JDK 1.5 or higher but it appears the server has JDK 1.2.2 and I cannot update it.

The only real problem is that I am using the Java.util.Scanner which apparently does not exist in the JDK 1.2.2.  

Is there any equivalent in JDK 1.2.2 that I can use to read a file line by line?  I have posted some of my code so you can see how I am trying to use the scanner.  Thanks in advance.


import java.io.*;
import java.util.Scanner;
import java.util.ArrayList; 
public class Main { 
    public static ArrayList transactionList = new ArrayList();
    
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in); 
        System.out.println("Enter path to transactions (InputAssocA) file: ");
        String fpath = input.nextLine(); 
        readTransactions(fpath);      
    } 
    public static void readTransactions(String filePath) throws FileNotFoundException{
        File file = new File(filePath);
        Scanner transactions = new Scanner(file.getAbsoluteFile()); 
        transactions.nextLine(); 
        while(transactions.hasNext()){
            int TransactionID = Integer.parseInt(transactions.next());
            int itemCount = Integer.parseInt(transactions.next());
            String transactionData = new String(transactions.next()); 
            Transaction t = new Transaction(TransactionID, itemCount, transactionData);
            transactionList.add(t);
        }
    }
}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use BufferedReader:
    public static void readTransactions(String filePath) throws IOException {
	String buffer = null;
	File file = new File(filePath);
	BufferedReader in = new BufferedReader(new FileReader(file));
	while((buffer = in.readLine()) != null) {
	    int TransactionID = Integer.parseInt(buffer);
	    int itemCount = Integer.parseInt(in.readLine());
	    String transactionData = in.readLine();
	    Transaction t = new Transaction(TransactionID, itemCount, transactionData);
	    transactionList.add(t);
	}
	in.close();
    }

Open in new window

Avatar of Midwest

ASKER

Thanks.  Does readLine() read the entire line or does it work similar to Scanner.next()? I suppose I should have told you what the file looked like that I was reading.  It is formatted like below.

TID #items items
1 5 A,B,C,D,E
.....
.....

So, the old code would produce results as shown in my comments for the first line.

int TransactionID = Integer.parseInt(transactions.next()); //TransactionID = 1
int itemCount = Integer.parseInt(transactions.next()); //itemCount = 5
String transactionData = new String(transactions.next()); //transactionData = A,B,C,D,E

Am I just going to have to parse this on my own or is there something else I can use that will produce similar results?
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Midwest,

Why did you ignore my earlier comment which suggested you use StringTokenizer?
At EE you need to accept *all* comments that help you, and I told you both how to read file line by line (as you asked) and also how to tokenize your string.
Avatar of Midwest

ASKER

CEHJ provided a specific solution to my specific problem.  Also, I didn't check back until after CEHJ had posted again, which obviously was a better solution.
Yes, but I originally suggested it and you should have at least split the points. Thats how EE works as explained in the guidelines.
Avatar of Midwest

ASKER

Can I still split points?  Sorry, I am new to EE and thought I was just supposed to accept the best answer.
you can, will get someone to reopen it for you and show you how.
Avatar of Midwest

ASKER

Thanks so much.