Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Show number of identical words in number

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!
Answers
-
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 :)
-
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.
-
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 :)
-
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!
-
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 :)
-
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.
-
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); } }
-
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.
-
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).
-
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 :)