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

  • SEARCH
  • Home
  • 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 7940869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:29:52+00:00 2026-06-03T23:29:52+00:00

I have this function: public static int checkRank(String lineToCompare){ int rank = 0; try{

  • 0

I have this function:

public static int checkRank(String lineToCompare){
    int rank = 0;
    try{
        // Open the file that is the first 
        // command line parameter
        FileInputStream fstream = new FileInputStream("ranks.txt");

        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String strLine;
        //Read File Line By Line
        System.out.println("SPOT 0");
        while ((strLine = br.readLine()) != null)   {
            //Compare the line with the line to compare (string)
            System.out.println("SPOT 1");
            if(strLine.trim().equals(lineToCompare)) {
                //I found it!  Read the next line...
                final String lineAfter = br.readLine();
                rank = Integer.parseInt(lineAfter);  
                System.out.println("SPOT 2");
            }else{
                rank = 0;
                System.out.println("SPOT 3");
            }
        }

        //Close the input stream
        in.close();
    }catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

    System.out.println("Username: " + lineToCompare + " | Rank: " + rank);
    return rank;
}

My ranks.txt file is as follows:

test1
2
test2
1

The console outputs (the “SPOT”s were just used for debugging):

[Commands] SPOT 0
[Commands] SPOT 1
[Commands] SPOT 2
[Commands] SPOT 1
[Commands] SPOT 3
[Commands] SPOT 1
[Commands] SPOT 3
[Commands] Username: test1 | Rank: 0

As you can see, if it were working correctly it would say test1 has a rank of 2. Can someone help me identify the problem?

  • 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-03T23:29:53+00:00Added an answer on June 3, 2026 at 11:29 pm

    You should add a break; statement after the line where you print out “SPOT 2” to end the while loop.

    Without the break you set the rank to 0 when you read the line “test2” and then again when you read the line “1” (the while loop continues until the end of file).

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

Sidebar

Related Questions

I have this user defined function. public partial class UserDefinedFunctions { static int i;
I have this function; static public void Print(Object[] arr) { for(int i = 0;
I have this function: public static void Play(string FileName, bool Async = false) {
I've got this function public static AdoEntity.Inspector GetInspectorWithInclude(int id, List<string> properties) { using (var
So I have this function: public static BaseList GetVersesByChapterVerseRanges(string translation, BaseList criteriaList) { BaseList
Suppose I have two functions which look like this: public static void myFunction1(int a,
I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
I have this function: public bool IsValidProduct(int productTypeId) { bool isValid = false; if
In the main view i have this function public function lang($file, $language){ require 'languages/'.$language.'/'.
I have this function which returns a datatype InetAddress[] public InetAddress [] lookupAllHostAddr(String host)

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.