We help IT Professionals succeed at work.

Scanner equivalent JDK 1.2.2

Midwest
Midwest asked
on
401 Views
Last Modified: 2013-12-14
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Author

Commented:
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?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Author

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Yes, but I originally suggested it and you should have at least split the points. Thats how EE works as explained in the guidelines.

Author

Commented:
Can I still split points?  Sorry, I am new to EE and thought I was just supposed to accept the best answer.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you can, will get someone to reopen it for you and show you how.

Author

Commented:
Thanks so much.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.