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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:10:14+00:00 2026-05-16T11:10:14+00:00

I am trying to open this file in java and i want to know

  • 0

I am trying to open this file in java and i want to know what i am doing wrong. The in file lies in the same directory as my Java file, but i tried to open this with both netbeans and eclipse and it gave a file not found exception. Can someone help me open this file and read from it. I am really new to java files. Here is the code

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

    public class Practice
    {

      public static void main(String[] args)throws IOException
      {


          FileReader fin = new FileReader("anagrams.in");
          BufferedReader br = new BufferedReader(fin);



          System.out.println(fin);


        String string = "Madam Curie";
        String test = "Radium came";

        string = string.toLowerCase();
        test = test.toLowerCase();

        string = string.replaceAll("[^a-zA-Z0-9]+", "");
        test = test.replaceAll("[^a-zA-Z0-9]+", "");

        char[] array = string.toCharArray();
        char[] array2 = test.toCharArray();

        boolean flag = false;
        HashMap hm = new HashMap();


        for(int i = 0; i < array.length; i++)
        {
            hm.put(array[i], array[i]);
        }

       for(int i = 0; i < array2.length; i++)
       {
           if(hm.get(array2[i]) == null || test.length() != string.length())
           {
               flag = false;
               i = array2.length;
           }
           else
           {
               flag = true;
           }

       }

       System.out.println(flag);
      }

    }
  • 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-16T11:10:14+00:00Added an answer on May 16, 2026 at 11:10 am

    A few tips:

    1. Abide to proper code indentation
      • If you’re using an IDE like Eclipse, it can automatically correct indentation for you
    2. Develop debugging instinct
      • Try to get what the current working directory is, and list all the files in it
    3. Refactor repetitive code
      • Writing paired statements like you did should immediately raise red flags
    4. Effective Java 2nd Edition
      • Item 23: Don’t use raw types in new code
      • Item 52: Refer to objects by their interfaces
      • Item 46: Prefer for-each loops to traditional for loops
    5. Use sensible variable names

    With regards to 2, try something like this:

    public static void listDir() {
        File current = new File(".");
        System.out.println(current.getAbsolutePath());
        for (String filename : current.list()) {
            System.out.println(filename);
        }
    }
    

    Then in your main, simply call listDir before everything else, and see if you’re running the app from the right directory, and if there’s a "anagrams.in" in the directory. Note that some platforms are case-sensitive.

    With regards to 3 and 4, consider having a helper method like this:

    static Set<Character> usedCharactersIn(String s) {
        Set<Character> set = new HashSet<Character>();
        for (char ch : s.toLowerCase().toCharArray()) {
            set.add(ch);
        }
        return set;
    }
    

    Note how Set<E> is used instead of Map<K,V>. Looking at the rest of the code, you didn’t seem to actually need a mapping, but rather a set of some sort (but more on that later).

    You can then have something like this in main, which makes the logic very readable:

        String s1 = ...;
        String s2 = ...;
    
        boolean isNotQuiteAnagram = (s1.length() == s2.length()) &&
            usedCharactersIn(s1).containsAll(usedCharactersIn(s2));
    

    Note how variables are now named rather sensibly, highlighting their roles. Note also that this logic does not quite determine that s1 is an anagram of s2 (consider e.g. "abb" and "aab"), but this is in fact what you were doing.

    Since this looks like homework, I’ll leave it up to you to try to figure out when two strings are anagrams.

    See also

    • Java Coding Conventions
    • Java Language Guide/For-each loop
    • Java Tutorials/Collections Framework

    Related questions

    • Why doesn’t Java Map extends Collection?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this error today when trying to open a Visual Studio 2008 project
recently I downloaded this open source project and I am trying to compile it.
All of a sudden I start getting this error while trying to open 2
I'm trying to solve this flickering problem on the iphone (open gl es game).
im trying to open a file using StreamReader and ive to set a Encoding,
I'm trying to open a folder in explorer with a file selected. The following
I'm trying to open a file and create a list with each line read
I'm trying to open a Microsoft Excel file in a C# program using the
Is there a better way than simply trying to open the file? int exists(const
I'm trying to open and read from a serial port using the System.IO.Ports.SerialPort class.

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.