8

I am getting an error in Java during compilation:

UserID.java:36: error: incompatible types
            + generator.nextInt(10);
            ^
  required: String
  found:    int

Here is the Java code:

public class UserID {

  private String firstName; 
  private String userId;  
  private String password;

  public UserID(String first) {
     Random generator = new Random();

     userId = first.substring(0, 3) + 
        + generator.nextInt(1) + 
       (generator.nextInt(7) + 3) + generator.nextInt(10);     //this works

     password = generator.nextInt(10) + generator.nextInt(10);   //Error is here

  } 
}

What is the reason for this error and how do I fix it? Why is it not automatically promoting the int to a String?

1
  • In order to place an integer into a String you have two choices: 1. Integer.toString(yourint). and 2. Add blankstring to your integer like this String mystring = "" + 25. If you don't, the compiler will let you know that you've made a mistake. Rightly so, you are putting something where it doesn't belong. Commented Jul 31, 2014 at 15:37

4 Answers 4

9

On the password line, you're adding Integers(When you want to be concatenating them) and putting it into a string without an explicit cast.You'll have to use Integer.toString()

So like this

password = Integer.toString(generator.nextInt(10) + generator.nextInt(10)
        + generator.nextInt(10) + generator.nextInt(10)
        + generator.nextInt(10) + generator.nextInt(10));

The reason it works in username is because you have Strings being added to integers the put into a String, so it's implicitly casting it to a String when concatinating.

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

2 Comments

I Suspect the OP is trying to concatenate multiple ints as strings, rather than add them together then convert the sum to a single string. So he should be wrapping each generator.nextInt(X) in Integer.toString rather than the sum of them all.
Yes. I agree that his goal is concatination, but in it's current state it's adding them. The solution is either Integer.toString() to to add a "" + before the whole thing.
2

Better way is to use StringBuilder,

StringBuilder sb=new StringBuilder();
sb.append(first.substring(0, 3));
sb.append(last.substring(0, 3));
sb.append(generator.nextInt(1));
sb.append(generator.nextInt(7) + 3);
sb.append(generator.nextInt(10));

userId=sb.toString();

Comments

1

Easy fix is to add "" first, e.g.:

password = "" + generator.nextInt(10) ...

Comments

0

Look at the return type of generator.nextInt() it returns an int but you're trying to assign it to a String that's what it says: incompatible type you can't assign an int to a String.

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.