3

I have just started to use android studio recently and currently I am working on a Roman Numeral Translator app. The app's interface looks like this: Application interface

The user will use the keypad to input a integer which will show up on the TextView shown above. When they hit the convert button it will take the integer they have entered and convert it (the program will be able to catch the input if contains a string or character). Then the app will reset the TextView to the result after the user hits the "convert" button.

Currently, my main activity contains the onClickListeners for the buttons and a separate translator method for the translations. My problem is with the "convert" button I'm not sure how to get the input from the translator method and set it as TextView when it finishes converting. Here is the sample of my code:

"Convert" button listener- `

convert.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View v) {
                        TextView numeralInput = (TextView) findViewById(R.id.textView);
                        String intValue = numeralInput.getText().toString();
                        try{
                            int integer = Integer.parseInt(intValue);
                            if (integer > 0 && integer <= 4999){
                                translator(integer);

                            }else{
                                numeralInput.setText("Please enter an integer between 0 and 4,999.");
                            }

                        }catch(NumberFormatException e){
                            numeralInput.setText("Invalid input try again.");
                        }
                    }
                }
        );

`

Translator Method- `

public static void translator(int integer) {
        LinkedList<String> stack = new LinkedList<String>();
        // if (integer > 0 && integer <= 4999) {
        //ArrayList<Integer> placement = new ArrayList<Integer>();
        int place = (int) Math.log10(integer);
        for (int i = 0; i <= place; i++) {
            //while(integer > 0){
            //System.out.println(integer);
            int placeOfValue = integer % 10;
            //stack.push(placeOfValue);
            //System.out.print(stack);

            //System.out.print(placeOfValue +":" + i);
            String placement = "";
            switch (i) {
                case 0:
                    placement = ones(placeOfValue);

                    break;
                case 1:
                    placement = tens(placeOfValue);

                    break;
                case 2:
                    placement = hundreds(placeOfValue);

                    break;
                case 3:
                    placement = thousands(placeOfValue);

                    break;
                default:
                    break;
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                stack.push(placement);
            }
            integer = integer / 10;

            //System.out.print(placement);
            // System.out.println(placement.size());
            //}
//              for(int j = 0; j < placement.size(); j++){
//                                    double tenthPower = Math.floor(Math.log10(placement.get(j)));
//                                    double place = Math.pow(10, tenthPower);
//                                    System.out.println(place);
//
//              }
            // }
            while (!stack.isEmpty()) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                    System.out.print(stack.pop());
                }
            }
//        } else {
//            System.out.println("Please enter an integer between 0 and 4,999.");
//        }

        }
    }

`

The other methods inside translator is like a library for the roman numerals each containing a numeral for each of the place values as shown below.

Thousands method- `

public static String thousands(int integer) {
        String thouValue = "";
        switch (integer) {

            case 1:
                thouValue = "M";
                //System.out.print("M");
                break;
            case 2:
                thouValue = "MM";
                //System.out.print("MM");
                break;
            case 3:
                thouValue = "MMM";
                //System.out.print("MMM");
                break;
            case 4:
                thouValue = "MMMM";
                //System.out.print("MMMM");
                break;
            default:
                thouValue = "";
                break;
        }
        return thouValue;
    }

`

Rave
  • 51
  • 4

2 Answers2

2

Make your translator() method return a string which contains the final output.

So before the while statement in that method, declare a string such as String result = null; and in the loop append the popped values to this variable like, result += stack.pop().

Now, the place where you call the translator(integer) method, do numeralInput.setText(translator(integer)) instead of translator(integer)

Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
  • @PrerakSola Thank you for your reply! I have followed through with what you have said so far and have encountered an error. I changed the method call to set text as mentioned but I android studio says "Cannot resolve method 'setText(void)'" If I changed my translator method to return String would that help with the problem? – Rave Jun 14 '16 at 15:47
  • Yes, change the return type of `translator` method to `String` and return the variable `result` from it in the end. – Prerak Sola Jun 14 '16 at 15:49
  • Happy to help ... :) – Prerak Sola Jun 14 '16 at 16:00
2

You need to make your TextView a class member and initialise it in your oncreate bundle, so that you can access the textview elsewhere in the activity.

TextView numeralInput;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_main);
    numeralInput = (TextView) findViewById(R.id.textView);

convert.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View v) {

                    String intValue = numeralInput.getText().toString();
                    try{
                        int integer = Integer.parseInt(intValue);
                        if (integer > 0 && integer <= 4999){
                            translator(integer);

                        }else{
                            numeralInput.setText("Please enter an integer between 0 and 4,999.");
                        }

                    }catch(NumberFormatException e){
                        numeralInput.setText("Invalid input try again.");
                    }
                }
            }
    );
  • 1
    Thank you for your reply! I will have to fix and add a few things into my code to see if it works and get back to you. – Rave Jun 14 '16 at 15:50