• 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

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




9 7 4
38,600 points

Related questions

1 vote
1 answer 30 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 669 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 145 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 20 views
I was working on that but I got into some problems. Solve Please.
asked Nov 5, 2020 TeamScript 9.3k points
0 votes
1 answer 10 views
I need to know how to remove the first cgaracter of a string in Java.
asked Sep 25, 2020 Daniel Anderson 4k points
0 votes
1 answer 20 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, 2020 LizzyM 6.1k points
0 votes
1 answer 4 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 5.5k points
1 vote
1 answer 703 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 380 views
Problem : I am completely stuck on this problem Given an array of ints, return True if the array contains a 2 next to a 2 somewhere. has22([1, 2, 2]) → True has22([1, 2, 1, 2]) → False has22([2, 1, 2]) → False I know the basic idea but I am unable to implement it. ... this, eg. graph, search? def has22(mynums): for l in mynums: if ( (mynums[l] = 2) and (mynums[l+1] = 2) ) return True return False
asked Dec 10, 2019 alecxe 7.5k points
0 votes
2 answers 19 views
Problem: Given the array a, write an expression that refers to the first element of the array?
asked Apr 12, 2020 ArifulIslam 5.6k points