-2

I got .txt file with nationalities and phone numbers in different formats and all these in single quote symbols, also it contains empty lines (''):

''   
'French'   
'1-500'   
'0345134123'   
''   
''   
'German'  
etc  

after I parse with the help of readLine() I got arr[0] with each of these lines.
I need to put lines into different arrays: lines with 'nationality' into one array and lines with 'phone numbers' into other.
I tried this

if(!arr[0].equals("''")){
   String[] arr1 = arr[0].split("'");
   if(!arr1[1].matches("[0-9]+)"){
     nations[n] = arr1[1];
     n++;
   }
   else {
     phone_numbers[p] = arr1[1];
     p++;
   }
}

Ofcourse it didn't work

slxvxBatih
  • 95
  • 2
  • 9
  • "Ofcourse it didn't work". Why? – tsolakp Feb 08 '18 at 02:18
  • 1
    If the number of lines per item be fixed, then I don't see any reason why you can't just iterate and dump each line into its appropriate array. – Tim Biegeleisen Feb 08 '18 at 02:22
  • Is your text file pattern like this: name nationality address phone name nationality address phone etc. – TraxX Feb 08 '18 at 02:49
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) The answer, *"A lot. An absurd amount. More than you think you are capable of. After you have reached the end of your rope and the pain of not having the answer exceeds the vast amount of shame received by posting your question, that's when you can go ahead and ask. Because at that point, you will have done whatever research necessary to make it a good question worth asking!"* –  Feb 08 '18 at 03:27

2 Answers2

0

In your question you said that you want

to put lines into different arrays: lines with 'nationality' into one array and lines with 'phone numbers' into other.

public static void main(String[] args) {
    try{
      File file = new File("path\\to\\yourfile");
      FileReader fr = new FileReader(file);
      BufferedReader br = new BufferedReader(fr);
      String line = "";
      String nationalities = "";
      String phones = "";
      while((line = br.readLine()) != null){
        String[] s = line.split("'");
        if(s.length > 0){
          if(s[1].matches("[a-zA-Z]+")){
          // nationalities
          nationalities += (nationalities.isEmpty()) ? s[1] : " " + s[1];
          }else{
          // line with phone numbers
          phones += (phones.isEmpty()) ? s[1] : " " + s[1];
          }
        }
      }
      String[] nationArr = nationalities.split(" ");
      String[] phoneArr = phones.split(" ");
      for(String val : nationArr){
        System.out.println(val);
      }
      System.out.println("------------");
      for(String val : phoneArr){
        System.out.println(val);
      }
    }catch(IOException e){
      System.out.println("Error");
    }

  }

I tested with this text file

''
'French'
'1-500'
'0345134123'
''
''
'Japan'
'2-200'
'08078933444'
''
''
''
'Germany'
'2-300'
'00078933444'
''

You will get two array, nationality[nationArr] and line with phone[phoneArr]. Here is the answer.

French
Japan
Germany
------------
1-500
0345134123
2-200
08078933444
2-300
00078933444
Nyein Chan
  • 1,215
  • 2
  • 17
  • 32
-1

I would suggest implementing some sort of system to differentiate between the types of lines. You could put 'n' at the start of the line for nationality, then detect it in your code.... Or if you knew the exact order of these lines e.g. nationality,number,nationality,number... you could easily separate these lines e.g. lineNumber%numOfLineTypes==0 would give you the first type of line...