[ Šojka @ 30.06.2016. 12:12 ] @
Dobar dan,

Imam jedan problem , kako prikazati Array u TextBox-u s praznim poljima ( tj. razmacima između brojeva)?

Program generira random 6 brojeva od 1-49 i ispisuje ih u textbox pomoću tipke generate, sve radi ok al problem je što ne mogu razdvojiti brojeve u textboxu.

Evo primjer koda:



class MainActivity extends AppCompatActivity {
private TextView txtView;

private Button btnBingo;

public void checkBingo (){
int []randomNumArray= new int [6];

randomNumArray [0] = randomNumber();
String numberString= "";


System.out.println(randomNumArray[0]);
numberString+= randomNumArray[0];
txtView.setText(numberString);



for (int i = 1; i<6; i++){
int candidate;
boolean foundMatch;
do {
candidate = randomNumber();
foundMatch = false;
for (int j=0; j<1; j++)
if (candidate == randomNumArray[j])
foundMatch =true;

}while (foundMatch);
randomNumArray = candidate;
System.out.println(randomNumArray);
numberString +=randomNumArray;
txtView.setText(numberString);



}



}

public static int randomNumber () {
return ( 1 + (int) (Math.random()*49));
}



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtView = (TextView)findViewById(R.id.txtView);

btnBingo = (Button) findViewById(R.id.btnBingo);

btnBingo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

checkBingo();

}
});



Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

[ Java Beograd @ 30.06.2016. 13:07 ] @
numberString +=randomNumArray + " ";
[ Šojka @ 30.06.2016. 18:10 ] @
Desila se mala greška kad sam kopirao code ne prikazuje mi i (stavi sam upitnik ) dolje u kodu , pošto na forumu znači italic .
Pokušao sam i tvoje rješenje ali daje isti rezultat , tj. brojeve sastavljene.

Pokušao sam još par primjera sa stackoverwolfa ali još sam bez rješenja.

Evo ispravka s kodom i = ?, zbog ograničenja na forumu .

for (int i = 1; i<6; i++){
int candidate;
boolean foundMatch;
do {
candidate = randomNumber();
foundMatch = false;
for (int j=0; j<1; j++)
if (candidate == randomNumArray[j])
foundMatch =true;

}while (foundMatch);
randomNumArray = candidate;
System.out.println(randomNumArray [?]);
numberString +=randomNumArray [?];
txtView.setText(numberString);



}



}

public static int randomNumber () {
return ( 1 + (int) (Math.random()*49));
}
[ Šojka @ 30.06.2016. 18:29 ] @
Hvala na odgovoru, pronašao sam rješenje, sitnica ali morao sam mozgati jedan sat zbog nje.
Pa ako nekog zanima evo code.

Samo prebacite sebi j u i da bi radilo u gore navedenom codu.


// String sadrži bingo brojeve
String numberString = "";
for(int j =0; j < 6; j++)
{
// Ispis brojeva u log/terminal
System.out.print(numbers.get(j));
// Append the string with the current number
numberString =numberString+","+" "+ numbers.get(j) + " ";
}
// Update vrijednosti u textbox , sa svim brojevima skupa

txtView.setText(numberString);