Home > Net >  I'm trying to build an Quiz App Where the user Defines the Amount of QA but the loop fails and
I'm trying to build an Quiz App Where the user Defines the Amount of QA but the loop fails and

Time:12-04

Fair Warning: I'm a student trying to build an app for our research.

So I'm Trying to build an app where the user Defines the amount of Q/A he/she wants, then the app will ask for the user to put different Q/A until it reaches the an equal amount where it will then open up a new page to make the user answer those Q/A.

The Problem is that the Loop fails to repeat at the stated amount by user making the app not being able to store let's say 10 Q/A to the JShared / Shared Preference.

Here's the loop code:

package com.prgr.quizards.canary;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatButton;

import com.google.android.material.textfield.TextInputEditText;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Objects;

public class question extends Activity {

    private HashMap<String, Object> map = new HashMap<>();
    private TextView text;
    private EditText inop;
    private TextInputEditText ans;
    private AppCompatButton btn;
    private SharedPreferences jshared2;

    private SharedPreferences jshared;

    @Override
    protected void onCreate(Bundle _savedInstanceState) {
        super.onCreate(_savedInstanceState);
        setContentView(R.layout.activity_question);
        initializedata();
        AppCompatButton btn2 = findViewById(R.id.button3);
        btn = findViewById(R.id.button);
        btn2.setOnClickListener(v -> gotoback());
        btn.setOnClickListener(view -> logicg());

    }
    public void gotoback(){
        Intent intent = new Intent(question.this, activity_home_screen.class);
        startActivity(intent);
    }

    private void initializedata(){
        jshared = getSharedPreferences("j", Activity.MODE_PRIVATE);
        jshared2 = getSharedPreferences("j2", Activity.MODE_PRIVATE);
        inop = findViewById(R.id.inop);
        ans = findViewById(R.id.ans);
        text = findViewById(R.id.text1);


    }
    private void logicg() {
        String mount = jshared.getString("amount", "");
        int amounts = Integer.parseInt(mount);

        for (int i = 1; i < amounts; i  ) {
            //String shite = Integer.toString(qloop);
            //Toast.makeText(getApplicationContext(), shite, Toast.LENGTH_SHORT).show();
            if (i == amounts) {
                Intent intent = new Intent(question.this, answerscrn.class);
                startActivity(intent);
            } else{
                map = new HashMap<>();
                map.put("answer", Objects.requireNonNull(ans.getText()).toString());
                map.put("question", inop.getText().toString());
                jshared2.edit().putString("data", new Gson().toJson(map)).commit();

            }


        }
    }
}

I tried to do a for loop if else inside the for loop and that only returns the error of Condition 'i == amounts' is always 'false' what i expect is for it to loop till it reaches the same number stated by the amounts (which is the user defined value) to open up a new page using intent.

CodePudding user response:

From what I understand, your problem it in the loop, tho I don't understand how do you ask/put diffrent questions.

i == amounts is never true because the condition in the FOR loop is: i < amounts

You can simplify your code like this:

private void logicg() {

    String mount = jshared.getString("amount", "");
    int amounts = Integer.parseInt(mount);

    for (int i = 0; i < amounts; i  ) {
        //String shite = Integer.toString(qloop);
        //Toast.makeText(getApplicationContext(), shite, Toast.LENGTH_SHORT).show();
        
        map = new HashMap<>();
        map.put("answer", Objects.requireNonNull(ans.getText()).toString());
        map.put("question", inop.getText().toString());
     
        jshared2.edit().putString("data", new Gson().toJson(map)).commit();

        
    }

    Intent intent = new Intent(question.this, answerscrn.class);
    startActivity(intent);
}

CodePudding user response:

for (int i = 1; i < amounts; i ) IF THIS WAS YOUR LOOP AND YOU TOLD THAT USE WILL SELECT THE NO OF QUESTION SO IF YOU ADD THIS LOOP THEN YOUR APP WILL STOP AT -1 STEP FOR EXAMPLE IF YOUR USER SELECT 10 AND THIS LOOP WILL END AT IT WILL END AT 9 BECAUSE YOUR APP IS STARTING AT I=1 AND I<AMOUNT (THE VALUE ENTER BY THE USE SO YOU CAN MEET THE GOAL SO YOU NEED TO CHANGE THE LOOP TO for (int i = 1; i < =amounts; i )

  • Related