I have made that what is entered must be int and must be positive and now I want to add validation so that the number entered must contain 10 digits
try {
nim = Integer.parseInt(txtNIM.getText());
} catch (NumberFormatException e) {
throw new numberException();
}if (nim < 0) {
throw new PositiveException();
}
try {
nimStr=txtNIM.getText();
if(nimStr.length() !=10){
throw new numberLengthNotTenException(); //this is a custom exception that
//needs to be created outside by
//yourself
// or else you can just print something to console directly and not throw an error
}
else{
nim = Integer.parseInt(nimStr);
}
}
catch (NumberFormatException e) {
throw new numberException();
}
catch(numberLengthNotTenException e){ //add this if you want to throw custom exception
System.out.println(e);
}
if (nim < 0) {
throw new PositiveException();
}
how to create custom exception
Utilize the String#matches() method with a Regular Expression (RegEx) however, you should keep in mind that a 10 digit number can potentially exceed the Integer MAX_VALUE of 2147483647 and will throw an exception, therefore your Integer variable should be of a Long Integer data type and the numerical string be parsed as such with Long.parseLong():
String nimStrg = txtNIM.getText();
long nim;
/* The string numerical value must be no more
and no less than ten digits and can not
start with zero (0). */
if (!nimStrg.matches("^[1-9][0-9]{9}$")) {
System.out.println("Invalid Numerical Value!");
// exit mechanism here if it's required...
}
else {
nim = Long.parseLong(nimStrg);
}
try {
int count = 0;
nim = Integer.parseInt(txtNIM.getText());
} catch (NumberFormatException e) {
throw new numberException();
}if (nim < 0) {
throw new PositiveException();
}
int nim1 = nim;
for (nim1 != 0; nim1 /= 10, ++count) {
}
if (count < 10) {
throw new PositiveException();
}
count variable will count the total digits of the number
Related
I'm trying to prompt the user for input to store either integers, doubles, or strings in a Comparable ArrayList. The problem is, I don't ask them to let the program know which it is. My solution was to parse the input for integers or doubles. If both of them return false, then I know it's a string. Here's my code:
int choice2num = 0;
System.out.println("Please enter your values: (Press enter to stop) ");
int integer = 0;
double doubl = 0;
boolean hasInt = true;
boolean hasDouble = true;
do
{
try
{
tempChoice = userIn2.nextLine();
if (hasInt){
try{
integer = Integer.parseInt(tempChoice);
}catch (NumberFormatException e)
{
hasInt = false;
}
}
if (haveInt == false && hasDouble){
try {
doubl = Double.parseDouble(tempChoice);
}catch (NumberFormatException er)
{
hasDouble = false;
}
}
if (hasInt)
{
try{
integer = Integer.parseInt(tempChoice);
}catch (NumberFormatException e )
{
System.out.println("Wrong format, please try again.");
break;
}
list.add(integer);
}
if (hasDouble)
{
try {
doubl = Double.parseDouble(tempChoice);
}catch (NumberFormatException e)
{
System.out.println("Wrong format, please try again.");
break;
}
list.add(doubl);
}
if (!hasDouble && !hasInt)
{
list.add(tempChoice);
}
if (!tempChoice.equals(""))
{
choice2num = Integer.parseInt(tempChoice);
list.add(choice2num);
}
}
catch(NumberFormatException e)
{
System.out.println(e.getMessage());
}
} while (!tempChoice.equals(""));
System.out.println("List: " + list);
secondMenu();
}
It's pretty messy, I know. When I run the code in the program, and enter integers 1, 2, 3, 4, and 5, it returns this:
Wrong format, please try again.
List: [1, 1.0, 1, 2, 2.0, 2, 3, 3.0, 3, 4, 4.0, 4, 5, 5.0, 5]
What is my error here?
You need an if else block.
if (userIn2.hasNextInt()){
// process integer
obj = userIn2.nextInt();
} else if(userIn2.hasNextDouble()) {
// process double
obj = userIn2.nextDouble();
} else {
// process String
obj = userIn2.next();
}
Use this order to get integers first, then doubles and lastly strings.
For one thing, if your input is an int then hasInt and hasDouble will both be true. In that case both blocks will run and the same number will be added as both an int and a double. (You're also not resetting them anywhere. If you enter something that isn't an int, no more ints will be added at all afterwards.)
Not only that, but in this block you're parsing the input and adding to the list a third time.
if (!tempChoice.equals(""))
{
choice2num = Integer.parseInt(tempChoice);
list.add(choice2num);
}
There's quite a few more problems here (you parse the string into an int or double twice) but the immediate cause of your issue is not using else if to avoid doing mutually exclusive things.
I had some success with this site, and I hope I find more excellent programmers to assist me.
So I am at my wit's end with this code. I am very new to programming, especially exceptions. I have looked very hard through my course material and sought help, but I have been quite unsuccessful. I am trying to create an improved parser that will override another parser. It reads a .txt file with student information of it including an ID, a name, a grade, and an optional email address and optional comment as tokens in a String separated by commas. The override checks for errors in each token and throws an exception called ParserException. The exception will check the code and then return an error message if the error is unfixable.
For example, if a student puts in an AB for the grade, the exception will flag and check if the input is a valid grade (which it is) and then return, if it is not, then it will throw a ParserException, in this case
throw new ParserException(ParserException.GRADE_INVALID_GRADE,lineIndex);
This shows that the does not work and sends out a message GRADE_INVALID on the line indexLine
I have a list of what I need to have as an output:
Any violation of the file format specified in the Input File Format Description section above should result in an ParcerException with an appropriate message
Duplicate IDs are not allowed
Grade values must be a float (92.0) or a letter grade and not an integer
I have all the code to correct and check for errors, but I cannot figure out how to get the try-catch to work. Here's is the override code:
#Override
public ParserResult parseLine(int lineIndex) {
String[] tokens = lines.get(lineIndex).split(",");
ArrayList<Integer> idList = new ArrayList<Integer>();
Integer studentId;
String name;
String grade;
String email;
String comments;
boolean isFloat;
float gradeFinal;
String editName;
studentId = new Integer(tokens[0]);
ParserResult result;
try{
return super.parseLine(lineIndex);
}
catch(ParserException e){
// Check reasonable number of tokens
if(tokens.length >= 3 && tokens.length <= 5){
name = tokens[1];
grade = tokens[2];
// Check the student ID
if(idList.contains(studentId)){
throw new ParserException(ParserException.DUPLICATE_ID, lineIndex);
}else{
idList.add(studentId);
}
// Check the grade
if(grade.trim().equalsIgnoreCase("A")){
gradeFinal = gradeA;
}else if(grade.trim().equalsIgnoreCase("AB")){
gradeFinal = gradeAB;
}else if(grade.trim().equalsIgnoreCase("B")){
gradeFinal = gradeB;
}else if(grade.trim().equalsIgnoreCase("BC")){
gradeFinal = gradeBC;
}else if(grade.trim().equalsIgnoreCase("C")){
gradeFinal = gradeC;
}else if(grade.trim().equalsIgnoreCase("CD")){
gradeFinal = gradeCD;
}else if(grade.trim().equalsIgnoreCase("D")){
gradeFinal = gradeD;
}else if(grade.trim().equalsIgnoreCase("F")){
gradeFinal = gradeF;
}else{
try{
Integer.parseInt(grade);
isFloat = false;
}
catch(Exception fl) {
isFloat = true;
}
if(isFloat){
if((Float.parseFloat(grade) < 100f) && (Float.parseFloat(grade) >= 0f)){
gradeFinal = Float.parseFloat(grade);
}else{
throw new ParserException(ParserException.GRADE_INVALID_GRADE,lineIndex);
}
}else{
throw new ParserException(ParserException.GRADE_INTEGER_VALUE,lineIndex);
}
}
// Check the name
if(name.split(" ").length > 3){
throw new ParserException(ParserException.UNKNOWN, lineIndex);
}else{
editName = name.trim().split(" ")[0];
}
result = new ParserResult(studentId, editName, gradeFinal);
// Checks the email
if(tokens.length >= 4){
email = tokens[3];
// Check for at sign
if(!email.contains("#")){
throw new ParserException(ParserException.UNKNOWN, lineIndex);
}
int count = 0;
// Counts number of # symbols
for(int i=0; i<email.length(); i++){
if(email.indexOf(i) == '#'){
count++;
}
}
if(count > 1){
throw new ParserException(ParserException.EMAIL_MULTIPLE_AT,lineIndex);
}
if(email.split(".").length == 2){
if(!(email.trim().split(".")[1].contains(".edu")) && !(email.trim().split(".")[1].contains(".com"))){
throw new ParserException(ParserException.EMAIL_NOT_EDU_OR_COM,lineIndex);
}else{
result.setEmail(email);
}
}
// Checks if email contains .com or .edu
// Checks the comments
if(tokens.length == 5){
comments = tokens[4];
result.setComment(comments);
}
}
return result;
}
}
// TODO Call Parser's parseLine() here to attempt to parse, catch any exceptions
return null;
}
The original parseLine that is overridden, but still used is:
public ParserResult parseLine(int lineIndex) {
String[] tokens = lines.get(lineIndex).split(",");
ParserResult result = new ParserResult(Integer.parseInt(tokens[0]),
tokens[1], Float.parseFloat(tokens[2]));
result.setEmail(tokens[3]);
return result;
}
Here is the main() file:
public static void main(String[] args){
// TODO Change the line below to use ImprovedParser
Parser parser = null;
try {
parser = new ImprovedParser(args[0]);
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
System.exit(-1);
}
List<ParserResult> results = parser.parse();
int count = results.size();
double sum = 0.0;
for (ParserResult result : results) {
sum += result.getGrade();
}
System.out.println("Number of valid input lines: " + results.size());
System.out.println("Number of invalid input lines: "
+ (parser.getLineCount() - results.size()));
System.out.println("Average grade: " + sum / count);
for (ParserResult result : results) {
System.out.println(result);
}
}
Lastly, here is the .txt file that is being read:
# student_id,name,grade,email
1234,Bob,92.0,bob#test.edu
4321,Alice,95.0,alice#test.edu
1111,Eve,80.0,eve#test.edu
1121,Barry,85.0,barrytest.edu
1131,Harry,86.0,h#rry#test.edu
1121,Larry,87.0,larry#test.edu
1141,Jim Song,88.0,jim#song.edu
1151,Jerry,77.0,jerry#test.net
1161,James,65.0,james#test.com
The last six inputs should cause exceptions, but I can't figure out how to organize it to work. The code ignores the line with # symbol.
Here is a sample successful output:
Number of valid input lines: 3
Number of invalid input lines: 0
Average grade: 89.0
1234, 92.0, Bob, bob#test.edu,
4321, 95.0, Alice, alice#test.edu,
1111, 80.0, Eve, eve#test.edu,
The major changes should be in the orverride method
Please help if you can, I sit at my desk still pondering possibilities, and your help will be most-appreciated.
Assuming ParseException has an error field being an int and someMethod() that throws ParseException:
try {
someMethod();
} catch (final ParseExeption ex) {
if (ex.getError() == ParseException.SOME_ERROR) {
// do something
} else if (ex.getError() == ParseException.OTHER_ERROR) {
// do something else
}
}
Note that it's usually better to use specific exceptions for specific error, something like SomeErrorParseException, OtherErrorParseException, ... (those can extends ParseException if you want) and try-catch like this:
try {
someMethod();
} catch (final SomeErrorParseException ex) {
// do something
} catch (final OtherErrorParseException ex) {
// do something else
}
Some reading: http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html
It seems that there is no code to actually cause the catch clause in the first place. Try adding a throw new ParserException(STUFF_HERE); when an error has been detected while reading the file.
I am creating a budget program in java and need to check to see if the user inputs a valid dollar amount, specifically 2 decimal places. There is no specification on how this should be done.
This is my current attempt but the logic is wrong
aExpense5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String desc = aExpense3.getText();
String value = aExpense4.getText();
double fValue = 0;
try
{
fValue = Double.valueOf(value);
}
catch (NumberFormatException d)
{
JOptionPane.showMessageDialog(null, "Invalid Number1");
}
double dValue = (fValue * 10) % 10;
if (dValue <= 0)
{
updateExpenseList(desc, fValue);
}
else
{
JOptionPane.showMessageDialog(null,"invalid Number");
}
}
});
You can use regex:
if (value.matches("\\d*\\.\\d\\d")) {
// the number string has two decimal places
}
This regex allows for optional whole number part, ".05" would match.
Try something like:
if (fValue*100 == (int)(fValue*100)) {
//fValue had 2 decimal places or less
}
If fValue = 0.11 then fValue*100 = 11. So you'd have 11 == (int)11 which is true.
If fValue = 0.111 then fValue*100 = 11.1. So you'd have 11.1 == (int)11.1 which is false.
This is my first time using exception handling so be gentle. I have a simple blob class that accepts an ID, the id must be between 30 and 50 or else an exception is thrown.
public class Blob {
int id;
public Blob() {
}
public Blob(int id) throws Exception {
this.id = id;
if (id < 30 || id > 50)
throw new Exception ("id = " +id+ ", must be between 30 and 50 inclusive");
}
}
It should prompt the user to enter an id, and throw the exception if it's not between 30 and 50, and should continue until the user enters a valid input and then simply displays the id number.
public class BlobCreator {
public static void main(String[] args) {
int id;
Scanner scan = new Scanner(System.in);
System.out.println("Enter ID number: ");
id = scan.nextInt();
do {
try {
Blob b = new Blob(id);
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("Enter a different ID: ");
id = scan.nextInt();
}
while(true);
}
System.out.println("Blob ID: " +id);
}
I think that I am using the throw and catch correctly, but my loop isn't working correctly so I think that should be a simple fix but I can't get it just right. Also is using a while loop like I have the best way for this situation or is there a better way to loop through throw and catch?
Thanks for any and all help
You should place the break; after the code is executed successfully.
do {
try {
Blob b = new Blob(id);
break;
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("Enter a different ID: ");
id = scan.nextInt();
} while(true);
So each time the loop would reach the end of its body, it would break out of the loop. You only should break after the blob is created successfully. Although I dont see why you put a break anyway. The while loop can check if the entered input was valid and simply stop the loop.
I modified the while in a do-while loop... By using true the loop will run forever, unless no exception is thrown by the constructor... This makes the code more generic (if you modify the conditions of the blob-construction, you don't have to modify the condition of the while loop).
Sorry, its kind of late to the party. Hope users who endup here may find this useful.
The use of break keyword is discouraged
Here is a very simple implementation to break away after implementing a retry mechanism
This iterates over the loop for the specified number of times and also if the exception still persists, then the exception is thrown. This can be leveraged for an actual real world scenario where the resttemplate might actually result in IO/Network errors and can be retried in those cases
public class TestClass {
public static void main(String[] args) throws Exception {
try {
int c = anotherM();
System.out.println("Now the value is" + c);
} catch (Exception e) {
System.out.println("Inside" + e);
}
}
public static int anotherM() throws Exception {
int i = 4;
Exception ex = null;
while (i > 0) {
try {
System.out.println("print i" + i);
throw new IOException();
// return i;
} catch (Exception e) {
System.out.println(e);
i--;
if (i == 1) {
ex = new Exception("ttt");
}
}
}
if (ex != null) {
throw new Exception("all new");
} else {
return i;
}
}
}
I am trying to parse a string to int value. But i am getting a NumberFormat Exception. I am writing the below code:
Logger.out("Myprof", "Contact "+strContact);
try{
i = Integer.parseInt(strContact.trim());
Logger.out("Myprof", "Contact8686866 "+i);
}
catch(Exception e)
{
Logger.out("Myprof", "exce "+e.toString());
}
Now when i am passing like below:
i = Integer.parseInt("11223344");
I am getting the i value as 11223344.
Where i am doing wrong here? Please Help.
The input value of 9875566521 is greater than Integer.MAX_VALUE of 2147483647. Instead use a Long. (BigInteger not an option for Blackberry)
Long number = Long.parseLong(strContact);
Logger.out("Myprof", "Contact8686866 " + number);
If the intended input numbers are greater then Long.MAX_VALUE, then Character.iDigit can be used as an alternative to validate values:
private static boolean isValidNumber(String strContact) {
for (int i = 0; i < strContact.length(); i++) {
if (!Character.isDigit(strContact.charAt(i))) {
return false;
}
}
return true;
}