-4

HI i am trying to print string in revrse order as i am coding it at jsp page log file is look like,

[1322110800] LOG ROTATION: DAILY
[1322110800] LOG VERSION: 2.0
[1322110800] CURRENT HOST STATE:arsalan.hussain;DOWN;HARD;1;CRITICAL - Host Unreachable (192.168.1.107)
[1322110800] CURRENT HOST STATE: localhost;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.06 ms
[1322110800] CURRENT HOST STATE: musewerx-72c7b0;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.27 ms

code is,

 List<String> data = new LinkedList<String>();
    String strpath="/var/nagios.log";
 FileReader fr = new FileReader(strpath);
 BufferedReader br = new BufferedReader(fr);
  String ch;
  int time=0;
  String Conversion="";
    do {
      ch = br.readLine();
Conversion=String.valueOf(inf.stringToLong(ch));
      Date d = new Date(Long.valueOf(Conversion));
    Pattern pt = Pattern.compile("\\[(\\d+)\\]");
    Matcher m = pt.matcher(ch);
    if (m.find()) {
    Date dt = new Date(Long.parseLong(m.group(1)) * 1000);
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
    ch = m.replaceFirst('['+ sdf.format(dt) +']');
   }
out.print(ch+"<br/>"); 
 } while (ch != null);
    fr.close();

then i tried it by adding this in my code,

 String[] arr=ch.split("\\[");
   time=arr.length;
  for(int num=time;num>=0;num--)
   {
     out.print(arr[num]+"<br/>");
   }

now above code look like after adding this code ,

 List<String> data = new LinkedList<String>();
    String strpath="/var/nagios.log";
 FileReader fr = new FileReader(strpath);
 BufferedReader br = new BufferedReader(fr);
  String ch;
  int time=0;
  String Conversion="";
    do {
      ch = br.readLine();
Conversion=String.valueOf(inf.stringToLong(ch));
      Date d = new Date(Long.valueOf(Conversion));
    Pattern pt = Pattern.compile("\\[(\\d+)\\]");
    Matcher m = pt.matcher(ch);
    if (m.find()) {
    Date dt = new Date(Long.parseLong(m.group(1)) * 1000);
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
    ch = m.replaceFirst('['+ sdf.format(dt) +']');
   }
  String[] arr=ch.split("\\[");
   time=arr.length;
  for(int num=time;num>=0;num--)
   {
     out.print(arr[num]+"<br/>");
   }
//out.print(ch+"<br/>"); 
 } while (ch != null);
    fr.close();

BUT result is exception in JSP ???

Hopes for you reply

Best Regards

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Salman Raza
  • 1,635
  • 6
  • 18
  • 18

1 Answers1

0
time=arr.length;
for(int num=time;num>0;num--)
{
  out.print(arr[num-1]+"<br/>");
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Nishant
  • 54,584
  • 13
  • 112
  • 127
  • it is printing as it is as in string not in reverse order i also use out.print(arr[time-1]+"
    "); but the result is same ??
    – Salman Raza Dec 29 '11 at 05:09
  • what? Do you still get error? what you wanted to do, really? You can try printing `time` I am sure your array has `size=1` and hence reverse and forward is the same. Your split is actually not splitting. – Nishant Dec 29 '11 at 05:16
  • now i am putting this code at right side after while condition but it give exception at this "Conversion=String.valueOf(inf.stringToLong(ch));" line ?? – Salman Raza Dec 29 '11 at 05:19
  • i want exactly to print log file as i mentioned above but in reverse order . after do while loop i get ch string having data and i can print it but i want to print ch string data in reverse order so i tried split to do that and i print this code after while but i told you that error in just above post – Salman Raza Dec 29 '11 at 05:24
  • what's `inf`? and `ch` is non-numeric. Infact `Conversion=String.valueOf(inf.stringToLong(ch)); Date d = new Date(Long.valueOf(Conversion));` is not required at all. – Nishant Dec 29 '11 at 05:27