• Register
0 votes
24 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,050 points

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,930 points

Related questions

0 votes
1 answer 6 views
6 views
I am trying to concatenate string and character in python program ?can anyone help me in clarifing my question i am new into this field and in my project i am learning python?
asked Oct 8 shilpa 120 points
0 votes
1 answer 8 views
8 views
My program consists of a menu that has: To register. Unsubscribe. Modify. To show. Leave. And the Person class with its constructors and methods. The Person class is something like this: Class Persona { private: char * dni; char * fullname; int age; public: Person (char ... is entered in the array but it doesn't work either. I hope you have understood me more or less and thank you very much!
asked Aug 17 game 4.6k points
0 votes
1 answer 15 views
15 views
I find myself with a problem, I am not very clear about how an array with Strings is established and how it would be done to access each String and how it would be accessed each letter of one of the String. The concept would be the following: words = ... the array was selected, I need to go through the characters of the selected string and then modify those characters. How would it be programmed?
asked Aug 11 game 4.6k points
1 vote
1 answer 17 views
17 views
Problem: Well, comparing two strings lengths in a c++ program is quite easy. I wonder if I can compare two strings c++ without using strcmp! Do you have any idea guys? Please, share with me. Thanks for your concern.
asked Apr 5 Gavin 15.3k points
0 votes
1 answer 1 view
1 view asked 4 minutes ago Mashhoodch 820 points