This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 30th, when you will be able to use this site as normal.

    Forum Stats

  • 3,889,900 Users
  • 2,269,775 Discussions
  • 7,916,823 Comments

Discussions

Show number of identical words in number

User_P726M
User_P726M Member Posts: 14 Green Ribbon

Hello together


I have a question regarding a programming and i don't know how to solve it.


I have 3 strings[] which is sensed by the input of a user.


As an example:

Scanner keyscan = new Scanner(System.in);

System.outprint("Team 1 ");

String team1 = keyscan.nextline();

System.outprint("Team 2 ");

String team2 = keyScan.nextline();

System.outprint("Team 3 ");

String team3 = keyScan.nextline();


int identical = 0;


system.out.println(identical);

keyScan.close();


I am trying to get the number of identical names printed as a number.


Example:

team1 contains the following string "Mike, Steve, Steve, Obama". So Steve appears here 2 times. Then the output should simply be 2. 


How can I program this?

Thanks a lot!

Tagged:
«1

Answers

  • justsomeone
    justsomeone Member Posts: 51 Red Ribbon

    Hello @User_P726M


    if you learned the regex then that would be so easy with it

    here are a free resource for it


    also you can use the string class api https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html it has great method to get part of string and other method to compare it

    here another resource that could help you learn about it


    hope that help and let me know if you stuck with anything

    and have a nice day :)

  • User_P726M
    User_P726M Member Posts: 14 Green Ribbon

    Hey justsomeone!

    Thanks for your time and links! I never had regular expressions before yet, but will get a look on it!

    Unfortunately, I'm at a loss as to how I can program so that it automatically detects the number of duplicate or multiple words and outputs the number of them. I have already thought about working with an additional string where I can then match after the word, but that does not really make sense.

  • justsomeone
    justsomeone Member Posts: 51 Red Ribbon

    you very welcome @User_P726M


    regular expression can do that automatically but sense you did not learned that yet then no problem to use the longer way that you just mentioned you get each word and search in the line and have counter that count how much you find it then output the number

    try to code your idea and share it and happy coding :)

  • User_P726M
    User_P726M Member Posts: 14 Green Ribbon

    Hello justsomeone!

    I tried to do this with a word match at the beginning just to get into.

    public class Main {
    public static void main(String args[]) {
    String string = "Why is blue blue?";
    String word = "is";
    String temp[] = string.split(" ");
    int count = 0;
    for (int i = 0; i < temp.length; i++) {
    if (word.equals(temp[i]))
    count++;
    }
    System.out.println("Numbers: " + string);
    }
    }
    

    It works like this. Unfortunately after several hours I can't get it to work without a word matching and this is stored in the variable identicly. Actually my goal is that the user input in run-time is set out again with a colon and the number occurring. 

    So if the user input is "Mike, Steve, Sarah", the output should be "Mike, Steve, Sarah: 0" and for the second output "Mike, Steve, Steve: 2". 

    I'm absolutly in a blackhole right now. Can you maybe show me how this works because I can't even really imagine how to program this that way.

    Thanks for your time and knowledge. You already helped me last time and i got better into java because of you. Really thanks a lot!

  • justsomeone
    justsomeone Member Posts: 51 Red Ribbon

    you very welcome @User_P726M

    you actuly very close to do it

    String string = "Why is blue blue?";
    

    for this one i am sure you can replace t with the user input right?

    now for

    String temp[] = string.split(" ");
    

    since the separator of each word is ", " comma and space then that what you need to split the input with

    String word = "is";
    

    for this one you need to replace it with each word from the temp[] and search the array for how many this word in it

    so word be = temp[0] then you search for the whole temp how many word repeated

    and get the max of repeat so you would need another variable to keep track of the max repeat

    another way but will need time to think about it as it not straight as before that after you get each word using the origianl line then you call replaceAll method https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#replaceAll(java.lang.String,java.lang.String) and replace all occurrence of that word in that line with "" (empty string) which return a new string then subtract the length of the old line with the new string returned from the replace then divide it by the length of word then it will return the number of it's occurrence in

    so

    ( inputLine.length() - inputLine.replaceAll(word,"").length() ) / word.length()


    System.out.println("Numbers: " + string);
    

    based on the output you mentioned you need to modify that to be string + ": " + max


    hope that help and have a nice day :)

  • User_P726M
    User_P726M Member Posts: 14 Green Ribbon

    Hi justsomeone

    I really appreciate your time and experience! 

    I'm slowly trying to understand how you mean that and I think I understand how you mean that. Apologies for this as I am really new to Java. Unfortunately I am not sure how to do this.

    I don't yet understand how to wrap this so that it just prints all 3 strings then as output + checks this. 

  • User_P726M
    User_P726M Member Posts: 14 Green Ribbon

    justsomeone

    That's what i have so far, but it doesn't work..still thinking about the situation and why it doesn't work.

    import java.util.Scanner;
    public class CountNumbers{
    public static void main(String args[]) {
    	Scanner keyScan = new Scanner(System.in);
        System.out.print("Team 1: ");
        String team1= keyScan.nextLine();
        String word = ",";
        String temp[] = team1.split(" ");
        int count = 0;
        for (int i = 0; i < temp.length; i++) {
        	if (word.equals(temp[i]))
        		count++;
        }
        System.out.println(team1+":"+count);
    	}
    }
    


  • Solomon Yakobson
    Solomon Yakobson Member Posts: 20,213 Red Diamond

    Use map. For example:

    import java.util.HashMap;
    import java.util.Map;
     
    class HashMapExample
      {
       public static void main(String[] args)
         {
          Map<String, Integer> TeamMembers = new HashMap<>();
          String Team = args[0];
          String temp[] = Team.split(",");
          int count = 0;
          for (int i = 0; i < temp.length; i++)
            {
             if (TeamMembers.containsKey(temp[i]))
               {
                TeamMembers.put(temp[i],TeamMembers.get(temp[i]) + 1);
               }
             else
               {
                TeamMembers.put(temp[i],1);
               }
            }
            System.out.println("Total names: " + TeamMembers.size());
            for (String key : TeamMembers.keySet())
              System.out.println(key + " - " + TeamMembers.get(key));
         }
      }
    

    Now:

    C:\>java HashMapExample "Mike,Steve,Steve,Obama"
    Total names: 3
    Mike - 1
    Steve - 2
    Obama - 1
    
    C:\>java HashMapExample "Mike,Steve,Obama,Mike,John,Steve,Obama"
    Total names: 4
    Mike - 2
    Steve - 2
    Obama - 2
    John - 1
    
    C:\>
    

    SY.

  • User_P726M
    User_P726M Member Posts: 14 Green Ribbon

    Hi Solomon Yakobson

    Thank you for your example!

    I tried to understand the code. I created a new class in eclipse with the name HashMapExample and tried to execute it. I get the error "Exception in thread "main" java.lang.arrayindexoutofboundsexception. Do i got it right, that i need to define the scanner like

    Scanner keyScan = new Scanner(System.in);
            System.out.print("Team 1: ");
            String team1 = keyScan.nextLine();
    

    in your code, to fill the array with the users input in run-time? And again sorry, still new in java.

    In theory, i have 3 teams in total.

    Scanner keyScan = new Scanner(System.in);
            System.out.print("Team 1: ");
            String team1 = keyScan.nextLine();
            System.out.print("Team 2: ");
            String team2 = keyScan.nextLine();
            System.out.print("Team 3: ");
            String team3 = keyScan.nextLine();
    

    In each one should the input from the user be the output with the numbers of total same words (or 0 when all words are unique).

  • justsomeone
    justsomeone Member Posts: 51 Red Ribbon

    Hello @User_P726M


    sorry for late replay


     String temp[] = team1.split(" ");
    

    you need to split the team1 with the separator which in our case is ", "


        String word = ",";
    

    word should change to temp[0] then temp[1] and so

    then you use it to compare/search the temp[] how many time this word exist there

    so find a way to make word equal to each element in the temp[] then use that to check how many time it exist in that array


    hope that help and have a nice day :)