-1

阅读文本文件时如何跳过 3 行?以下仅显示第 3 行。我该如何通过以下方式纠正它?

表值:

michelle

98765432

michelle@hotmail.com

22

3

6,7

代码:

try
        {                           
            String line = null;         
            while ((line = reader.readLine()) != null)
            {
                if (!(line.startsWith("*")))
                {
                    //reader.readLine();
                    //reader.readLine();
                    //display booking IDs of target customer
                    if (line.equals(String.valueOf(customermobilenumber)))
                    {                                           
                        System.out.println (reader.readLine());
                    }
                }
            }           
        }
4

4 回答 4

0
try
        {                           
            String line = null;         
            while ((line = reader.readLine()) != null) // read first line
            {
                if (!(line.startsWith("*")))
                {
                    String ph_no =  reader.readLine();  // read next line (phone number)
                    String email =  reader.readLine();  // read next line (email)
                    //display booking IDs of target customer
                    if (ph_no.equals(String.valueOf(customermobilenumber)))
                    {                                           
                       // sysout(email);
                        sysout(reader.readLine());
                         sysout(reader.readLine());
                         sysout(reader.readLine());


                    }
                }
            }           
        }
于 2013-11-11T07:54:53.050 回答
0

它可能看起来太愚蠢的解决方案,但经过更多的试验和错误后我得到了它:

try
        {           
            String line = null;         
            while ((line = reader.readLine()) != null)
            {
                if (!(line.startsWith("*")))
                {
                    //display booking IDs of target customer
                    if (line.equals(String.valueOf(customermobilenumber)))
                    {               
                        if (reader.readLine() != null )
                        {
                            //skip lines
                            reader.readLine();
                            reader.readLine();
                            System.out.println (reader.readLine());
                        }

                    }
                }
            }           
        }
于 2013-11-11T07:44:05.057 回答
0
if (line.equals(String.valueOf(customermobilenumber)))
{                                           
    reader.readLine();
    reader.readLine();                        
    System.out.println (reader.readLine());
}
于 2013-11-11T07:44:14.267 回答
-1
     while ((line = reader.readLine()) != null)
                {
                    if (!(line.startsWith("*")))
                    {
//Skip line like this. just check after skip whether file ended or not! to avoid null pointer exception.
                        reader.readLine();
                        reader.readLine();
                        reader.readLine();
                        //display booking IDs of target customer
                        if (line.equals(String.valueOf(customermobilenumber)))
                        {   
                           //use null pointer check here.
                           if(reader.readLine != null)
                           {                                        
                            System.out.println (reader.readLine());
                           }
                           else
                           {
                              //File end.
                           }
                        }
                    }
                }   
于 2013-11-11T07:32:23.487 回答