Home > other >  how can I write a code to read each name in a file
how can I write a code to read each name in a file

Time:04-27

I typed 3 names in the file, and I wanted to write a code to count how many times each name was repeated (Example: Alex was repeated in the file 3 times..and so on). The code I wrote only counted each name once, and this is wrong because the names were repeated more than once. Can you help me with the part that could be the cause of this problem?

public class MainClass {

    public static void readFile() throws IOException {
        //File file;
        FileWriter writer=null;
        String name, line;
        List <String> list = new ArrayList <>();
        int countM = 0, countAl = 0, countAh = 0; 
        
    try 
    {
    File file = new File("\\Users\\Admin\\Desktop\\namesList.txt");
    Scanner scan = new Scanner(file); 
       
       while(scan.hasNextLine())    { 
            
        line = scan.nextLine();
        list.add(line);
        }
       
       for (int i=0; i<list.size(); i  ) 
       {
           name=list.get(i);
           
           if (name.equals("Ali"))
           {
               countAl=  1;
           }
          if (name.equals("Ahmed"))
               {
                   countAh=  1;
               }
              if (name.equals("Muhammad"))
              {
                  countM =  1;
              }
       }
       

       Collections.sort(list);
       
        writer = new  FileWriter("\\Users\\Admin\\Desktop\\newNameList");

            for(int i=0; i<list.size(); i  ) 
            {
                
                name = list.get(i);
                writer.write(name  "\n");
            }
             
           writer.close();
           
         System.out.println("How many times is the name (Ali) in the file? "   countAl);
         System.out.println("How many times is the name (Ahmed) in the file? "   countAh);
         System.out.println("How many times is the name (Muhammad) in the file? "   countM);
    
    }
           
     catch(IOException e) {
        System.out.println(e.toString());
    }
}
    
    public static void main(String[] args) throws IOException  {
    
        readFile();
    }
}

CodePudding user response:

You an do this much simpler:

//Open a reader, this is autoclosed so you don't need to worry about closing it
try (BufferedReader reader = new BufferedReader(new FileReader("path to file"))) {
    //Create a map to hold the counts
    Map<String, Integer> nameCountMap = new HashMap<>();
    //read all of the names, this assumes 1 name per line
    for (String name = reader.readLine(); name != null; name = reader.readLine()) {
        //merge the value into the count map
        nameCountMap.merge(name, 1, (o, n) -> o n);
    }
    
    //Print out the map     
    System.out.println(nameCountMap);
} catch (IOException e) {
    e.printStackTrace();
}

CodePudding user response:

try:

      for (int i=0; i<list.size(); i  ) 
   {
       name=list.get(i);
       
       if (name.equals("Ali"))
       {
           countAl  = 1;
       }
      if (name.equals("Ahmed"))
           {
               countAh  = 1;
           }
          if (name.equals("Muhammad"))
          {
              countM  = 1;
          }
   }

This works with me.

= is not same =

  •  Tags:  
  • java
  • Related