Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:34:57+00:00 2026-06-02T16:34:57+00:00

I am new to java just learned it this week, my teacher want me

  • 0

I am new to java just learned it this week, my teacher want me parse a HTTP request saved in text file format. Basically he wanted me to extract
just this line “GET /fenway/ HTTP/1.0\r\n” and this “Host: http://www.redsox.com\r\n”
and save it in a text format.
My program doesn’t work well yet as it contains error. What I did was to read a text file line by line and then enter it in to a function that tries to read the buffer and tokenizes each word and saves it to copyGetWord, then running a while loop to detect if the word “GET” is found it will store getWord to an arraylist.

Another thing is that am only allowed to use java for this no perl or python. Also I have tried jnetpcap libraries but have problem installing them so am stuck with text files.

Hope anyone can help.

this is a sample text file:

Transmission Control Protocol, Src Port: bridgecontrol (1073), Dst Port: http (80), Seq: 1, Ack: 1, Len: 270
Hypertext Transfer Protocol
GET /azur/ HTTP/1.0\r\n
Connection: Keep-Alive\r\n
User-Agent: Mozilla/4.08 [en] (WinNT; I)\r\n
Host: http://www.google.com\r\n

public static void main(String[] args) {

    System.out.println("Tries to read the samplepcap1.txt, \n");
    ReadingFile1 handle = new ReadingFile1();
    ArrayList <String> StoreLine = new ArrayList<String>();

    try{

        FileReader ReadPcap = new FileReader("samplePcapSmall.txt");
        BufferedReader bufferPcap = new BufferedReader(ReadPcap);

        String readBufLine = bufferPcap.readLine();
        StoreLine.add(readBufLine);

        while (readBufLine!=null){
            readBufLine = bufferPcap.readLine();

            handle.LookForWord(readBufLine);
        }

           bufferPcap.close();
    }
    catch (FileNotFoundException e){
        System.out.println("\nFile not found");
    }
    catch (IOException e){
        System.out.println("Problem reading the file.\n");
    }
}

public String LookForWord(String getWord){
    ArrayList <String>MatchingWord = new ArrayList<String>();
    StringTokenizer copyGetWord = new StringTokenizer(getWord," ");

    while (copyGetWord.hasMoreElements()){
        if(copyGetWord.nextToken().matches("GET")){
            MatchingWord.add(getWord);
        }    
    }
    System.out.println("\nMatch: "+MatchingWord);

    return getWord;
}

Tries to read the samplepcap1.txt, stores in an ArrayList and Displays ArrayList

Match: []

Match: [    GET /fenway/ HTTP/1.0\r\n]

Match: []

Match: []

Match: []
Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:182)
at java.util.StringTokenizer.<init>(StringTokenizer.java:204)
at readingfile1.ReadingFile1.LookForWord(ReadingFile1.java:84)
at readingfile1.ReadingFile1.main(ReadingFile1.java:62)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:34:59+00:00Added an answer on June 2, 2026 at 4:34 pm

    Your while loop is the issue. Try this instead:

    String readBufLine = bufferPcap.readLine();
    
    while (readBufLine!=null){
        StoreLine.add(readBufLine);
        handle.LookForWord(readBufLine);
        readBufLine = bufferPcap.readLine();
    }
    
    bufferPcap.close();
    

    This reads the line from the buffer and loops while the read line is not null – the new line is always read as the last action of the body of the loop so that the next iteration of the loop will check that the read line is not null prior to attempting to tokenize it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the new java.nio.file.Files.walkFileTree in Scala. And I was even successful:
I am new to programming in java and i have just learned how to
I stumbled across this: http://en.literateprograms.org/Vending_Machine_%28java%29 It is a java vending machine, I want to
I am extremely new to Java (just started this weekend) and have a background
I am new to Java. I just read that class variables in Java have
Just a warning: I'm completely new to Java and am trying to teach myself
I am new to Java Persistence API . I have just learnt it and
I'm new in Maven,Java and Eclipse. I just download a project that uses Maven.
I'm new to web development and am just wondering about best practices for java
I'm new to Java and just code a program with BigInteger. public static void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.