• Register
0 votes
157 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!

7.5k 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
}

Output

WooHoo

Woo

36.1k points

Related questions

1 vote
1 answer 17 views
17 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.2k points
0 votes
2 answers 232 views
232 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 77 views
77 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 18 views
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 5.7k points
1 vote
1 answer 192 views
192 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 LizzyM 5.7k points