Keep track of string matches to create a new ID - java

This is the prompt
Write a program that reads in a name and outputs an ID based on the name. The ID should be uppercase and formatted using the first three letters of the name and three digits starting at 005. The digits should be incremented by 5 if an ID already exists.
This is my code so far
import java.util.Scanner;
public class substring_match {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int id = 5;
int repeatId = 5;
int nameCount = 1;
String[] nameArray = new String[5];
String name;
String subName = null;
for (int i = 0; i<nameArray.length; i++){
name = scan.nextLine();
subName = name.substring(0, 3);
nameArray[i] = subName;
}
for(int x = 0; x < nameArray.length; x++){
if(nameArray[x].substring(0, 3) == subName.substring(0,3)){
nameCount = nameCount + 1;
System.out.println("nameCount " + nameCount);
repeatId = nameCount * id;
if(repeatId == 5){
System.out.println(nameArray[x].toUpperCase() + "00" + repeatId);
}else{ // if repeatId is 10 or higher, won't need the extra 0
System.out.println(nameArray[x].toUpperCase() + "0" + repeatId);
}
}else{
System.out.println(nameArray[x].substring(0, 3).toUpperCase() + "00" + id);
}
}
}
}

I would probably consider doing this another way...
Have an empty hashmap<String sub, int multiplier>
Read in the name
Generate the subname
Fetch from or create subname in hashmap
If new, set multiplier to 1
If exists, increment multiplier
Return String.format(“%s%3d”, subname, multiplier x 5).toUpperCase()
I would give you code but writing the above was hard enough on an iPad 😂

Related

How do I check to see if a user Input matches an element of a 2D array? JAVA

I am creating a student grade book that allows a user to input their students names and 4 grades, then have options to calculate student and class averages, as shown in the pic below.
With the code below, I am receiving the student average as "0.0%" and no errors. Am I correctly instructing the program to see if the students name matches the appropriate element in the two-dimensional array?
I have all the other parts of the program working.
private String[][] grades = new String[16][5];
//Declare Variables
String firstName, lastName, name;
String test1, test2, test3, test4;
test1 = t1Input.getText();
test2 = t2Input.getText();
test3 = t3Input.getText();
test4 = t4Input.getText();
firstName = firstInput.getText();
lastName = lastInput.getText();
name = firstName + " " + lastName + ": ";
grades[students][0] = name;
grades[students][1] = test1;
grades[students][2] = test2;
grades[students][3] = test3;
grades[students][4] = test4;
students++;
private void sAvgInputActionPerformed(java.awt.event.ActionEvent evt) {
String firstName = firstInput.getText();
String lastName = lastInput.getText();
String name = firstName + " " + lastName + ": ";
int sum = 0;
double divide = 0;
for (int i = 0; i <= 1; i++) {
if (name.equals(grades[0][1])) {
for (int grade = 1; grade <= 4; grade++) {
sum = Integer.parseInt(grades[i][1]) + i;
}
break;
}
}
divide = sum / 4;
gradesOutput.setText(firstName + "'s grade average is " + divide + "%.");
}
Change the loop to this:
//assume it as a simple table since its 2d array
//Since you are storing the name in first(0th index) column,
//only row should be loop
for (int i = 0; grades[i][0] != null; i++) {
if (name.equals(grades[i][0])) {
for (int grade = 1; grade <= 4; grade++)
//same row,different column
sum = sum + Integer.parseInt(grades[i][grade]);
break;
}
}

I keep getting the error "Index 5 out of bounds for length 5" but don't know what to change

