• Register
0 votes
28 views

I am working on a simple program where I need to split strings, the requirement is as follows:

 prob 1 : Program will continue while the user use "a" to quit .
  
  prob 2 : Get input until valid string "a" is entered .

  prob 3 : Get two string from user and seperate it where user input comma or Space.
 

     Scanner scnr = new Scanner(System.in);
     String userInput = " ";
     boolean inputDone = false;

      while (!inputDone) {
         System.out.print("Enter input string: \n");
         userInput = scnr.nextLine();

         if (userInput.equals("a")){
            System.out.println("First word: " + userInput);
            inputDone = true;
         } else  {
                              
            System.out.println("First word: " + userArray[0]);
            System.out.println("Second word: " + userArray[1]);
            
            
            System.out.println();
        }
      }

      return;


   I have wrote this program and i dint not find what happened
   and it shows error, what should i do now ?
Thanks in advance!

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes

Everything looks okay in this program. but one thing you have forget that you have to split the string by using comma or space. java splits the strings into two words and then prints it.Here is an example:
      

      Scanner scnr = new Scanner(System.in);
      String userInput = " ";
      boolean inputDone = false; 

      while (!inputDone) {
         System.out.print("Enter input string: \n");
         userInput = scnr.nextLine();


         if (userInput.equals("q")){
            System.out.println("First word: " + userInput);
            inputDone = true; 
         } else  {
             
            String[] userArray = userInput.split(",");                  
            System.out.println("First word: " + userArray[0]);
            System.out.println("Second word: " + userArray[1]);
            
            
            System.out.println();
        }
      }


      return;


    Note: Remember that understanding the code is a very important
    process in learning a programming language.

11 6 4
34,950 points

Related questions

0 votes
1 answer 13 views
13 views
How can I do this?
asked Dec 18, 2020 TeamScript 13.5k points
0 votes
1 answer 2 views
2 views
Problem: I need assistance in resolving the following issue: how to sort an array of strings in java
asked 2 days ago tuhin1 48.4k points
0 votes
1 answer 2 views
2 views
Problem I want to know if is it possible to Store a String variable on a String array? I wonder if someone could help me to
asked 3 days ago neeraj 9.5k points
0 votes
1 answer 6 views
6 views
Problem: I'm having problems during my learning as I mentioned in my question above, I try to find on web search but I can't get proper information, can you help me to continue my project?
asked Mar 23 rubelkhan 20.4k points
0 votes
1 answer 7 views
7 views
Problem: My string contains numbers and I want to return the number it represents.
asked Feb 25 Ashty 5.7k points
0 votes
1 answer 48 views
48 views
Problem: Is it possilble to read the text file into array of strings in java? If it is possible than suggest me proper way of doing it.
asked Nov 4, 2020 Mubashi-Question 1.7k points
0 votes
1 answer 8 views
8 views
problem; I need to split my String by spaces. For this I tried: str = "Hello I'm your String"; String[] splited = str.split(" "); But it doesn't seem to work.
asked Feb 7 kaash_kashi 1.2k points
0 votes
1 answer 19 views
19 views
I am new to this. I just want to know how to do that? Help me.
asked Nov 5, 2020 TeamScript 13.5k points
0 votes
1 answer 21 views
21 views
Problem: How can I solve this? help me.
asked Feb 19 TeamScript 13.5k points
0 votes
1 answer 11 views
11 views
Problem: read a text file line by line and split words in java
asked Feb 24 Hira 2.2k points