Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For ex: The user inputs "n" designations and we check which of them is a sales designation and display those.

Input:
3 // no. of designations
SalesManager
SalesExecutive
HRManager

Output:
Designations in Sales dept are:2
SalesManager
SalesExecutive

What I have tried:

I don't understand what to do, I took the array from the user:
public class sales {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String arr[]=new String[n];
        if(n>0){
            for(int i=0;i<n;i++){
                arr[i]=sc.nextLine();
            }
            
        }
        else{
            System.out.println("INVALID INPUT");
        }
    }
    
}
Posted
Updated 12-Jan-22 0:48am

 
Share this answer
 
Comments
Sourabh Jambale 12-Jan-22 6:06am    
But we don't want to compare strings here, we want to input some strings and then check if those strings contain the word "Sales" and print those values.
OriginalGriff 12-Jan-22 6:31am    
And what have you tried to do that so far?
Sourabh Jambale 12-Jan-22 6:36am    
I did
if (Arrays.stream(arr).anyMatch("sales"::equals)) {
                    System.out.println(arr[i]);
                }

but it doesn't show any output.
OriginalGriff 12-Jan-22 6:52am    
I'm sure it does: it gives the error "cannot find symbol" for "i" as it has no idea what you are trying to do, and you couldn't fulfil you homework requirements if it did, and you have no way to find *which* string it triggered on!

Have you considered look at the Java String contains() Method[^]
Here is the Tip for you. Some of the search complexities have been ignored intentionally.

* Build a Class "Designation"
* Designation Class should have Two Attributes. DesignationType and DesignationName
* Take Number of Designations as Input in N
* ArrayList<Designation> designationList = new ArrayList<Designation>()
* For each I in N
*    Designation design = new Designation()
*    Set design.DesignationName = Take Input
*    Set design.DesignationType = Take Input
*    designationList.Add(design)
* End For
* Input DesignationType into searchType
* For Each K in N
*     design = designationList(K)
*     IF (design.DesignationType == searchType)
*         Print design.DesignationName
*     End If
* End For
* End
 
Share this answer
 
string designation = "Sales";
int counter = 0;
string[] result = new string[5];

for (int j = 0; j < answer.Length; j++)
{
	if (answer[j].StartsWith(designation))
	{
		result[counter] = answer[j];
		counter++;
	}
}

System.out.println("Designations in Sales dept are: " + counter + "");
for (int i = 0; i < counter; i++)
{
	System.out.println(result[i]);
}
 
Share this answer
 
v2
Comments
Sourabh Jambale 12-Jan-22 9:02am    
What is the "answer" variable here?
M Imran Ansari 12-Jan-22 10:07am    
Source array, where values exists ie, SalesManager, SalesExecutive etc.
Sourabh Jambale 13-Jan-22 2:18am    
Thank you so much for the answer. But how can we avoid the negativesArraysize exception error in this code? We want to return "Invalid input" when we enter a negative value for "n" which is the array size. Here's my code:
String designation = "Sales";
        int counter = 0, n;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        String[] result = new String[n];
        if (n > 0) {
            for (int i = 0; i < n; i++) {
                result[i] = sc.next();
            }
            for (int j = 0; j < result.length; j++) {
                if (result[j].startsWith(designation)) {
                    result[counter] = result[j];
                    counter++;
                }
                
            }
            if(counter>0){
                System.out.println("Designations in Sales Department " + counter );
                
            }
            else if(counter==0){
                System.out.println("No designations in Sales department ");
            }
           
            for (int i = 0; i < counter; i++) {
                System.out.println(result[i]);
            }
        }
        else{
            System.out.println("INVALID INPUT");
        }
M Imran Ansari 13-Jan-22 4:41am    
String designation = "Sales";
int counter = 0, n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
String[] result; // Declare the array
if (n > 0)
{
result = new String[n]; // Initialize here
....
..
}
else
{
System.out.println("INVALID INPUT");
}
Sourabh Jambale 13-Jan-22 7:58am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900