0
First Character upper case
Please Help me in writing this program Write a program to insert name of 5 person using class and print on those names whose First letter is in uppercase
7 Antworten
+ 4
Some Problems:
* As @Visph brought up, you made the method "get" throw an exception. This must be handled.
Fix:
When calling the method you must handle the exception that was thrown:
This code would be in the main method:
try{
obj.get();
}catch(IOException e){}
I recommend just doing the try catch when you take input instead. That's likely why visph was giving you that tutorial encase you are confused.
* In the func() method, you missed round brackets around the condition. if(condition)
Fix:
Instead of the String and the condition you wrote in the if statement, it may be easier to do this (and more optimal).
char firstChar = nm.charAt(0);
if(firstChar >= 'A' && firstChar <= 'Z')
return.... etc
OR, if you want to stick with what you had:
if(nm.charAt(0) == upr.charAt(0))
return.... etc
Notice* The c in charAt() is LOWERCASE. What you wrote would give a compile error.
However, I think it would make more sense for this method to be a boolean. All it does is check whether or not the string has a capital letter in the front. I suggest making it return a boolean, then return true if the if statement is true, else false.
* In the func() method the method doesn't return anything if the if statement is false. This will not compile.
To fix this outside the if statement at the end of the method you can return null. So if the method is null, the first letter is not capital. Or, as I stated before, make it return false instead.
+ 6
Specify at least the targeted language... even if homework has no interest to be done by others: it's so much better (for you) to make your own attempt, and ask specific help for the part you stuck on ;P
+ 3
Just create the class 5 times and set the names.
Then test if the first character is uppercase in each, if it is print it.
+ 3
I'm not expert at all in Java, but it seems that you missuse exception handling (and specifically the "throws" keyword)... So I advise you to dive into some reference to it...
Among result of a google search "java exception', you can find this link:
https://www.tutorialspoint.com/java/java_exceptions.htm
;)
+ 3
My previous post was to long, this is an extention.
* This program might not work the way you wanted it to.
The question said "print on those names whose First Letter is uppercase". That could imply 'do not print those who aren't'. The reason I'm saying "might not work the way you wanted" is because you are still printing those who don't have a capital letter first. You aren't printing their names, but you are printing something (null). (Assuming it doesn't give a nullException).
To fix this, in the "show" method, first check what the func() method returns before printing. That way you can print something like "There is no caps in first character", or nothing at all.
Fix:
In the "show" method:
If you made the func() method a boolean:
if(func())
System.out println("name is"+nm);
If you didn't make func() a boolean:
if(func() != null)
System.out.println("name is"+nm);
(Just print nm, not the method, there's no point in re-running the method in this case).
* Lastly in the func() method right after the return statement you put a colon ':', it's supposed to be a semi-colon. ';'
You also never left a space between what you are returning and the return keyword.
Fix:
return nm;
OR, if you made it a boolean like I suggested:
return true;
0
Something like this?
https://code.sololearn.com/cTvBJ3UEi4yT/#java
- 1
please make corrections
import java. io. *;
class Name
{private String nm;
public void get() throws IOException
{BufferedReader input =new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter name");
nm=input.readLine();
}
public String Func()
{String upr = nm.toUpperCase():
if nm.CharAt(0) == upr. CharAt(0)
return(nm):
}
public void show()
{System.out.println("name is" +Func());}
}
public class Program
{
public static void main(String[] args) {
Name obj=new Name();
obj. get();
obj. show();
}
}