-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!
translate your question so that someone can help you. Our site is Portuguese.
– R.Galamba
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?
– Igor Vargas
@R.Galamba already translated, thank you!
– Guilherme Daniel
@Igorvargas yes, it has to be an array. It’s for a college job and we’re being forced to do it in array.
– Guilherme Daniel
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.
– Igor Vargas
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.
– user28595
@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?
– Guilherme Daniel
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.
– user28595
@Articunol Thanks!
– Guilherme Daniel
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.
– user28595