• Register
0 votes
26 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 6 views
6 views
How can I do this?
asked Dec 18, 2020 TeamScript 10.3k points
0 votes
1 answer 26 views
26 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 18 views
18 views
I am new to this. I just want to know how to do that? Help me.
asked Nov 5, 2020 TeamScript 10.3k points
0 votes
1 answer 4 views
4 views
Problem Hey, I just want to split the string using string delimiter in C++. I want to use a string as a delimiter and want to split this string scott>=tiger With this sign >= as a delimiter. So I would get scott and tiger. Here’s my code using ... input="text to be parsed"; stringstream input_stringstream(input); if (getline(input_stringstream,parsed,' ')) {      // do some processing. }
asked Dec 22, 2020 chris jordan 2.2k points
0 votes
1 answer 8 views
8 views
Problem: Hello! I just came across a programming problem in which the programmer has to write a program that counts the number of the same characters in two strings. I am puzzled about how to achieve the count of the same characters. I want help from you in finding the solution. Thanks!
asked Jan 8 Code Learner 5.5k points
0 votes
1 answer 14 views
14 views
1 I'm trying to compare the first character of two different strings (and so on) to form a new string based on those results. This is what I've tried using, however its comparing every element of each list to each other. def compare(a,b): s = "" for x in a: for y in b: if x == y: s+=str(x) else: s+=str(y) It seems like such a simple question but I'm stuck.
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 8 views
8 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, 2020 shilpa 120 points
0 votes
1 answer 10 views
10 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, 2020 game 4.6k points
0 votes
1 answer 20 views
20 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, 2020 game 4.6k points
0 votes
1 answer 11 views
11 views
Problem: mutable exist, alterable don't. In Java, all strings are changeless. In your first model, you are changing the reference to the String , in this way allotting it the estimation of two different Strings joined: Why String Is Immutable? In Java, String is a final and immutable class, which makes it the most special
asked Nov 5, 2020 Mashhoodch 6.1k points