Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String help!  RSS feed

 
Justine Trudeau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright so I just began taking a class learning about entry level java programming, and so far decently good but I have run into a problem writing a homework program. In my head this program seems to work. However in eclipse it keeps telling that i can't convert this char to a string. I have to follow these instructions:

copySubsequence(): this method accepts a String s and two ints a and b as parameters. The
method returns a new String that contains all the characters to the right of position a and left of
position b in s, inclusively. For example, if s = abcdef, a = 0, b = 3, then the method will return
abcd.

Note: that the characters in a String are numbered starting at 0. If a String has n characters, the
last character is at position n-1. If you try to access a String character at position 􀀀5, say, your
program will crash. Similarly, trying to access a String character at position n will also cause the
program to crash.

The copySubsequence() method addresses \bad" values of a and b as follows:
{ If a (or b) is less than 0, its value is changed to 0.
{ If a (or b) is greater than or equal to the length n of s, its value is changed to n - 1.
{ If a is greater than b the method returns an empty String (since there is nothing that is both
to the right of a and to the left of b).
{ If s is empty, the method returns s.

Here's my attempt at the program

Thanks!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Justine,

Welcome to the CodeRanch!

This is clearly a homework assignment, and so we won't give you the answer, but we can be sure to help you along the way. First: To make the code easier for us to read, please UseCodeTags. I will put them in this time, but in the future please use code tags whenever you post code. Second: If you TellTheDetails - for example what error messages you get, or what behavior you get that you don't expect, we can address those issues and help you get compilable and working code. So what is wrong with the code you posted? What is the exact error message (copy and paste for best results)?
 
Justine Trudeau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply! Very understandable, I am not looking for an answer, just what I am doing wrong and some guidance in the right way. I have a feeling I am not approaching the problem in the right direction.

At line 13 it says, "Type mismatch: cannot convert from char to String"
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so the error tells you a lot:

Line 13 is:


So what is wrong with that line? The return statement is going to send the result of the expression (s.charAt(a)) as the return value for the method. The method is defined as:

The return type of the method is String, and you are returning the results of s.charAt(a). We know that from API (found here: java.lang.String) that charAt() returns a char (single, primitive character) not a String. So it sort of makes sense that the compiler is complaining - the method needs to return a String, but instead you are returning a char, and there is no automated way of converting the char to a String.

At this point you should be thinking - "But that's not what I want to do!" because you don't want to return the character at position a. What you really want to do is take the character at position a and copy it to a new String, and return that new String.
 
Campbell Ritchie
Marshal
Posts: 55734
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing is that the code terminates at the return, so the a++ after it is never reached.
And never use == to compare reference types for equality. Use the equals() method, or better still, go through the String class and see whether there are any methods which tell you whether or not the String is empty.
 
Justine Trudeau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Steve that was really helpful, I was under the assumption that it could have "automatically" converted the char into a String!
Now the string statement that I will make, will it be inside the while loop? or outside? Also would performing this task be easier using a "for" loop?

Steve Luke wrote:Okay, so the error tells you a lot:

Line 13 is:


So what is wrong with that line? The return statement is going to send the result of the expression (s.charAt(a)) as the return value for the method. The method is defined as:

The return type of the method is String, and you are returning the results of s.charAt(a). We know that from API (found here: java.lang.String) that charAt() returns a char (single, primitive character) not a String. So it sort of makes sense that the compiler is complaining - the method needs to return a String, but instead you are returning a char, and there is no automated way of converting the char to a String.

At this point you should be thinking - "But that's not what I want to do!" because you don't want to return the character at position a. What you really want to do is take the character at position a and copy it to a new String, and return that new String.
 
Anupam Jain
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justine Trudeau wrote:Hey Steve that was really helpful, I was under the assumption that it could have "automatically" converted the char into a String!
Now the string statement that I will make, will it be inside the while loop? or outside? Also would performing this task be easier using a "for" loop?

Steve Luke wrote:Okay, so the error tells you a lot:

Line 13 is:



Hello Justine,

You are not making any String statement here. This program will simply return the character at the 'a'th index of the String s.

What you actually need is, declare another String variable outside the loop, initialize it with empty String and then concatenate the characters in the loop to this new String. Also move the return statement from inside of loop to outside.

As for easyness of using a particular loop... i believe that is a question more of personal choices... (Though while loop should be farely easier to use here)...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!