• Register
0 votes

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

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




9 7 4
38,600 points

Related questions

1 vote
1 answer 21 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 Gavin 15.3k points
0 votes
2 answers 407 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 111 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 7 views
I need to know how to remove the first cgaracter of a string in Java.
asked Sep 25 Daniel Anderson 4k points
0 votes
1 answer 18 views
I have a presentation about capitalize the first letter of each word of a sentence I write a program like that : public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Input a Sentence: "); String line = in.nextLine(); ... AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 9 seconds) What is the error ?
asked Mar 15 LizzyM 6.1k points