Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

line.indexOf problem, probably really easy...

Posted on 2003-03-31
7
Medium Priority
?
533 Views
Last Modified: 2012-08-14
What im trying to do is have it get everything BUT the lines with cmd in it, the whole ITPInput are another class file for writing data so its not really important, but heres the code...

-- code --
import java.io.*;
import java.util.*;
import java.awt.*;


class RemoveAttacks {

public static void main(String[] args) {

String weblog, newfile;


 if (args.length != 3) {    
  System.err.println ("Usage: java RemoveAttacks <weblogfile> <outputfile>");
  System.exit (1); }

try {

     System.out.println( "Where is the weblog located?" );
     System.out.println( "ex. c:\\windows\\system32\\weblog\\weblog32.txt" );
     System.out.print( "File Location: " );
     weblog = ITPInput.readLine();
     System.out.println( "What would you like the edited/stripped file name to be?" );
     System.out.print( "New File Name: " );
     newfile = ITPInput.readLine();
     
     File origFile = new File(weblog);
     BufferedReader reader = new BufferedReader(
         new FileReader(origFile));  

     File tempFile = new File(newfile);  
     PrintWriter writer = new PrintWriter(
         new FileWriter(tempFile));

     for(String line; (line = reader.readLine()) != null; ) {
       if (line.indexOf("cmd") > -1) {

         System.out.println(line);
       writer.println(line);     } // Used
                   
      }
     
     writer.close();
     reader.close();
        }
      catch(java.io.IOException e){
       System.out.println("Cannot access file");
   }
  }
}

-- code --

The  if (line.indexOf("cmd") > -1) {
line is what is giving the problems, i thought it would be like if (!line.indexOf...) but apparently you cant do that with an int. Is it possible to have it grab all BUT those lines with cmd in it...
Oh in case any of you wanted to know, it filters out webserver logs (or is supposed to :P) for code red/nimda attacks.
0
Comment
Question by:jgilmour
7 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 200 total points
ID: 8238861
if( line.indexOf( "cmd" ) == -1 )
0
 
LVL 35

Expert Comment

by:girionis
ID: 8238871
 Try using:

 if (line.toLowerCase().startsWith("cmd"))
0
 
LVL 35

Expert Comment

by:girionis
ID: 8238884
 Yeap.. sorry didn't read the question carefully. I thought lines were starting with "cmd". Ignore my comment.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 8238923
:-)
0
 

Expert Comment

by:LouisP
ID: 8239348
Hmm. Well, jgilmour, you're trying to get rid of any string that has the substring "cmd" right?

And the <String>.indexOf() operation will return a value bigger than -1 if that substring is found...indicating the index at which that substring starts.

Frankly, I don't think there's anything wrong with your
condition statement:

if (line.indexOf("cmd") > -1)

because that will evaluate true if there is a line with the substring cmd.

I wrote a little test program that confirms it - go check it out yourself:

public class scratch
{

 public scratch()
 {
    String line = "fhsjkfhsdjfsdhjksdfcmdfdhjkfsdhjk";

    System.out.println("First Test:");

    if (line.indexOf("cmd") > -1)
    {
      System.out.println("yeah");
    }
    else
    {
      System.out.println("nay");
    }

    line = "fhsjkfhsdjfsdhjksdffdhjkfsdhjk";

    System.out.println();
    System.out.println("Second Test:");

    if (line.indexOf("cmd") > -1)
    {
      System.out.println("yeah");
    }
    else
    {
      System.out.println("nay");
    }

    System.out.println();
    System.out.println("For Loop Test:");

    for (String l; (l = "fsdjklcmdfdsfdsfsd") != null;)
    {
      System.out.println(l);

      if (l.indexOf("cmd") > -1)
      {
        System.out.println("yeah");

        break;
      }
      else
      {
        System.out.println("nay");
      }
    }
 }

 public static void main(String[] args)
 {
  scratch scratch1 = new scratch();
 }
}

Maybe you should elaborate more on what exactly is it that goes wrong for you.

Personally I think you're problem lies elsewhere.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8239899
You probably wan't to make it case-insensitive:

if(line.toLowerCase().indexOf("cmd") < 0)
0
 
LVL 1

Author Comment

by:jgilmour
ID: 8241134
Great job, thanks to everyone for helping but this seemed to work!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question