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 6817213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:01:22+00:00 2026-05-26T21:01:22+00:00

I’m trying to create a program that reads in a .txt file with multiple

  • 0

I’m trying to create a program that reads in a .txt file with multiple lines containing lists of names. A sample of the test file is below:

Joe Sue Meg Ry Luke

Kay Trey Phil George

I have three classes(also below). Everything works fine, but I would like to know which friend-set has the greatest number of friends (i.e. in the test file Joe would have the greatest number of friends)

The data isn’t limited to only two friend-sets though…

import java.io.*; 

//Finds the file
public class ReadFileLine {     
public static void main(String[] args) throws Exception {       
    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in),1);
    System.out.println("Hello! " + "Please enter the name of your test file: " +
    "\n**Hint** for this assignment the file name is: friendsFile.txt\n");
    String fileName= keyboard.readLine();
    System.out.println(fileName);//
    FileLine doLine = new FileLine();
    doLine.readList(fileName);

}
}

Class 2:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class InStringFile {

//read the file
private BufferedReader in;
//read each line
private String nextLine;


//handle exceptions 
public InStringFile(String filename) {   

    //line by line input
    try {       
    in = new BufferedReader(new FileReader(filename));
      nextLine = in.readLine();
    }
    catch (FileNotFoundException ee){
       System.out.println("We're sorry,\n" +"File " + filename + " cannnot be found.");
       System.exit(0);
    }
    catch (IOException e){
       System.out.println("We're sorry,\n" +"File " + filename + " cannot be read.");
       System.exit(0);
  }
}

//reads the file as string
public String read() {
  String current = nextLine;
  try {
   nextLine = in.readLine();
  }

  //catch exception
  catch (IOException e){
     System.out.println("We're sorry, this file cannot be read.");
     System.exit(0);
  }
  return current;
}


public boolean endOfFile() {
    return (nextLine == null);
}

//close the file
public void close(){
   try {
      in.close();
      in = null;
   }

   //catch if file cannot be closed
   catch (IOException e){
       System.out.println("Problem closing file.");
       System.exit(0);
   }
}
}

Class 3:

import java.util.StringTokenizer;


public class FileLine {


     public  void readList (String fileName) throws Exception {       

        //opens the file and controls file reading               
        InStringFile reader = new InStringFile(fileName);
        System.out.println("\nFile Found!" + 
        " Now reading from file: " + fileName + "\n");

        // line by line read              
        String line;

        do  {
          line = (reader.read());

          //print the friend list
          System.out.println("The following friend-set exists: " + line);  
          this.TokenizeString(line);

        }while (!reader.endOfFile()); 

        reader.close(); 
    }

     //number of friends
    public void TokenizeString(String nameList){
        StringTokenizer tokens = new StringTokenizer(nameList);
        System.out.println("The number of friends in this friend-set is: " + tokens.countTokens()); 

    }

}

Okay, so I modified the fileLine class to be the following:

import java.util.StringTokenizer;


public class FileLine {


     public  void readList (String fileName) throws Exception {       

        //opens the file and controls file reading               
        InStringFile reader = new InStringFile(fileName);
        System.out.println("\nFile Found!" + 
        " Now reading from file: " + fileName + "\n");

        // line by line read              
        String line;

        do  {
          line = (reader.read());

          //print the friend list
          System.out.println("The following friend-set exists: " + line);  
          this.TokenizeString(line, line);

        }while (!reader.endOfFile()); 

        reader.close(); 
    }

     //number of friends
    public void TokenizeString(String nameList, String nameByName)  {
        StringTokenizer tokens = new StringTokenizer(nameList);
        System.out.println("The number of friends in this friend-set is: " + tokens.countTokens()); 

        StringTokenizer st = new StringTokenizer(nameByName, " ");
        String firstName = st.nextToken();
        System.out.println("Friend-set Leader: " + firstName);

        }

    }

So now the code returns the first name in each line… I still am stuck on how to store the number of tokens. IF I could do that then I could compare and return the greatest number (right?)…

  • 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-05-26T21:01:23+00:00Added an answer on May 26, 2026 at 9:01 pm

    Let tokenizeString(..) return the number of friends. Then:

    int maxFriends = 0;
    int maxFriendsLine = 0;
    int currentLine = 0;
    while (..) {
        int friends = tokenizeString(..);
        if (friends > maxFriends) {
            maxFriendsLine = currentLine;
            maxFriends = friends;
        }
        currentLine++;
    }
    

    A few notes:

    • see if you can use commons-lang FileUtils.readLines(..) or guava Files.readLines(..)
    • prefer str.split(" ") instead of StringTokenizer
    • use lower-case methods – that’s what the java convention prescribes.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.