How to append 4 digit number to the next string read from file

I have one file to read which is like this

mytxt.txt

1234 http://www.abc.com

8754 http://www.xyz.com

I tried with this

try {  
        // make a 'file' object   
        File file = new File("e:/mytxt.txt");  
        //  Get data from this file using a file reader.   
        FileReader fr = new FileReader(file);  
        // To store the contents read via File Reader  
        BufferedReader br = new BufferedReader(fr);                                                   
        // Read br and store a line in 'data', print data  
        String data;  

        while((data = br.readLine()) != null)   
        {  
            //data = br.readLine( );                                       
            System.out.println(data);  
        }                                  
    } catch(IOException e) {  
        System.out.println("bad !");  
 }  

I used this but the actual question is I want to read one this two charachter one by one and then appens the digit to the link which I'll read as string. Can anyone tell me how I am suppose to do that..? any help would be appreciated.

Answers


Parse the line you are reading, search for the first white space (I'm assuming you have only one space separating your digit and your url) something like this:

try {  
    // make a 'file' object   
    File file = new File("e:/mytxt.txt");  
    //  Get data from this file using a file reader.   
    FileReader fr = new FileReader(file);  
    // To store the contents read via File Reader  
    BufferedReader br = new BufferedReader(fr);
    // Read br and store a line in 'data', print data  
    String data;  

    while((data = br.readLine()) != null)   
    {  
        int posWhite = data.indexOf(' ');
        String digit = data.substring(0, posWhite);
        String url = data.substring(posWhite + 1);
        System.out.println(url + "/" + digit);  
    }                                  
} catch(IOException e) {  
    System.out.println("bad !");  
}  

Is this what you want?

   while((data = br.readLine()) != null)   
    {                                      
        String[] data=br.readLine().split();
        if(data!=null&&data.length==2)
          {
           System.out.println(data[1]+"/"+data[0]);
          }else
          {
            System.out.println("bad string!"); 
           }
    }   

In the while((data = br.readLine()) != null), make the code like this:

String tmpData[] = data.split(" ");
System.out.println(tmpData[1] + "/" + tmpData[0]);

Need Your Help

Trim spaces from form input?

javascript validation

Hello so i'm trying to trim the white spaces when the user enter spaces he get an error explaning that he should enter valid chars but if he used: ' test ' the same value is entered in the da...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.