1

Got this code:

            System.out.println("Introduce salary: ");
            Scanner alpha8 = new Scanner(System.in);
            String salary = alpha8.nextLine();
            int salaryNew = 0;
            if(salary .isEmpty()){
                salary = null;
            }else{
                salaryNew = Integer.parseInt(salary);
            }

How can i make this to output only 1 variable from the IF? Because if the introduced value is null (blank space from scanner, like enter) sets salary to null wich i use later on.

The thing is that i either need a "null" or an "int value".

In the "else" i cannot do this:

salary = Integer.parseInt(salary);

because id get an error, neither i can parse int into to null.

And this methodto convert dates:

public static Date changeDate(String introducedDate) throws ParseException {
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date sqlDate = sdf.parse(introducedDate);
    java.sql.Date newSqlDate= new java.sql.Date(sqlDate.getTime());
    return newSqlDate;
}

If introducedDate is null it will throw an exception, how can i change that method to return NULL if introduced date is NULL too?

3
  • Using an if statement? Commented Sep 18, 2014 at 19:21
  • if(introduced date == null){return null} like this? but there would be 2 returns in that method coz there muset be an else too Commented Sep 18, 2014 at 19:23
  • Exactly like that, yes. Commented Sep 18, 2014 at 19:24

3 Answers 3

2

Part 1

The thing is that i either need a "null" or an "int value".

If you want to have an integer value which is nullable, use the wrapper class Integer:

Integer salaryNew = null;
if (!salary.isEmpty()) {
    salaryNew = Integer.parseInt(salary);
}

Part 2

If introducedDate is null it will throw an exception, how can i change that method to return NULL if introduced date is NULL too?

You need to check the value of the parameter before you do any work on it (See @nem's answer, since he beat me to that half).

Sign up to request clarification or add additional context in comments.

7 Comments

strange, getting null pointer exception, i must set that salaryNew to an entity, like employee.setSalary(salaryNew) and getting null pinter lol. Works with date but not with salary
@Alpha2k I would expect that the salary type inside the employee is int, which as you've already discovered cannot be assigned a null value. When the compiler tries to unbox the null, it'll throw a NPE. Do you really need salaryNew to be nullable?
even tough i changed the employee to Integer salary i still get a null pointer in the prepared statement: pstmt.setInt(7, employee.getSal());
@Alpha2k what value do you expect to be written to the database is the salary is null? If the database is expecting a number, you'll need to give it one. Do you have some sort of "no salary here" error code you can enter? If so, you can check for a null parameter in setSal() and assign that error value.
yea kinda need it nullable max for example when searching with ranges. User may define lowestPrice but if highestPrice is not introduced by user is considered as null.
|
1

Check out @azurefrog's answer for your first question.

If introducedDate is null it will throw an exception, how can i change that method to return NULL if introduced date is NULL too?

public static Date changeDate(String introducedDate) throws ParseException {
    if(introducedDate == null) {  // ADD THIS CHECK
        return null;
    }
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date sqlDate = sdf.parse(introducedDate);
    java.sql.Date newSqlDate= new java.sql.Date(sqlDate.getTime());
    return newSqlDate;
}

2 Comments

Is there a way to combine our answers...? ;-)
@azurefrog i don't think so haha then again, we both got an upvote for a half-answer so I guess that is a combination haha
0

you can do -

if(salary != null && ! salary.isEmpty() )
{
     return Integer.parseInt(salary);
}else{
      return null;
}

for your second part add this if condition before doing any date format-

   if(introducedDate == null) { 
        return null;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.