Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

java program

Posted on 2011-05-06
8
Medium Priority
?
255 Views
Last Modified: 2012-05-11
Looking for a Java implementation of the following:

file execution1.txt
PASS 4  location Date
Fail 3 location Date
PASS2 4  location Date

I want to get all the first set of data like:
PASS, Fail,Pass2 only as a output?
I know the format is fixed each line is having 4 entries and 3rd entry can be a BIG string separated by comma
==
0
Comment
Question by:vkchaitu82
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35707226
Try the following ('x.txt' is the input file):
StringBuilder sb = new StringBuilder();
	Scanner s = new Scanner(new File("x.txt"));
	while(s.hasNext()) {
	    sb.append(s.next()).append(" ");
	    s.nextLine();
	}
	System.out.println(sb);
	s.close();

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35707632
vkchaitu82,
come on, we wrote with you far more complex programs,
what is this question about?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 332 total points
ID: 35710980
BufferedReader in = new BufferedReader(new FileReader(file));
String result = in.readLine.split(" ")[0]+","+in.readLine.split(" ")[0]+","+in.readLine.split(" ")[0];
System.out.println(result);
in.close();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Accepted Solution

by:
for_yan earned 168 total points
ID: 35711018

This is fully functional program:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.StringTokenizer;

public class ReadFirstPart {
    public ReadFirstPart(){

        try{
            DataInputStream in = new DataInputStream(new FileInputStream("execution1.txt"));
            String buff;
            String s= "";
            while((buff=in.readLine()) != null){
                if(buff.trim().length() == 0)continue;
                StringTokenizer t = new StringTokenizer(buff);
                     s += "," + t.nextToken();

            }
            in.close();
            System.out.println(s.substring(1));

        } catch(Exception ex){
            System.out.println("error " + ex.toString());
            ex.printStackTrace();
        }
    }

    public static void main(String [] args){
        new ReadFirstPart();
    }

}

Open in new window


Input from file execution1.txt:
PASS 4  location Date 
Fail 3 location Date
PASS2 4  location Date 

Open in new window


Output:
PASS,Fail,PASS2

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 332 total points
ID: 35711024
typo in my code above, should be:


BufferedReader in = new BufferedReader(new FileReader(file));
String result = in.readLine().split(" ")[0]+","+in.readLine().split(" ")[0]+","+in.readLine().split(" ")[0];
System.out.println(result);
in.close();

Let me know if you have any problems
0
 

Author Closing Comment

by:vkchaitu82
ID: 35713560
Thanks CEHJ:, Yan and objects.

Yan, I am learning file operations and  I was looking how differently and efficiently we can do this. Thats why I posted this question .
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35713566
Can you tell me why you ignored my correct answer?
0
 

Author Comment

by:vkchaitu82
ID: 35713587
sorry CJEJ, its by mistake .
 i want to select all the 3 answers. its my mistake..
I will try to add it .as solution
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question