Reading strings and saving them in arrays

Asked

Viewed 80 times

-1

I am trying to make a program in which vendor names have to be read and have to be stored in an array.

My biggest problem is that I don’t know what is the limit of names entered by the user. Hence I used while:

public static void main(String[] args)
{
  Scanner ler = new Scanner(System.in);
  String seller = "";
  int x = 0;
  String[] arr = new String[x];
  
  while (!vendedor.equals("end"))
  {
    System.out.print("Vendedor: ");
    vendedor = ler.next();
    
    for (int indice = 0; indice < arr.length; indice++)
    {
      arr[indice] = ler.next();
      System.out.print(arr[indice]);

    }
  }
}

Thank you!

  • 1

    translate your question so that someone can help you. Our site is Portuguese.

  • 2

    Wouldn’t it be better to create a class and pass the clients?. It has to be an array even if it’s an array?

  • 1

    @R.Galamba already translated, thank you!

  • 2

    @Igorvargas yes, it has to be an array. It’s for a college job and we’re being forced to do it in array.

  • 1

    I don’t know exactly how your teacher wants but there is Arraylist in java. Do a little research on it. In case you don’t want to use it. Give you an array inside a loop with a large number and put the names in and when the person type a number he leaves the loop.

  • If it is not possible to know the quantities of strings to be saved, array does not serve, as it is finite and needs to know its size when it is started. What you could do, which would be a pessima gambiarra by the way, is to check whether the array has reached the final Dice, and create another larger one by repopulating the smaller one in it.

  • @Articunol then it is not possible for the program user to enter the names and at the end of the program read and store these names without me having put an array limit?

  • Please reread what I explained. I didn’t mean it. You want to store an amount that you don’t know how to set indices in the array, but the array needs to be started with a size OBLIGATORY before being used to store something. If Voce does not know the size, it should use ARRAYLIST, which automatically fits according to the amount of items added.

  • @Articunol Thanks!

  • Now if the exercise requires an array without Collections, go to suggest my first comment, which is to create a mechanism that, before saving a new string, controls the size of the array, if it reaches the last Indice, creates another larger one and repopulates the data in it, adding the new string after this.

Show 5 more comments

2 answers

2

I will leave my contribution more as an alternative, because we do not know if your teacher will accept the trick with split().

I used your idea of imposing a limit based on the size of the array.

So let’s say you create, for example, an array with size 10.

If you enter only 5 names, at the end it prints only the 5 names.

If you insert 10, which is the limit in the example, the loop stops automatically and prints the 10 names.

That’s where I got it:

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner ler = new Scanner(System.in);
    int tamanho = 3;
    int indice = 0;
    String[] arr = new String[tamanho];

    while (indice != tamanho)  {
      System.out.print("Vendedor: ");
      String vendedor = ler.nextLine();

      if (vendedor.equals("end")) break;

      arr[indice++] = vendedor;
    }

    for (int i = 0; i < indice; i++)
      System.out.println(arr[i]);
  }
}

0

@Guilherme Daniel

Take everything before and then move to your array.

public class StackOverflow {

    public static String EXIT = "exit";

    public static void main(String[] args) {

        Scanner ler = new Scanner(System.in);
        StringBuilder bancoDeDados = new  StringBuilder(); 

        while (ler.hasNext()) {

            String str = ler.next();
            bancoDeDados.append(str + ",");         
            if(str.contains(EXIT)) break;           
        }

        int tamanhoDoArray = bancoDeDados.toString().split(",").length;
        String[] arr = new String[tamanhoDoArray];
        arr = bancoDeDados.toString().split(",");
    }
}
  • I don’t know if you’ve read the comments, where it says you can only use array.

  • @Articunol and now chief?

  • Gambiarra with split hein. Not to mention it will add EXIT in the array.

  • then refactor

  • The goal of the response field is to give a plausible solution. If the author has to correct the errors of the answer, then in fact you wouldn’t be helping him.

  • Yes, he wants to solve his school work and the problem is not clear whether or not to save EXIT, it is a school work and not an application, he can smoothly brush, leaves a reply ai tb and "help!". @Articunol

  • If it is not clear, do not think it is right to ask him in the comments than to give an answer, which besides containing some mistakes and tricks that can pass the wrong learning to him?

  • If you haven’t noticed, that’s exactly what I did, he didn’t answer, so I didn’t answer. I prefer to wait for clarification to give a correct and adequate answer than to answer anyway.

  • Okay Professor, happy new year.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.