?
Solved

line.indexOf problem, probably really easy...

Posted on 2003-03-31
7
Medium Priority
?
531 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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