#trying to exctract the middle character from a text string

1 messages · Page 1 of 1 (latest)

quartz dock
#

i get the right number of the position of the character but i want the program to outprint the chracter not the position

lyric oceanBOT
#

<@&987246399047479336> please have a look, thanks.

lyric oceanBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

stoic musk
#

U can get the character at that position

woven token
#

text.charAt(x)

quartz dock
#

umm when i lets say type hello i get number 3

#

but i want the "l" to outprint

#

@stoic musk

woven token
#

whats unclear to u, after getting the explanation?

#

if u need more hands-on help, u have to share ur code

woven token
#

where is the main method?

quartz dock
#

here

woven token
#

ah

#

u want ur method to return a char

#

but u wrote int

#

so java converts the char to its ascii index

quartz dock
#

i see thats why i get like 97 when i typ aaaa

woven token
#

yes

quartz dock
#

now the return statement screams at me insted

#

it wants me to change my method to a boolean?

woven token
#

u have to understand the error messages and ur code

#

now u wrote that ur method must return a char, as u wanted

#

however

#

u return a boolean now in line 30

#

which represents the case when u have entered text with an odd length

#

u return the mid char if the user entered an even text

#

but return false if the user entered an odd text

quartz dock
#

yes, i just wanted to see if that first statement worked

woven token
#

okay. but to make java happy, u must return a char in all cases

#

so return some char instead

quartz dock
#

i swapped it to "return 1"

#

just to see if the first statement worked

woven token
#

thats an int

#

return 'a';

quartz dock
#

okey

woven token
#

or throw new AssertionError("TODO");

quartz dock
#

like that?=

woven token
#

u can just write return 'a';

#

or return '_'; or whatever

quartz dock
#

that dident work xd

#

tried it first

woven token
#

theres neither a need for a variable

#

nor do chars take ints

#

u can write a char by using '...'

#

instead of "..." for strings

quartz dock
#

i dont understand why i get the wrong answer tho

#

i typ abcd and i get d?

woven token
#

okay. quick tip about debugging. add a System.out.println("Mid is: " + mid); to ur code

#

and tell us what it says

#

ull notice the issue immediately after doing that

quartz dock
#

in the main?

woven token
#

in ur middle method

#

where u compute int mid

#

u have to inspect ur method with prints to figure out whats going wrong

#

stop just staring at ur code. u wont find the bugs like that

#

u gotta inspect what ur variables are

#

print them out

quartz dock
#

if i typ hell mid will be 2

#

and i set a as mid +1 so it will be 3

woven token
#

thats what u think

#

not what the code says

#

i know what u wanted to do. but apparently it doesnt work yet

#

so u have to find the bug

#

and the easiest way to do that is by printing out ur variables

#

cause u will suddenly notice that they are not what u thought they are

#

and thats how u find the bugs

#

just put the print in there please and show me what it says

woven token
#

println not print please

woven token
#

anyways. so whats the matter now? l is the middle of hell

#

what did u want to get instead?

quartz dock
#

here is the problem

#

i get d when i typ abcd

woven token
#

okay

#

thats bc u didnt think about indices starting at 0

#

let me show u:

#
abcd
0123
#

length of abcd is 4

quartz dock
#

ohhh i seee

woven token
#

4 / 2 = 2

#

2 + 1 = 3

#

index 3 is d

quartz dock
#

3 is d

#

yeee

#

thank you!

woven token
#

so u want the third character, but thats on index 2

quartz dock
#

hahaha

#

my man! now i got more problems to fix but i will try that on my own first

#

thx for all the help

#

❤️

woven token
#

ur welcome, good luck 🙂

quartz dock
#

tyty

stoic musk
#

Imma go sleep again