Home java How to fill the string array entered using scanner data?

How to fill the string array entered using scanner data?

Author

Date

Category

Here is the code:

scanner in = new scanner (System.in);
  ArrayList & LT; Integer & GT; List = New ArrayList & LT; & GT; ();
  if (in.next (). Equals ("Create")) {
    int n = integer.paraseint (in.next ());
    For (int i = 1; i & lt; = n; i ++) {
      List.add (i);
      if (list.size () == n) {
        Break;
      }
    }
  }
  While (! In.next (). Equals ("PrintList")) {
    String [] Arr = in.NextLine (). Split ("");
      if (arr [0] .equals ("add")) {
        if (arr.length == 3) {
        int a = integer.paraseint ("arr [1]");
        list.add (a);
      }
    }
    System.out.PrintLN (List);
  }

I solve the task on the processing of the list. In my case, first the word CREATE and N number (for example 10) are applied to the input. In the first part of the code, I fill out a list of LIST numbers from 1 to 10. There are no questions here. Next, I tried to create an ARR array, which should be filled with the following input data: the word “add” and one number. For some reason, only the number falls into the array, and the “Add” command does not fall. What should I do in order for the word to fall into an array?

just in case I attach the condition of the task , in order to make it more clear what I need to do in the end:
* There are a number of methods for the ArrayList class: add (n) – insertion, add (i, n) – insertion by index, Remove (i) – deletion by index, SET (I, N) – change the value of the index.

Lines are fed to the program input, the first of which CREATE N – creates the source list with numbers from 1 to N inclusive. Signatures of the remaining commands:

add 5 – inserts 5 to the end of the list

add 5 6 – inserts 6 by index 5

Remove 7 – Deletes 7 List Element

SET 7 3 – replaces the value of the seventh list of the list by 3

PrintList command – signals the end of the data entry end and should display the list obtained after all manipulations *

An example of input data:

create 30
Add 12.
Add 17.
Add 3 15.
REMOVE 4.
SET 12 34.
PrintList.

Answer 1

First error:
You write Arr.Length == 3 when you need to write Arr.Length == 2 (you sell add to the end here, right?).

Second, serious error:
Pay attention to the following expression:! IN.NEXT (). Equals (“PrintList”)
We take the next element from the queue and compare it with the string “PrintList”. The fact is that in the case of the “Add 2” string, we pull out the line “add” from the queue. We have not saved this line anywhere. Therefore, with this entry, if you print the contents of the Arr variable, we will get a lonely element “2”. And all because “add” in the queue is not left.

Solution:
It is enough for you to create an array of lines in which we will receive a new line in the expression, and then compare its first element with a string “PrintList”:

while (userline = in.nextline (). Split ("")) [0] .equals ( "PrintList")) {
    if (userline [0] .equals ("add")) {
      if (userline.length == 2) {
        int a = integer.paraseint (userline [1]);
        list.add (a);
      }
    }
    System.out.PrintLN (List);
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions