Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a text file textfile.txt which contains data as follows :

A-abc , A-xyz , B-mno , A-ijk , B-pqr

Now, i have to read from this file and store the values in two separate arrays "A" and "B" , such that the values with prefix "A-" gets stored in array A and values with prefix "B-" gets stored in array B.

Also, while storing the data, the prefix needs to be removed i.e. only "abc" needs to be stored in array A.

FileInputStream fstream = new FileInputStream("C:\opt\New_Workspace\Salary.txt"); 
// use DataInputStream to read binary NOT text
// DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null)   {
String[] arrayLine1= strLine.split(" , ");
for(String s:arrayLine1)

String[] arrayLine2 = s.split(": ");
{
if(s.matches("Basic: "))
{
basic = Double.parseDouble(arrayLine[1]);                 
}
else if(s.matches("Perc-D ");
{
percD = Double.parseDouble(arrayLine[3]);                
}
else if(s.matches("Perc-A: "))
{
percA = Double.parseDouble(arrayLine[5]);                 
}

}
share|improve this question
 
Can you please show us what you have tried ? –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Oct 29 '13 at 11:40
 
show us what have you tried? –  JqueryLearner Oct 29 '13 at 11:40
 
do your own homework –  Bohemian Oct 29 '13 at 11:42
add comment

2 Answers

Try something like this :-

FileInputStream in = new FileInputStream("file.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    String strLine;
    String[] filearray;
    filearray = new String[10];

    while ((strLine = br.readLine()) != null) {

    for (int j = 0; j < myarray.length; j++){
    filearray[j] = br.readLine();
    }

    }
    in.close();
share|improve this answer
5  
I think we should encourage people to try these simple things, let them post their code, and help if they have some issue. By doing their assignments we ensure, that they don't learn. :) +1 –  Batty Oct 29 '13 at 11:48
 
I have added my code. It just took some time to edit. –  Namrata Oct 29 '13 at 11:50
add comment

This was written in a hurry, so please pardon any errors:

String aStore = "";
String bStore = "";
String aFinal[];
String bFinal[];
try{
    Scanner input = new Scanner(new File("file.txt"));
    while(input.hasNextLine()){
        String message = input.nextLine();
        message = message.replace(" ", "");
        String store[] = message.split(",");
        for(int a = 0; a < store.length; a++){
            if((store[a]).contains("A-"){
                String t[] = (store[a]).split("-");
                aStore = aStore + "_" + t[1];
            }
            if((store[a]).contains("B-"){
                String t[] = (store[a]).split("-");
                bStore = bStore + "_" + t[1];
            }
        }
        aFinal = aStore.split("_");
        bFinal = bStore.split("-");
    }
    input.close();
}
catch(Exception e){}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.