I have two files Persons.txt and Hobby.txt.In the third file i want to put all the persons names and add each name some hobbies.This is how my files look like
Persons.txt
ID;NUME;PRENUME;DATA_NASTERII;PROFESIA
1;Stan;Ilie;22-01-1977;profesor;
2;Becali;GG;01-07-1965;patron;
3;Tanase;Cristian;07-12-1988;fotbalist;
4;Pop;Ion;21-03-1984;pictor;
5;Popescu;Rodica;17-04-1986;sculptor;

Hobby.txt
ID;NUME;DESCRIERE;NUMAR_MINIM_PERSOANE;ELEMENT_NECESAR
1;baschet;sport in care se arunca mingea la cos;6;minge
2;fotbal;sport in care nue voie sa atingi mingea cu mana in care trebuei sa inscrii in poarta adversa;14;minge
3;chitara;cantatul la chitara;1;chitara
4;pianul; cantatul la pian;1;pian
5;programarea;scrierea de programe software;1;PC

I need a third file that looks like this:
Ion Pop : baschet, volei
Ilie Stan: steaua, handbal

The problem is that i don't know how to get the persons name and how to append to them 2 or three hobbies.i have no idea how to split each line from the file and write to the third file

With some code to see discussion will be match easier. Otherwise we would be looking on forum rules breaking

Do provide evidence of having done some work yourself if posting questions from school or work assignments

I have two files Persons.txt and Hobby.txt.In the third file i want to put all the persons names and add each name some hobbies.This is how my files look like

Persons.txt
ID;NUME;PRENUME;DATA_NASTERII;PROFESIA
1;Stan;Ilie;22-01-1977;profesor;
2;Becali;GG;01-07-1965;patron;
3;Tanase;Cristian;07-12-1988;fotbalist;
4;Pop;Ion;21-03-1984;pictor;
5;Popescu;Rodica;17-04-1986;sculptor;

Hobby.txt
ID;NUME;DESCRIERE;NUMAR_MINIM_PERSOANE;ELEMENT_NECESAR
1;baschet;sport in care se arunca mingea la cos;6;minge
2;fotbal;sport in care nue voie sa atingi mingea cu mana in care trebuei sa inscrii in poarta adversa;14;minge
3;chitara;cantatul la chitara;1;chitara
4;pianul; cantatul la pian;1;pian
5;programarea;scrierea de programe software;1;PC

I need a third file that looks like this:
Ion Pop : baschet, volei
Ilie Stan: steaua, handbal

The problem is that i don't know how to get the persons name and how to append to them 2 or three hobbies.i have no idea how to split each line from the file and write to the third file

This is my code:

import java.io.*;
import java.util.*;
import java.util.ArrayList;

public class ListaHobby {


     String line="";
     Persoana p = new Persoana();
     Hobby h = new Hobby();
      public void writeListaHobbies(){
        try{
          FileReader  file1 =new FileReader("Persoane.txt");
          Scanner scan = new Scanner(new File("Persoane.txt"));

          ArrayList<String> values = new ArrayList<String>();


          System.out.println(values.size());

          FileReader file2 = new FileReader("Hobby.txt");
          scan = new Scanner(new File("Hobby.txt"));

          values = new ArrayList<String>();

          System.out.println(values.size());
          while(scan.hasNext()){
              values.add(scan.next());

          }

          BufferedReader br1 = new BufferedReader(file1);
          BufferedReader br2 = new BufferedReader(file2);

          String temp1="";
          String temp2="";

          while(br1.readLine() != null){
            temp1 = br1.readLine() + temp1;
          }
          while(br2.readLine() != null){

              temp2 = br2.readLine() + temp2;
          }
          String temp = temp1 + temp2;
          System.out.println("temp" + temp);
          FileWriter fw = new FileWriter("PersHobby.txt");

          char buffer[] = new char[temp.length()];
          temp.getChars(0, temp.length(), buffer, 0);
          fw.write(buffer);
          file1.close();
          file2.close();
          fw.close();

        }
        catch(IOException ex){
            System.out.println("Error opening file.");
            System.exit(1);
        }`


}

how to get the persons name

When you read in a line from the names file, you need to split the line into parts at the ;. The name looks like it is the first part of the line. Look at the String class's split method to do the splittng. The save the names in a list.
It looks like the same for the hobby file. Read the lines, split off the hobby and save in a list.
Now you have two lists: one of names and one of hobbies. Use them to build the data to write to the output file.

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.