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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:14:18+00:00 2026-05-28T03:14:18+00:00

Here I wrote Java code to display duplicate numbers in a text file. Here

  • 0

Here I wrote Java code to display duplicate numbers in a text file. Here I hardcoded the path name of text file. I assumed that text file contains only numbers in every line of text file.

And I like to display only those numbers which are repeated. Code is as shown below:

import java.util.*;
import java.io.*;

public class FileRead {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    HashMap<String,String> lines=new HashMap<String,String>();

    try{
        FileInputStream fstream=new FileInputStream("C:/Users/kiran/Desktop/text.txt");
        DataInputStream in=new DataInputStream(fstream);
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        ArrayList arr=new ArrayList();
        String str,str1;
        int i=0;
        while((str=br.readLine())!=null){
            i++;
            str1=Integer.toString(i);
            if(lines.containsValue(str)){
                System.out.println(str);
            }else{
                lines.put(str1, str);
            }
        }
        in.close();
    }catch(Exception e){
        System.out.println(e);
    }

}
}

Contents of text file is as shown below:

 56
 75
 1
 46
 100
 97
 75
 46
 46

Expected output is:

 75
 46

This is the output I’m getting:

 75
 46
 46

I’m not able to find out the error in the program. Can anyone help me??

  • 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-28T03:14:19+00:00Added an answer on May 28, 2026 at 3:14 am

    You needed to keep track of what you have printed, for example in a HashSet<String>.

    Declare Set<String> seen = new HashSet<String>() at the top, then add an if:

    if(lines.containsValue(str)){
        if (seen.add(str)) {
            System.out.println(str);
        }
    } else {
        lines.put(str1, str);
    }
    

    An even easier solution is to remove else:

    if(lines.containsValue(str)){
        System.out.println(str);
    }
    lines.put(str1, str);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usually, when dealing with Java IO code, here is what I wrote FileOutputStream out
I wrote a script that decodes aydio using FFmpeg. Here's the code: JNIEXPORT jint
I wrote some Java code to generate pseudo-random numbers but I'm running into what
Here I'm trying to read a text file which contains only integers.in every line.For
here's the stored procedure i wrote.In this proc p_subjectid is an array of numbers
See also C Tokenizer Here is a quick substr() for C that I wrote
Here i wrote some code but not getting output.. Please tell me why is
I've got an application here that I wrote many years ago that consists of
Newbie here...can I write one program which incorporates .NET LINQ and also various Java
As comment to one of the questions here a commenter wrote (emphasis mine): ...

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.