• Register
0 votes
278 views

Problem :

I have written following code so far:

    Scanner scanit = new Scanner(System.in);
    String strscan = scanit.nextLine();
    int x = strscan.length();
    int y = strscan.charAt(x/2);
    System.out.println(strscan.substring(y, x-1));

Because of some reason when I am trying to run above code it is giving me the error.

Please let me know if somebody can help in resolving the issue!

I am very new to java and I only have the basic knowledge in Java. I don't know about the use of arrays and other advanced stuff. If someone can tell me how to resolve this issue in the easiest way possible that would be the great help!

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I know java quite well and so I am helping you in solving your issue.

I have gone through your code and I found that int y = str.charAt(x/2); this line of code is not required. I think you need to return just half the string from the beginning of string.

To achieve the solution please use below code.

public static void main(String[] args) {
Scanner scanit = new Scanner(System.in);
String strscan = scanit.nextLine();
int x = strscan.length();
System.out.println(strscan.substring(0, x/2)); // returns part of full string starting from index 0 to mid index
}

Output

WooHoo

Woo

9 7 4
38,600 points

Related questions

0 votes
1 answer 41 views
41 views
Problem: Please help me.I am stuck. How to return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.?
asked Mar 25 Sana8989 8.9k points
1 vote
1 answer 47 views
47 views
Problem: Hey good people, I hope you are doing good. I was trying to write a program where, given a string name, e.g. "bob", return a greeting of the form "hello bob!". I was surfing over the internet and find this question from one of the websites. This problem is beyond my understanding. I looking forward to you for a solution. Thanks.
asked Mar 22, 2020 Gavin 15.3k points
0 votes
2 answers 890 views
890 views
Problem : I am facing following issue need solution return true if the string "cat" and "dog" appear the same number of times in the given string
asked Nov 13, 2019 peterlaw 6.9k points
0 votes
1 answer 192 views
192 views
Problem : I am trying to print every string in Reverse Double Character way (e.g. string -> ggnniirrttss) import java.util.Scanner; public class ReverseDoubleCharecters { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter ... I get as follows: ss tt rr ii nn gg All double characters are printed on new lines. Anybody has the solution on this?
asked Nov 22, 2019 peterlaw 6.9k points
0 votes
1 answer 3 views
3 views
Problem: Can anybody tell me the program to sort first half of an array in ascending and second half in descending order?
asked Mar 30 Madhav96 2.3k points
0 votes
1 answer 20 views
20 views
Problem: return true if the string "cat" and "dog" appear the same number of times in the given string.
asked Feb 23 Muneeb Saadii 130k points
1 vote
1 answer 1.1K views
1.1K views
Problem: I am new in python programming and I have been doing a course on it as a new comer I tried to repeat some char in a string. I wrote a code : def Work(str): for i in range(len(str)): return i * 2 But it does not work . how can I do this?
asked Apr 3, 2020 LizzyM 6.1k points
0 votes
1 answer 4 views
4 views
Problem: Please help me with my problem. How to return second last digit of given number in java?
asked Mar 25 Sana8989 8.9k points
0 votes
1 answer 15 views
15 views
Problem: Hello Developers! I am new to the world of programming and I started learning Java at an academy. My teacher gave us an assingment in which we have to find the index of last occurrence of a character in a string. The program should return the ... those solutions. If someone has better solution then kindly explain or else please explain what substrings are and how I can use it. Thanks
asked Nov 29, 2020 Code Learner 9.2k points
0 votes
1 answer 1 view
1 view
Problem: How do i calculate the length of a line between two points?
asked 2 days ago sumaiya simi 15.8k points