1

I have a text file with state-city values:- These are the contents in my file:-

Madhya Pradesh-Bhopal
Goa-Bicholim
Andhra Pradesh-Guntur

I want to split the state and the city... Here is my code

   FileInputStream fis= new FileInputStream("StateCityDetails.txt");
    BufferedInputStream bis = new BufferedInputStream(fis);
    int h=0;
    String s;
    String[] str=null;
    byte[] b= new byte[1024];
    while((h=bis.read(b))!=-1){
     s= new String(b,0,h);
    str= s.split("-");
    }
    for(int i=0; i<str.length;i++){
        System.out.println(str[1]);  ------> the value at 1 is Bhopal Goa
    }
       }

Also I have a space between Madhya Pradesh.. So i want to Remove spaces between the states in the file and also split the state and city and obtain this result:-

     str[0]----> MadhyaPradesh
     str[1]----> Bhopal
     str[2]-----> Goa
     str[3]----->Bicholim

Please Help..Thank you in advance :)

2
  • What is the question? Does your code not work? If not, what do you not understand? Commented Jul 8, 2016 at 5:21
  • The code works fine sir but the result is not as expected...I do not know how to obtain the expected output.. Commented Jul 8, 2016 at 5:22

1 Answer 1

1

I would use a BufferedReader here, rather than the way you are doing it. The code snippet below reads each line, split on hyphen (-), and removes all whitespace from each part. Each component is entered into a list, in left to right (and top to bottom) order. The list is converted to an array at the end in case you need this.

List<String> names = new ArrayList<String>();
BufferedReader br = null;

try {
    String currLine;

    br = new BufferedReader(new FileReader("StateCityDetails.txt"));

    while ((currLine = br.readLine()) != null) {
        String[] parts = currLine.split("-");
        for (int i=0; i < parts.length; ++i) {
            names.add(parts[i].replaceAll(" ", ""));
        }
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
    if (br != null) br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

// convert the List to an array of String (if you require it)
String[] nameArr = new String[names.size()];
nameArr = names.toArray(nameArr);

// print out result
for (String val : nameArr) {
    System.out.println(val);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at com.psl.Client.main(Client.java:31) names.add(parts[1].replaceAll(" ","")); <----- its not doing this..gives an error here
@Bhavana_1622 Can you put any effort on your own to debug this? My guess is that your input file has some lines which do not have a hyphen appearing. I added logic to basically skip over such lines.
Thank you.. yes i will try... :) thnx
add this after splitting currline and adding it to a string:- while ((currLine = br.readLine()) != null) { String[] parts = currLine.split("-"); for(int i=0;i<parts.length;i++){ names.add(parts[i].replaceAll(" ", ""));} } this works fine for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.