I'm pretty new to coding so please bear with me, I am just experimenting with ways to shuffle an array for eventual use in shuffling a deck of cards in a card game I am creating in Java. I know that index 5 is out of bounds for length 5 but what is puzzling to me is that the error doesn't ALWAYS pop up. Sometimes I try to run my code and it works just fine, other times I get the error even if I haven't changed anything between times that I run it.
public class Card{
public static void shuffle(Object[] array) {
int noOfCards = array.length;
for (int i = 0; i < noOfCards; i++) {
int s = i + (int)(Math.random() * (noOfCards - 1));
Object temp = array[s]; //this is the first line it says has a problem
array[s] = array[i];
array[i] = temp;
}
}
public static void main(String[] args) {
String[] strOfCards = {"A","B","C","D","E"};
Card.shuffle(strOfCards); //this is the second line that has a problem
for(int i = 0; i < strOfCards.length; i++) {
System.out.println(strOfCards[i] + " ");
}
}
}
I have no idea how to change the flawed lines, any suggestions are welcome!
*** i have tried changing the number of letters in the string but then the error changes with it i.e. "Index 6 out of bounds for length 6"
Consider the lines:
for (int i = 0; i < noOfCards; i++) {
int s = i + (int)(Math.random() * (noOfCards - 1));
Object temp = array[s]; //this is the first line it says has a problem
i varies from 0 to noOfCards - 1
Your random number expression varies from 0 to noOfCards - 2
So s varies from 0 to (2 * noOfCards) - 3
Then array[s] will throw an exception whenever s >= noOfCards
It doesn't happen every time you run it because sometimes the random numbers all happen to be under noOfCards
If you want to swap with a random other card then you could try:
Random random = new Random();
int s = (i + random.nextInt(noOfCards - 1)) % noOfCards;
I realise you're using this as a learning opportunity, but in case you weren't aware, there is a Collections.shuffle method that can be used to shuffle collections such as ArrayList in one command.
Max index of array with length N is (N-1). Code should be like this:
public static void shuffle(Object[] array) {
int noOfCards = array.length;
Random random = new Random();
for (int i = 0; i < noOfCards; i++) {
int s = random.nextInt(noOfCards);
Object temp = array[s];
array[s] = array[i];
array[i] = temp;
}
}
Here is the line that causes the trouble:
int s = i + (int)(Math.random() * (noOfCards - 1));
Whenever the value s is >= 5, the array[s] will point to something out of bound.
For example i can be 3, if Math.random() returns something like 0.5, then 3 + (int)(0.5 * 4) is equal to 5, which is out of bound (0..4)
It doesn't happen all the times, because some times the Math.random() generates small enough number, so the evaluation of s is smaller than 5.
To ensure that the value of s always in the range (0..4), you should modulo the result to 5.
Here is how the line should be:
int s = (i + (int)(Math.random() * (noOfCards - 1))) % 5;
change this line
int s = i + (int)(Math.random() * (noOfCards - 1));
to this
int s = (int)(Math.random() * (noOfCards - 1));
just remove i variable from the above code
You need to remember that indices are zero-based in Java.
Note a couple of things:
- make things final if they're final
- use SecureRandom: it is more random than Random
- use the nextInt(n) method of SecureRandom to get an int in your range
- note the use of Streams to print out the result
import java.security.SecureRandom;
import java.util.stream.Stream;
public class Card {
public static void shuffle(final Object[] array) {
final SecureRandom random = new SecureRandom();
final int noOfCards = array.length;
for (int i = 0; i < noOfCards; i++) {
final int s = random.nextInt(noOfCards);
final Object temp = array[s];
array[s] = array[i];
array[i] = temp;
}
}
public static void main(final String[] args) throws Exception {
final String[] strOfCards = {"A","B","C","D","E"};
Card.shuffle(strOfCards);
Stream.of(strOfCards).forEach(System.out::println);
}
}
If you fancy using a List it's a bit more compact:
import java.security.SecureRandom;
import java.util.*;
import java.util.stream.IntStream;
public class Card {
public static void main(final String[] args) throws Exception {
final SecureRandom random = new SecureRandom();
final List<String> cards = Arrays.asList("A", "B", "C", "D", "E");
IntStream.range(0, cards.size()).forEach(i ->
Collections.swap(cards, i, random.nextInt(cards.size()))
);
cards.forEach(System.out::println);
}
}
M getting the same issue with this code and still no idea how to solve it
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Main
{
public static void main (String[] args)
{
while (true)
{
String number = JOptionPane.showInputDialog("Please Enter \n"
+ "1 for 'Add new Employee'"
+ "\n 2 for 'Search in employee'"
+ "\n 3 for 'Exit'");
int convertnumber = Integer.parseInt(number);
if (convertnumber == 1)
{
addEmployee();
}
else if (convertnumber == 2)
{
String eId = JOptionPane.showInputDialog("Enter ID to search");
searchEmplye(eId);
}
else if (convertnumber == 3)
{
JOptionPane.showMessageDialog(null, "Developed By: BC180401942 SHEHZAD ADEEL");
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null, "Invalid input");
}
}
}
public static void searchEmplye(String eId)
{
try
{
String tokens[] = null;
String id, name, dob, qual, expe, pays;
FileReader fr = new FileReader("empData.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
if (line == null)
{
JOptionPane.showMessageDialog(null, "Employee Not found");
}
while (line != null)
{
tokens = line.split (",");
id = tokens[0];
name = tokens[1];
dob = tokens[2];
qual = tokens[3];
expe = tokens[4];
pays = tokens[5];
Employee emp = new Employee (id, name, dob, qual, expe, pays);
ArrayList list = new ArrayList();
list.add(emp);
line = br.readLine();
/*
for (int i = 0; i < list.size(); i++)
{
Employee p = (Employee) list.get(i);
if (eId.equals(p.getEmpId()))
{
JOptionPane.showMessageDialog(null, "Employee: \n"
+ "Employee ID: " + p.getEmpId()
+ " \n Employee Name: " +p.getEmpName()
+ " \n Employee DOB: " + p.getEmpDoB()
+ " \n Qualification: " + p.getEmpQualf()
+ " \n Experience: " + p.getEmpExp()
+ " \n Pay Scal: " + p.getEmpPSacle()
);
}
*/
for (int i = 0; i < list.size(); i++)
{
Employee p = (Employee) list.get(i);
if (eId.equals(p.getEmpId()))
{
JOptionPane.showMessageDialog( null, "Employee: \n" +
"EmpId: " + p.getEmpId() + "\n" +
"Name: " + p.getEmpName() + "\n" +
"DoB: " + p.getEmpDoB() + "\n" +
"Qualification: " + p.getEmpQualf() + "\n" +
"Experience: " + p.getEmpExp() + "\n" +
"PayScale: " + p.getEmpPScale() );
}
else
{
JOptionPane.showMessageDialog(null, "Employee Not found");
}
}
}
br.close();
fr.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public static void addEmployee()
{
try
{
ArrayList list = new ArrayList();
String id = JOptionPane.showInputDialog("Enter Employee ID: ");
String name = JOptionPane.showInputDialog("Enter name: ");
String dob = JOptionPane.showInputDialog("Enter DoB (year): ");
String qual = JOptionPane.showInputDialog("Enter Qualification: ");
String exp = JOptionPane.showInputDialog("Enter Employee experience (in months): ");
String pays = JOptionPane.showInputDialog("Enter Employee Pay Scale (in number): ");
Employee emp = new Employee (id, name, dob, qual, exp, pays);
list.add(emp);
/*
ArrayList list = new ArrayList();
String id = JOptionPane.showInputDialog("Enter ID ");
String name = JOptionPane.showInputDialog("Enter Name ");
String dob = JOptionPane.showInputDialog("Enter DOB ");
String qualification = JOptionPane.showInputDialog("Enter qualification ");
String experience = JOptionPane.showInputDialog("Enter Employee Experience");
String paScale = JOptionPane.showInputDialog("Enter Employee pay scale ");
Employee emp = new Employee(id, name, qualification, experience, paScale);
list.add(emp);
*/
String line;
FileWriter fw = new FileWriter("empData.txt");
PrintWriter pw = new PrintWriter(fw);
for (int i = 0; i < list.size(); i++)
{
emp = (Employee) list.get(i);
//line = emp.getEmpId() + "," + emp.getEmpName() + "," + emp.getEmpDoB() + "," + emp.getEmpQualf() + "," + emp.getEmpExp() + "," + emp.getEmpPSacle();
line = emp.getEmpId() + "," +
emp.getEmpName() + "," +
emp.getEmpDoB() + "," +
emp.getEmpQualf() + "," +
emp.getEmpPScale();
pw.println(line);
}
pw.flush();
pw.close();
fw.close();
}
catch (IOException ioEx)
{
System.out.println(ioEx);
}
}
}

How do I make a for loop that will print an Array of strings in order in Java?

I am trying to make a for loop to print out all of the following strings to go into the println statement below but cannot figure out how to get the for loop to print the Array. I keep getting the error 'cannot find symbol.' There will be a scanner that allows for user inputs after each println statement.
public static void Real() {
String[] sequence;
sequence = new String[4];
sequence[0] = ("first");
sequence[1] = ("second");
sequence[2] = ("third");
sequence[3] = ("fourth");
int number;
for (number = 0; number <= sequence(); number++ ) {
System.out.println("Input your " + sequence + " lap time");
}
}
A couple of syntax errors present in your code, also, method name in java should start with a lowerase.
Code below should do the work.
public static void real() {
String[] sequence;
sequence = new String[4];
sequence[0] = "first";
sequence[1] = "second";
sequence[2] = "third";
sequence[3] = "fourth";
int number;
for (number = 0; number < sequence.length; number++ ) {
System.out.println("Input your " + sequence[number] + " lap time");
}
}
use number<sequence.length instead of number <= sequence()
and
use sequence[number] instead of sequence inside system.out.println()
public static void Real(String a[]) {
String[] sequence;
sequence = new String[4];
Scanner input= new Scanner(System.in);
sequence[0] = ("first");
sequence[1] = ("second");
sequence[2] = ("third");
sequence[3] = ("fourth");
int number;
for (number = 0; number <4 ; number++ ) {
System.out.println("Input your " + sequence[number] + " lap time");
System.out.println("Input");
variable = inupt.nextDatatype();
}
}
this is the answer after what i understood.
i don't know about this by the way sequence();
You can't print an array just by referring to its name in a print statement, you need to loop through all the elements and print element at each index. You access an element using the syntax array[index].
You need to loop from 0 to the last index, I think what you're trying to get from sequence() is the size of the array. To get the size of array you need to use array.length
So your code should look like this :
public static void Real() {
String[] sequence;
sequence = new String[4];
sequence[0] = ("first");
sequence[1] = ("second");
sequence[2] = ("third");
sequence[3] = ("fourth");
int number;
for (number = 0; number < sequence.length; number++) {
System.out.println("Input your " + sequence[number] + " lap time"); } }
How about using for-each loop?
for (String s : sequence)
System.out.println("Input your " + s + " lap time");

How to integrate a loop in my code

I am new to Java. My program first gets inputs from the user about their car, and then it shows the result.
I need to integrate my "Rövarspråk" in to the code, but I am not really sure how.
If the user owns a "Saab" or a "Volvo" the "rövarspråk" loop should change the user's "string name".
If something is unclear, just tell me and I'll try to explain better.
Thanks in advance.
public static void main(String[] args) {
String lookSaab;
String consonantsx;
String input;
String slang;
String add;
// String
int length;
// int
Scanner skriv;
// Scanner
String reg;
String year;
String brand;
String name;
String car;
String when;
String small;
String medium;
String big;
// String
int mod;
int randomNumber;
int quota;
int denominator;
// int
reg = JOptionPane.showInputDialog("Ange registreringsnummer"); // Input plate number of your car
year = JOptionPane.showInputDialog("Ange årsmodell"); // Input model year of the car
mod = Integer.parseInt(year);
brand = JOptionPane.showInputDialog("Ange bilmärke"); //Input car brand
name = JOptionPane.showInputDialog("Ange ägare "
+ "(för - och efternamn)"); //Input owner of the car first name + last name
car = brand + reg;
Date date = new Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEE MMM dd");
when = sdf.format(date);
denominator = 1500;
randomNumber = 1500 + (int)(Math.random() * ((40000 - 1500) + 1));
quota = randomNumber / denominator;
small = "Liten service";
medium = "Medium service";
big = "Stor service";
if (randomNumber <= 8000){
JOptionPane.showMessageDialog(null, small, "Typ av service", 1);
} else if ( randomNumber <= 20000){
JOptionPane.showMessageDialog(null, medium, "Typ av service", 1);
} else {
JOptionPane.showMessageDialog(null, big, "Typ av service", 1);
}
String resultat = "Bil: " + car + "\n"
+ "Årsmodell: " + mod + "\n"
+ "Ägare: " + name + "\n"
+ "Mästarställning: " + randomNumber + "\n"
+ "Inlämnad: " + when + "\n"
+ "Klar om: " + quota + " dagar";
JOptionPane.showMessageDialog(null, resultat, "Resulat", 1);
lookSaab = "Saab";
if (brand.equals(lookSaab)){
}
/* Rövarspråket */
consonantsx = "bBcCdDeEfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"; //Saves all consonants to string
char consonants[] = consonantsx.toCharArray(); //String to charr
System.out.println("Mata in en mening");
skriv = new Scanner(System.in);
input = skriv.nextLine(); //Saves the input
length = input.length(); //Length inc. space
char array[] = input.toCharArray(); // Input to a char array
slang = "";
System.out.println("På rövarspråk:");
for(int i = 0; i<length; i++) {
for(int x = 0; x<20; x++){
if(array[i] == consonants[x])
{
add = array[i]+"o"+array[i];
slang = slang + add;
break;
}
else{
}
}
}
System.out.println(slang);
}
}
OK so as mentioned a good start would be to put your RoverSpraket translator into its own method:
public String rovarSpraket(String normalString) {
final String consonantsx = "bBcCdDeEfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ";
char consonants[] = consonantsx.toCharArray(); // String to charr
int length = normalString.length(); // Length inc. space
char array[] = normalString.toCharArray(); // Input to a char array
String slang = "";
System.out.println("På rövarspråk:");
for (int i = 0; i < length; i++) {
for (int x = 0; x < 20; x++) {
if (array[i] == consonants[x]) {
String add = array[i] + "o" + array[i];
slang = slang + add;
break;
} else {
}
}
}
return slang;
}
This method takes a "normal" String as input and returns the Rövarspråk version of it.
Given that it can be used anywhere you want now, like:
/i named my class "Goran" ;)
Goran goran = new Goran(); //instatiate a class object
String hello = "hello world";
System.out.println(goran.rovarSpraket(hello)); //use class object method "roverSpraket"
This will print as the following on the console:
På rövarspråk:
hoheoelollolloldod
Only thing left to do is use this in the remaining code. I guess what you want is that:
if (brand.equals("Saab") || brand.equals("Volvo")){
name = rovarSpraket(name); //translate if brand is Saab or Volvo
}
And a working example for calling the method (one way to do it)
public class Goran {
public static void main(String[] args) {
String brand;
String name;
//i named my class "Goran" ;)
Goran goran = new Goran(); //instatiate a class object
String hello = "hello world";
System.out.println(goran.rovarSpraket(hello)); //use class object method "roverSpraket"
brand = "Saab";
name = "henry";
if (brand.equals("Saab") || brand.equals("Volvo")){
name = goran.rovarSpraket(name); //translate if brand is Saab or Volvo
}
System.out.println("new name is " + name);
}
public String rovarSpraket(String normalString) {
final String consonantsx = "bBcCdDeEfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ";
char consonants[] = consonantsx.toCharArray(); // String to charr
int length = normalString.length(); // Length inc. space
char array[] = normalString.toCharArray(); // Input to a char array
String slang = "";
System.out.println("På rövarspråk:");
for (int i = 0; i < length; i++) {
for (int x = 0; x < 20; x++) {
if (array[i] == consonants[x]) {
String add = array[i] + "o" + array[i];
slang = slang + add;
break;
} else {
}
}
}
return slang;
}
}
Hope this helps ^^

Getting values from a text file then having them placed into a list with no duplicates?

What I have is a program that will read a text file that has data from a file that is in the format
`FootballTeamName1 : FootballTeamName2 : FootballTeam1Score : FootballTeam2Score
Currently it reads in the file and will split it at each colon what I want to know is how would I make it so that each time it comes across a name then it will add that name to the list if it doesn't already exist or if it does then it will not create a duplicate value rather it will add the values to the values that already exist for that teams name.
I currently have a program that can get the values for when you search for a team but what I want to do is make it so that it is possible when the user does not specify a team then it will make it so that it will return the results for all of the teams. Here is what I have currently which asks for the location of the file and asks the user for specifying a file which works but I'm not sure how to do what I want.
import java.awt.Desktop;
import java.io.*;
import java.util.*;
public class reader {
//used to count the number of invalid and valid matches
public static boolean verifyFormat(String[] words) {
boolean valid = true;
if (words.length != 4) {
valid = false;
} else if (words[0].isEmpty() || words[0].matches("\\s+")) {
valid = false;
} else if ( words[1].isEmpty() || words[1].matches("\\s+")) {
valid = false;
}
return valid && isInteger(words[2]) && isInteger(words[3]);}
//checks to see that the number of items in the file are equal to the four needed and the last 2 are integers
//also checks to make sure that there are no results that are just whitespace
public static boolean isInteger( String input ) {
try {
Integer.parseInt( input );
return true;
}
catch( Exception e ) {
return false;
}
}
//checks to make sure that the data is an integer
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while(true){ //Runs until it is specified to break
Scanner scanner = new Scanner(System.in);
System.out.println("Enter filename");
String UserFile = sc.nextLine();
File file = new File(UserFile);
if(!file.exists()) {
continue;
}
if(UserFile != null && !UserFile.isEmpty()){
System.out.println("Do you want to generate plain (T)ext or (H)TML");
String input = scanner.nextLine();
if ( input.equalsIgnoreCase("H") ) {
processFile(UserFile) ; }
else if ( input.equalsIgnoreCase("T")){
processFile(UserFile);
}
else{
System.out.println("Do you want to generate plain (T)ext or (H)TML");
}
}
}
}
//checks how the user wants the file to be displayed
private static void processFile(String UserFile) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
int totgoals = 0;
int gamesplayed = 0;
int gs = 0;
int gc = 0;
int w = 0;
int d = 0;
int l = 0;
String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.
Scanner s = new Scanner(new BufferedReader(
new FileReader(UserFile))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the name of the team you want the results for");
String input = scanner.nextLine();
while (s.hasNext()) {
String line = s.nextLine();
String[] words = line.split("\\s*:\\s*");
//splits the file at colons
if(verifyFormat(words)) {
hteam = words[0]; // read the home team
ateam = words[1]; // read the away team
hscore = Integer.parseInt(words[2]); //read the home team score
totgoals = totgoals + hscore;
ascore = Integer.parseInt(words[3]); //read the away team score
totgoals = totgoals + ascore;
if ( input.equalsIgnoreCase(hteam)){
gamesplayed = gamesplayed + 1;
gs = gs + hscore;
gc = gc + ascore;
if (hscore > ascore)
w = w + 1;
else if (ascore > hscore)
l = l + 1;
else
d = d + 1;
}
else if (input.equalsIgnoreCase(ateam)){
gamesplayed = gamesplayed + 1;
gs = gs + ascore;
gc = gc + hscore;
if (hscore < ascore)
w = w + 1;
else if (ascore < hscore)
l = l + 1;
else
d = d + 1;
}
}
}
System.out.println(input + newLine + "--------------------------" + newLine + "Games played: " + gamesplayed + newLine + "Games Won: " + w + newLine + "Games Drawn: " + d + newLine + "Games Lost: " + l + newLine + "Goals For: " + gs + newLine + "Goals Against: " + gc);
}
}
If you want no duplicate elements in your collection use a Set. A Set is a collection that contains no duplicate elements (doc here).
Sounds like you want a Map from Team to Score, where if the team already exists, then get the value from the map, and and the score to that.
You can add the entries into a Set (or if you want them sorted - SortedSet). Since Set only holds unique values - you'll always have only one "copy" of each value.
For reference, the following code uses 5 values, but the set will have only 3 (unique) values:
String str = "A : B : C : B : A";
String[] vals = str.split(":");
SortedSet<String> myVals = new TreeSet<String>();
for(String val : vals)
{
myVals.add(val.trim());
}
System.out.println("Set size: " + myVals.size());

Categories

Resources