Why can't I concatenate strings and integers in java?

Put your programming skills to the test in these challenges.

Why can't I concatenate strings and integers in java?

Post by ghostheadx2 on Fri Jul 31, 2015 11:31 am
([msg=89166]see Why can't I concatenate strings and integers in java?[/msg])

So, I have this for loop, at the end of which I attempt to concatenate an integer to a string to an integer, like so:
Code: Select all
      a = 0;
      for (int i = 0; i < userResponses.length; i++){
         if (userResponses[i].equals(correctAnswers[i])){
            a++;
         } return a + ("/") + correctAnswers.length;
      }
   }

It gives me an incompatible types error. That seems really weird as I thought Strings and ints could be concatenated. Here's the exact error:
Code: Select all
    TriviaTest.java:51: error: incompatible types
         } return a + ("/") + correctAnswers.length;
                            ^
      required: int
      found:    String
      1 error

From what I observe, the problem is with the return statement. Am I wrong?

:D
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: Why can't I concatenate strings and integers in java?

Post by ghost107 on Fri Jul 31, 2015 3:22 pm
([msg=89170]see Re: Why can't I concatenate strings and integers in java?[/msg])

if you put the string into parenthesis the java compiler will not parse the expression as a string concatenation but as a int addition, so in your case the type int + string+ int, where string is not a int.

Code: Select all
a + "/" + correctAnswers.length;
or
String.format("%d/%d", a, correctAnswers.length);
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: Why can't I concatenate strings and integers in java?

Post by ghostheadx2 on Fri Jul 31, 2015 5:52 pm
([msg=89174]see Re: Why can't I concatenate strings and integers in java?[/msg])

I still get an incompatible types error if I put:

Code: Select all
      a = 0;
      for (int i = 0; i < userResponses.length; i++){
         if (userResponses[i].equals(correctAnswers[i])){
            a++;
         } return String.format("%d/%d", a, correctAnswers.length);
      }


I think your idea came closer but it didn't work. I tried this also:

Code: Select all
      a = 0;
      for (int i = 0; i < userResponses.length; i++){
         if (userResponses[i].equals(correctAnswers[i])){
            a++;
         } return a + "/" + correctAnswers.length;
      }
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: Why can't I concatenate strings and integers in java?

Post by ghost107 on Sat Aug 01, 2015 12:41 am
([msg=89179]see Re: Why can't I concatenate strings and integers in java?[/msg])

Then look at the function return type (int or a string).
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: Why can't I concatenate strings and integers in java?

Post by PraetG on Sun Dec 13, 2015 7:56 am
([msg=90920]see Re: Why can't I concatenate strings and integers in java?[/msg])

Yes, that should be the problem. After you set the return type from int to String, it should work. Yet I do not really know your program, it might be more elegant to return the int and create the string in the caller method. That way you can do whatever you want with the result.
PraetG
New User
New User
 
Posts: 3
Joined: Sun Dec 13, 2015 7:45 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests