Looping array with while get only last value - java

I have three array
int[] image = {R.drawable.img1,R.drawable.img2} int[] sound= {R.raw.m1,R.raw.m2} String[] nom ={"el1","el2"}
I tryed to change a view with imageview, text from those array when click button but i got only the last value from three array these my method i call when onclick method
private void updateData() {
while (i<nom.length) {
ImageView.setImageResource(image[i]);
textview.setText(nom[i]);
mysong = MediaPlayer.create(Activity.this, sound[i]);
mysong.start();
i++;
}
}

Try this....
Random random = new Random();
private void updateData() {
int random_number = random.nextInt(image.length);
ImageView.setImageResource(image[random_number]);
textview.setText(nom[random_number]);
mysong = MediaPlayer.create(Activity.this, sound[random_number]);
mysong.start();
}
Note : Make Sure Your All Three Array Same Size...

Try this....
int anInt = 0;
And After OnClick
private void updateData() {
if (anInt < image.length - 1) {
anInt++;
} else {
anInt = 0;
}
ImageView.setImageResource(image[anInt]);
textview.setText(nom[anInt]);
mysong = MediaPlayer.create(Activity.this, sound[anInt]);
mysong.start();
}
Note : Make Sure Your All Three Array Same Size...

Related

"I am not able to catch NumberFormatException properly, because of this my onClick is not working

I was trying to build a simple app that takes the plain text and no of lines as input and convert the plain text into cipher text using the rail fence ciphering technique. I was taking no of lines input from user and convert that string input into integer by casting. As i was doing it, it shows NumberFormatException. I wrote the casting line inside the try block and there after the scope of that variable is limited such that my encryption() method is not able to access it. What can i do as my onClick function is not producing the correct desired cipher text?
The button behaves like it was never clicked.
I have tried creating that variable outside the try block and then typecasting it inside the block, i also made that lines variable final as it was accessed within the class. Then it asked me to initialize the variable, I have done that also but it does not seems helping me.
Button decryptBtn, encryptBtn;
TextView hlWrld, encryptedText;
EditText noOfLines, plainText;
int lines;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
decryptBtn = findViewById(R.id.decryptBtn);
encryptBtn = findViewById(R.id.encrptBtn);
hlWrld = findViewById(R.id.hlwWorld);
encryptedText = findViewById(R.id.encryptedText);
noOfLines = findViewById(R.id.lineNo);
plainText = findViewById(R.id.plntxt);
final String plntxt = plainText.getText().toString();
final String noOflines = noOfLines.getText().toString();
int lines = 0;
try {
lines = Integer.parseInt(noOflines);
} catch (NumberFormatException e) {
}
final int finalLines = lines;
encryptBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
encryption(plntxt, finalLines);
}
});
}
public void encryption(String plntxt, int lines) {
boolean checkdown = false; // check whether it is moving downward or upward
int j = 0;
int row = lines; // no of row is the no of rails entered by user
int col = plntxt.length(); //column length is the size of string
char[][] a = new char[row][col];
// we create a matrix of a of row *col size
for (int i = 0; i < col; i++) { // matrix visiting in rails order and putting the character of plaintext
if (j == 0 || j == row - 1)
checkdown = !checkdown;
a[j][i] = plntxt.charAt(i);
if (checkdown) {
j++;
} else {
j--;
}
}
// visiting the matrix in usual order to get ciphertext
for (int i = 0; i < row; i++) {
for (int k = 0; k < col; k++) {
System.out.print(a[i][k] + " ");
}
System.out.println();
}
String en = "";
System.out.println("----------------------");
for (int i = 0; i < row; i++) {
for (int k = 0; k < col; k++) {
if (a[i][k] != 0)
en = en + a[i][k];
}
}
System.out.println(en); // printing the ciphertext
encryptedText.setText(en);
}
I expect the output to be a cipher text come to me as a result of setText() method that I have applied on my textView. But, nothing is happening at all.
I was taking no of lines input from user and convert that string input into integer by casting. As i was doing it ,it shows NumberFormatException.
That is because you're trying to read a string from an EditText with no input yet as integer which is not a valid number with the following code (see the comment):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// your view binding code with findViewById
// ...
// here you're trying to read the EditText value,
// but no user input yet because you've only inflate it before.
final String plntxt= plainText.getText().toString();
final String noOflines= noOfLines.getText().toString();
// the noOfLines is "", an empty string.
int lines = 0;
try {
// this raise an exception because empty string is not number.
lines = Integer.parseInt(noOflines);
}
catch (NumberFormatException e){
}
...
}
I have tried creating that variable outside the try block and then typecasting it inside the block, i also made that "lines" variable final as it was accessed within the class. then it ask me to initialize the variable, i have done that also but it does not seems helping me.
What you done is make more damage to your code because your making the variable value constant. Both the value of plntxt and noOflines will always be an "", empty string. So, your following code won't work:
final String plntxt= plainText.getText().toString();
final String noOflines= noOfLines.getText().toString();
...
final int finalLines = lines;
encryptBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// this won't work because plntxt is always empty string
// and finalLines is always invalid number.
encryption(plntxt, finalLines);
}
});
A simple fix can be done by moving all the text getter to the inside of your onClick method:
encryptBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String plntxt= plainText.getText().toString();
String noOflines= noOfLines.getText().toString();
int lines = 0;
try {
lines = Integer.parseInt(noOflines);
}
catch (NumberFormatException e){
}
encryption(plntxt, finalLines);
}
});

Check if user decided correct

I'm currently working on a school project (a small android game) and so far I've written a code which generates a random equation 2 seconds after the activity InGame is launched and displays it in a textview. Another 5 seconds later, the second equation is generated and displayed in a different textview. Now the user has to decide if the second equation has a bigger result than the first one by either pressing the button bigger or smaller. If it was correct, the next equation should be displayed and it would go on like this until the user decided wrong.
Here is my code so far:
(Code for the first equation):
// Generate random equation and display it in textview
String[] operationSet = new String[]{"+", "-", "/", "*"};
String equation;
static double doubleAnswer1;
public void start1() {
Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;
List<String> operations = new ArrayList<>();
for (int i = 0; i < numOfOperations; i++) {
String operation = operationSet[random.nextInt(4)];
operations.add(operation);
}
int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < numOfNumbers; i++) {
int number = random.nextInt(10)+1;
numbers.add(number);
}
String equation = "";
for (int i = 0; i < numOfOperations; i++) {
equation += numbers.get(i);
equation += operations.get(i);
}
equation += numbers.get(numbers.size() -1);
TextView TextEquation = (TextView)findViewById(R.id.textView_first_equation);
TextEquation.setText(equation);
// Evaluate the result of the equation
double doubleAnswer1 = eval(equation);
String stringAnswer = Double.toString(doubleAnswer1);
TextView textAnswer = (TextView)findViewById(R.id.textView4);
textAnswer.setText(stringAnswer);
}
(Code for second equation (basically same as for first equation except the name of the strings and doubles are different)):
String equation2;
static double doubleAnswer2;
public void start2() {
Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;
List<String> operations = new ArrayList<>();
for (int i = 0; i < numOfOperations; i++) {
String operation = operationSet[random.nextInt(4)];
operations.add(operation);
}
int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < numOfNumbers; i++) {
int number = random.nextInt(10)+1;
numbers.add(number);
}
String equation2 = "";
for (int i = 0; i < numOfOperations; i++) {
equation2 += numbers.get(i);
equation2 += operations.get(i);
}
equation2 += numbers.get(numbers.size() -1);
TextView TextEquation = (TextView)findViewById(R.id.textView3);
TextEquation.setText(equation2);
// Evaluate the result of the equation
double doubleAnswer2 = eval(equation2);
String stringAnswer = Double.toString(doubleAnswer2);
TextView textAnswer = (TextView)findViewById(R.id.textView_result2);
textAnswer.setText(stringAnswer);
}
And here is my onCreate code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ingame);
// Display first equation 2 seconds after the activity is launched
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
start1();
}
}, 2000);
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
#Override
public void run() {
start2();
}
}, 7000);
// Check if user was right or wrong
final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
Log.v("TAG", "you are right");
} else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
Log.v("TAG", "you are right");
} else {
Log.v("TAG", "you are wrong");
}
}
};
buttonBigger.setOnClickListener(listener);
buttonSmaller.setOnClickListener(listener);
}
The app launches correctly and it also displays the first and second equation, but when I press one of the button, it tells me in the logcat you are wrong but I decided 100% correct. However if I debug the app, it tells me that doubleAnswer1 and doubleAnswer2 are both = 0. That's why it all ways tells me 'you are wrong'. I don't know how to fix this, maybe I need to store the doubleAnswer1 and doubleAnswer2 somewhere.
I really don't know what to do, so it would really help me if someone has an idea what to do.
If anything is unclear in my question, feel free to ask and I will try to clarify the problem.
Thank you in advance for your help!
I think your problem lies here:
double doubleAnswer1 = eval(equation);
I did a quick internet search and did not find any native function called eval(). Instead you should look into Script Engine Manager for java:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));
or exp4j which is shown here:
Executing math equation in Android
Edit:
change the following:
double doubleAnswer1 = eval(equation);
to:
doubleAnswer1 = eval(equation);
Similarly do the same for doubleAnswer2

Make Button Array Invisible(Make an hint)

I want to make a Hint button, so when I click on it, I want to delete two buttons from the list (answers list). Now I don't know how to do it,ho w to make the for loop on the button array, so I can make this buttons invisible.
public class ClassicMode extends Activity {//מהמשחק עצמו
String pic;//תמונה של הדגל
Button answer1;//תשובות
Button answer2;
Button answer3;
Button answer4;
Button hint;
TextView guess;
TextView numOfGuess;
TextView score;
TextView scorenum;
DatabaseHandler db = new DatabaseHandler(this);
String fn;
Guesses G;
Bitmap bm;
Score s;
Button [] b = new Button[4];
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
score =(TextView)findViewById(R.id.score);
scorenum =(TextView)findViewById(R.id.scorenum);
scorenum.setText(String.valueOf(s.score));
guess =(TextView)findViewById(R.id.guesses);
numOfGuess=(TextView)findViewById(R.id.numOfGuesses);
numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
hint =(Button)findViewById(R.id.hint);
Flags f = new Flags();
Random r = new Random();//הדגל שיבחר לשאלה
int num = r.nextInt(160);//Up
f = db.getFlag(num);//הצגת הדגל הרנדומלי שיצא
fn = f.getName().toString();
pic = f.getImage().toString();
pic_view(pic);//מעבר לפונקציה להשמת התמונה של הדגל במשחק
//מערך ארבע כפתורים כנגד ארבע תשובות
b[0] = (Button)findViewById(R.id.button1);
b[1] = (Button)findViewById(R.id.button2);
b[2] = (Button)findViewById(R.id.button3);
b[3] = (Button)findViewById(R.id.button4);
List<String>Answers=new ArrayList<String>();//מערך תשובות
Answers.add(f.getName().toString());//הוספת התשובה הנכונה
for(int i=1;i<4;i++)
{
num = r.nextInt(200);
String valToAdd1 = db.getFlag(num).getName().toString();
if(!Answers.contains(valToAdd1)){
Answers.add(valToAdd1);
}
}
/*num = r.nextInt(30);
Answers.add(db.getFlag(num).getName().toString());//הוספת 3 תשובות רנדומליות
num = r.nextInt(30);
Answers.add(db.getFlag(num).getName().toString());
num = r.nextInt(30);
Answers.add(db.getFlag(num).getName().toString());*/
Collections.shuffle(Answers);//ערבוב התשובות
for(int i=0;i<Answers.size();i++)
{
b[i].setText(Answers.get(i));//השמת התשובות מהמהערך למערך הכפתורים
}
}//end of OnCreat
Now what I've done (there is the function check, which check if you answered correctly and the hint which I don't know how to make):
public void check(View v)
{
Log.d("yes", fn);
Button b = (Button)v;
String text = b.getText().toString();
if(text.equals(fn))
{
s.score+=5;
resetQuiz();
}
else
{
s.score-=5;
if(Guesses.numOfGuesses==1)
{
G.setNumOfGuesses(3);
finish();//כאשר מספר הניחושים
return;
}
Guesses.numOfGuesses--;
numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
}
}
public void hint(View v)
{
G.numOfGuesses--;
for(int i=0;i<2;i++)
for(int j=0;j<4;j++)
{
if()
}
}
Note: this is {mostly} pseudocode
I suggest keeping two separate lists of your answers. Your Flag object already holds the correct answer. You need a list to keep track of the wrong answers (so we don't have to loop and check against each item every time). You also need a list of all of them together that you can shuffle and display.
I took a little bit of liberty making your variable names longer so they are more clear.
onCreate() {
...
btnHint.setOnClickListener(hintOnClickListener);
...
Flag f = db.getFlag(randomNum); // This is the real question & answer
List<String> wrongAnswers = new ArrayList<String>(3);
List<String> allAnswers = new ArrayList<String>(4);
// Loop 3 times for 3 random wrong answers
for (int i=0; i<=3; i++) {
randNum = r.nextInt(200);
String randWrongAnswer = db.getFlag(randNum).getName().toString();
if (! wrongAnswers.contains(randWrongAnswer)) {
wrongAnswers.add(randWrongAnswer);
}
}
allAnswers.add(f.getName().toString());
allAnswers.addAll(wrongAnswers);
Collection.shuffle(allAnswers);
...
}
I like to declare all my listeners separately further down in the code, to keep the OnCreate method clean and legible.
private OnClickListener hintOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
G.numOfGuesses--;
// Since you shuffled the 'allAnswers' before displaying to the screen,
// we can just pick the first 2 answers from wrongAnswers list
// and it will appear to be random to the user.
for (int i=0; i < buttons.length; i++) {
String buttonText = buttons[i].getText().toString();
if (buttonText.equals(wrongAnswers.get(0))
|| buttonText.equals(wrongAnswers.get(1))) {
buttons[i].setVisibility(View.INVISIBLE);
}
}
}
};
Edit: to add hint logic based on OP's comment.

how to generate unrepeating random layouts

public class Music extends Activity {
private int [] layouts = {
R.layout.question_selector,
R.layout.question_selector2,
R.layout.question_selector3,
R.layout.queston_selector4,
R.layout.question_selector5,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int i = layouts.length;
Random r = new Random();
while (--i > 0) {
int j = r.nextInt(i + 1);
//swap values
int temp = layouts[j];
layouts[j] = layouts[i];
layouts[i] = temp;
}
setContentView(layouts[i]);
}
}
Here's what i have done so far: it works just fine, but I have notice that there are times that some layouts are shown over and over again. What I want is that when I press the button and it generates a layout randomly, the next time I'll press the button again it should not repeat the layout that had been shown before. How can I do such thing?
You can create an arrayList and then shuffle it instead of an array with Random, this will make it random but only use every item once
EDIT, code example:
ArrayList<int> mArrayList = new ArrayList<int>;
//OR ArrayList<int> mArrayList = new ArrayList<int>(Arrays.asList(mOrdinaryArray));
mArrayList.put(R.blabla.blabla);
mArrayList.put(R.blabla.blablatwo);
Collections.shuffle(mArrayList);
Log.d(TAG, "output after shuffle: " + mArrayList);

In Java/Swing, is there a way to legally "attempt to mutate in notification"?

I was wondering if there is some sort of magic I can use to get around an IllegalStateException and allow a JTextField to "attempt to mutate in notification", or in other words to set its own text if its listener is triggered.
For your information, I am trying to program an auto-complete function which returns the most likely match in a range of 12 enums in response to a user's input in the JTextField.
Here is the code sample. You'll have to pardon my clumsy algorithm which creaks out enum results. I've highlighted the code which produces the exception with a comment:
jtfElement1.addCaretListener(new CaretListener() {
#Override
public void caretUpdate(CaretEvent e) {
String s = jtfElement1.getText();
int[] attributes = new int[13];
// iterate through each enum
for (BaseEnumAttributes b: BaseEnumAttributes.values()) {
// iterate through the length of the current text in jtfElement1
for (int i = 0; i < s.length(); i++) {
if (s.length() <= b.toString().length()) {
if (b.toString().charAt(i) == s.charAt(i)) {
// increase the number of "hits" noted for that enum
attributes[b.ordinal()] = attributes[b.ordinal()] + 1;
}
}
}
}
int priorC = 0;
int rightC = 0;
// iterate through the "array" of enums to find the highest score
for (int j = 0; j < attributes.length; j++) {
if (attributes[j] > priorC) {
priorC = attributes[j];
rightC = j;
}
}
if (!s.equals("")) {
// assign to b the Enum corresponding to the "array" with highest score
BaseEnumAttributes b = BaseEnumAttributes.values()[rightC];
iController.updateInputElement1String(b.toString());
// THIS TRIGGERS EXCEPTION
jtfElement1.setText(b.toString());
}
}
});
You are probably better off using a document filter or a custom document.
What are other listeners expected to see if the document doesn't stay the same during event dispatch?
Use SwingUtilities.invokeLater() placing all the modifications there
Maybe you can delay the setText() with a Thread to run after caretUpdate() has terminated.
i'm found on the same problem but i found an easy solution:
lock the caretUpdate() by a boolean if(false) while u'r setting the text to the jTextField than unlock it after . . something like this:
boolean caret = true;
private void listValueChanged(javax.swing.event.ListSelectionEvent evt) {
caret = false;
name.setText((String)list.getSelectedValue());
caret = true;
}
private void nameCaretUpdate(javax.swing.event.CaretEvent evt) {
if(caret){
model = new DefaultListModel();
this.fillList(name.getText());
list.setModel(model);
}
}
Create a custom Document and override insertString( )
filenameText = new JTextField(new FilenameDocument(), "", 0);
...
/**
* document which adds .xml extension if not specified
*
*/
private class FilenameDocument extends PlainDocument {
#Override
public void insertString(int offset, String insertedText, AttributeSet set)
throws BadLocationException {
if (offset == 0) {
insertedText = insertedText.trim( );
}
super.insertString(offset, insertedText, set);
if (filenameText != null) {
final int caretPos = filenameText.getCaretPosition();
String text = filenameText.getText().trim();
if (text.indexOf('.') == -1) {
filenameText.setText(text + ".xml");
filenameText.setCaretPosition(caretPos);
}
}
}
}
Note that calling setText will result in a recursive call to insertString( ), so make sure you have a stopping condition.
I'm surprised no one has answered this, but would'nt you have been better off implementing an editable JSpinner with a SpinnerListModel?

Categories