Failed to handle test case for checking invalid sentence java program

Not working :slightly_frowning_face:

Then Idk, I think my code is solid tho.

1 import java.util.*;
2 public class UniqueChar{
3     public static void main(String[] args){
4         Scanner sc=new Scanner(System.in);
5         String str="null";
6         System.out.println("Enter the sentence:");
7         str=sc.nextLine();
8         str=str.toLowerCase();
9         

10 int flag=0;
11 char[] s=str.toCharArray();
12
13 for(int i=0;i<str.length();i++){
14 char c=s[i];
15 if(Character.isDigit( c ))
16 {
17
18 System.out.println(ā€œInvalid Sentenceā€);
19 System.exit(0);
20 }
21 }
22
23 int[] count =new int[255];
24 for(int i=0;i<str.length();i++){
25 if(str.charAt(i)!=ā€™ '){
26 count[(int)str.charAt(i)]++;
27 }
28 }
29
30 char[] f=new char[255];
31 for(int i=0;i<str.length();i++){
32 char k=s[i];
33 if(k>ā€˜aā€™ && k<ā€˜zā€™){
34 if(count[(int)str.charAt(i)]==1){
35 f[i]=str.charAt(i);
36 flag++;
37 }
38 else if(k<ā€˜aā€™ && k>ā€˜zā€™){
39 flag=-1;
40 }
41 }
42 }
43
44 if(flag==0){
45 System.out.println(ā€œNo unique charactersā€);
46 }
47 else{
48 System.out.println(ā€œUnique characters:ā€);
49 for(int i=0;i<f.length;i++){
50 System.out.println(f[i]);
51 }
52 }
53 sc.close();
54 }
55 }

1 Like

Thanks :smile:

1 Like

Solution for ā€œAverage and grade calculationā€ in java please

Can you show me the question please?

Develop a smart application as Student Grade Calculator(SGC).

Create a class Student with following private attribute :

int id, String name, marks(integer array), float average and char grade. Include appropriate getters and setters methods and constructor.

public void calculateAvg() - This method should calculate average and set average mark for the current student.

public void findGrade() - This method should set the grade based on the average calculated. If the average is between 80 and 100 then, then return grade as ā€˜Oā€™, else ā€˜Aā€™ .If the student gets less than 50 in any of the subjects then return grade as ā€˜Fā€™. Using appropriate setter method set the grade to the student.

(Note : number of subject should be greater than zero, if not display as ā€˜Invalid number of subjectā€™ and get number of subject again, Assume mark for a subject should be in the range 0 - 100. If not display a message ā€œInvalid Markā€ and get the mark again)

Write a class StudentMain and write the main method.

In this class, write a method

public static Student getStudentDetails() - this method should get the input from the user for a student, create a student object with those details and return that object.

In main create studentā€™s object by invoking the getStudentDetails method. Also calculate average and grade for that student object using appropriate methods.

SGC app should get the input and display the output as specified in the snapshot:

Sample Input 1:
Enter the id:
123
Enter the name:
Tom
Enter the no of subjects:
3
Enter mark for subject 1:
95
Enter mark for subject 2:
80
Enter mark for subject 3:
75

Sample Output 1:

Id:123
Name:Tom
Average:83.33
Grade:O

Sample Input 2:

Enter the id:
123
Enter the name:
Tom
Enter the no of subjects:
0

Invalid number of subject

Enter the no of subjects:

3
Enter mark for subject 1:
75
Enter mark for subject 2:
49
Enter mark for subject 3:
90

Sample Output 2:

Id:123
Name:Tom
Average:71.33
Grade:F

working on it
i also got same hands-on, are you joining CTC?

Yes.
We can help each other to complete all hands-on

Sure
Here is the code for Student class in Average and Grade:

import java.util.*;

public class Student {
private int id;
private String name;
private int marks[];
private float average;
private char grade;

    public Student(int i, String n, int [] m){
        this.id=i;
        this.name = n;
        marks = new int[m.length];
        for(int j=0; j<m.length; j++)
             this.marks[j]= m[j];
        this.calculateAvg();
        this.findGrade();
    }
    
    public int getId(){
        return id;
    }
    
    public void setId(int id){
        this.id=id;
    }
    
    public String getName(){
        return name;
    }
    
    public void setName(String name){
        this.name=name;
    }
    
    public int[] getMarks(){
        return marks;
    }
    
    public void setMarks(int marks[]){
        this.marks=marks;
    }
    
    public float getAverage(){
        return average;
    }
    
    public void setAverage(float average){
        this.average=average;
    }
    
    public char getGrade(){
        return grade;
    }
    
    public void setGrade(char grade){
        this.grade=grade;
    }
int temp=0;
    public void calculateAvg(){
        float sum=0;
        for (int i=0; i<marks.length; i++){
            sum+=marks[i];
            if(marks[i] < 50)
                temp++;
        }
        average = sum / marks.length;
    }
    
    public void findGrade(){
        if(average >= 80 && average <= 100)
            grade ='O';
        else if(temp>0){
            grade='F';
        }
        else 
            grade='A';
    }

    public void display(){
        System.out.println("Id:"+ id);
        System.out.println("Name:"+name);
        System.out.printf("Average:%.2f",average);
        System.out.println("\nGrade:"+grade);
    }
};

And for StudentMain class:

import java.util.*;
public class StudentMain {

public static Student getStudentDetails() {
    
    System.out.println("Enter the id:");
    Scanner sc = new Scanner(System.in);
    int studntId=sc.nextInt();
    
    // Skip the newline
    sc.nextLine();
    
    System.out.println("Enter the name:");
    String studntName=sc.nextLine();
    
    System.out.println("Enter the no of subjects: ");
    int noOfSubjects = sc.nextInt();
    
    while(noOfSubjects<1) {
        System.out.println("Invalid number of subject");
        System.out.println("Enter the no of subjects: ");
        noOfSubjects = sc.nextInt();
    }
        
    int[] studntMarks;
    studntMarks = new int[noOfSubjects];
    
    for(int i=0; i<noOfSubjects; i++){
        System.out.println("Enter mark for subject " + (i+1) + ": ");
        studntMarks[i]=sc.nextInt();
        if(studntMarks[i]>100||studntMarks[i]<0){
            System.out.println("Invalid Mark");
            i--;
        }
    }
    
    Student studnt = new Student(studntId, studntName, studntMarks);
    
    
    return studnt;
}


public static void main(String args[]){
    Student studnt = getStudentDetails();
    studnt.display();
}

};

Thanks :blush:

2 Likes

Welcome

How much you have completed in hands-on section?

I need to complete 11 moreā€¦ Otherwise done with all

Only in java or completed all for SQL and WebUI as well

1 Like

Java only

I got 24 left from java

Bro please help me tooā€¦Iā€™m also trying to complete CTS hands-on

Bro please post other programs alsoā€¦Iā€™m also trying to complete CTS hans-on