+ 1

Why this program not run according to which what i want???

if i write input as like 3 6......so it show exception n if i write input 7 so it is run infinity https://code.sololearn.com/cn9rd6fyZdzR/?ref=app Arun Tomar Fata1 Err0r Helga Hans Larry HAWKEYE Gaurav Agrawal

26th Jul 2018, 4:15 PM
Harsh Agrawal
Harsh Agrawal - avatar
7 odpowiedzi
+ 1
https://code.sololearn.com/ccXWdou4hv4g/?ref=app I've updated the code. You can use charAt() method to retrieve the character at a given index of a string, so 0 will be the first one. It might also be worth checking out the Character class (easy to find on google) which provides some handy utility methods for characters (like the Integer class that you have used already to parseInt) Hope this helps :)
27th Jul 2018, 5:44 PM
Dan Walker
Dan Walker - avatar
+ 2
When you read() the char value and then do a readLine() you'll read the rest of the line you're on, which is an empty string. this can't be parsed to an int and so the error is thrown. Use an int instead of a char, changing the relevant fields in the switch etc. and change read to readLine and it will work (I tested it on my app) looks like you want to do the loop when the value is between 1 and 4 and break out of the loop for the condition you have written. an invalid input produces an infinite loop
26th Jul 2018, 6:37 PM
Dan Walker
Dan Walker - avatar
+ 2
readLine gives a string, so you'd have to parse into a character (read gives an int or byte which is easily coerced into a char). So you'd need to check if the string had size 1. I used ints because it's probably easier but you could make the change I suggested here too
27th Jul 2018, 1:59 PM
Dan Walker
Dan Walker - avatar
+ 1
https://code.sololearn.com/ccXWdou4hv4g/?ref=app I'll leave the fixing of the while loop to you :)
27th Jul 2018, 6:13 AM
Dan Walker
Dan Walker - avatar
0
Dan Walker can u give me d code????
27th Jul 2018, 12:40 AM
Harsh Agrawal
Harsh Agrawal - avatar
0
but why we dont use char data types! is der any logical error or something nd if i want to do in with char data types only so what can i do,??
27th Jul 2018, 1:14 PM
Harsh Agrawal
Harsh Agrawal - avatar
0
Dan Walker can u do it for me!!! so it wll be easy i tried many times with char data types but it show error!!!! so plzzz do it.......
27th Jul 2018, 3:49 PM
Harsh Agrawal
Harsh Agrawal - avatar