I commented where it stops working, can someone please help!This code is a roman numeral converter. I got the number to roman working, but the roman to number just crashes the app. If anyone knows why or how the code is crashing please lmk, and how to fix it! I been working on this for 8 hours straight and still can't find what is wrong. I am new to android studio this also may be why. Thanks.
Logcat error:
2022-01-27 13:27:33.286 1575-1575/com.example.numeralromancalculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.numeralromancalculator, PID: 1575
android.content.res.Resources$NotFoundException: String resource ID #0x5
at android.content.res.Resources.getText(Resources.java:444)
at android.widget.TextView.setText(TextView.java:6412)
at com.example.numeralromancalculator.MainActivity$1.onClick(MainActivity.java:54)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
convertToNum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//converter class is called
converter cn = new converter();
//checks if input is int or not
boolean digitCheck = TextUtils.isDigitsOnly(inputRoman.getText());
//App Crashes HERE
if (digitCheck == false){
//the input from the user
String theRom = inputRoman.getText().toString();
//string output to user
int outputNum = cn.romanToInteger(theRom);
//outputs number to roman conversion
numOutput.setText(outputNum);
}else{
//statement to print to screen
String reTry = "Invalid: Try Again";
//Outputs that users input is not valid
numOutput.setText(reTry);
}
}
});
//If Convert tto Roman button is clicked
convertToRoman.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//converter class is called
converter nc = new converter();
//checks if input is int or not
boolean digitCheck = TextUtils.isDigitsOnly(inputNum.getText());
if (digitCheck == true){
//the input from the user
int theNumber = Integer.parseInt(inputNum.getText().toString());
//string output to user
String theRoman = nc.toRoman(theNumber);
//outputs number to roman conversion
romanOutput.setText(theRoman);
}else{
//statement to print to screen
String reTry = "Invalid: Try Again";
//Outputs that users input is not valid
romanOutput.setText(reTry);
}
}
});
}
}
public class converter {
public String toRoman(int numberInput) {
//checks if integer conversion is possible
if (numberInput < 1 || numberInput > 3999) {
return "Sorry, Not Possible";
}
//Roman letter total
String returnValue = "";
//while statements to check for roman letters, and adds to total
//descending order
while (numberInput >= 1000) {
returnValue = "M";
numberInput -= 1000;
}
while (numberInput >= 900) {
returnValue = "CM";
numberInput -= 900;
}
while (numberInput >= 500) {
returnValue = "D";
numberInput -= 500;
}
while (numberInput >= 400) {
returnValue = "CD";
numberInput -= 400;
}
while (numberInput >= 100) {
returnValue = "C";
numberInput -= 100;
}
while (numberInput >= 90) {
returnValue = "XC";
numberInput -= 90;
}
while (numberInput >= 50) {
returnValue = "L";
numberInput -= 50;
}
while (numberInput >= 40) {
returnValue = "XL";
numberInput -= 40;
}
while (numberInput >= 10) {
returnValue = "X";
numberInput -= 10;
}
while (numberInput >= 9) {
returnValue = "IX";
numberInput -= 9;
}
while (numberInput >= 5) {
returnValue = "V";
numberInput -= 5;
}
while (numberInput >= 4) {
returnValue = "IV";
numberInput -= 4;
}
while (numberInput >= 1) {
returnValue = "I";
numberInput -= 1;
}
//return a string value of the numeral
return returnValue;
}
//APP CRASHES USING THIS CLASS
public int romanToInteger(String roman) {
int number = 0;
for (int i = 0; i < roman.length(); i ) {
char c = roman.charAt(i);
switch (c) {
case 'I':
number = (i != roman.length() - 1 && (roman.charAt(i 1) == 'V' || roman.charAt(i 1) == 'X'))
? number - 1
: number 1;
break;
case 'V':
number = 5;
break;
case 'X':
number = (i != roman.length() - 1 && (roman.charAt(i 1) == 'L' || roman.charAt(i 1) == 'C'))
? number - 10
: number 10;
break;
case 'L':
number = 50;
break;
case 'C':
number = (i != roman.length() - 1 && (roman.charAt(i 1) == 'D' || roman.charAt(i 1) == 'M'))
? number - 100
: number 100;
break;
case 'D':
number = 500;
break;
case 'M':
number = 1000;
break;
}
}
return number;
}
}
CodePudding user response:
It seems like value from your method is interpreted as resource Id, so make sure, the type is String:
//string output to user
String outputNum = Integer.toString(cn.romanToInteger(theRom));
//outputs number to roman conversion
numOutput.setText(outputNum);
CodePudding user response:
In order to use the method setText you have to pass variables of the type String to it as a parameter but the type of numOutput is Int. You have to change it to String. You can use String.valueOf(outputNum) and pass the result as a parameter to setText.
ConvertToNum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//converter class is called
converter cn = new converter();
//checks if input is int or not
boolean digitCheck = TextUtils.isDigitsOnly(inputRoman.getText());
//App Crashes HERE
if (digitCheck == false){
//the input from the user
String theRom = inputRoman.getText().toString();
//string output to user
int outputNum = cn.romanToInteger(theRom);
//outputs number to roman conversion
//***************************************************ANSWER!!! You must use String for "setText"! Not Int
// numOutput.setText(outputNum);
numOutput.setText(String.valueOf(outputNum));
//***************************************************
}else{
//statement to print to screen
String reTry = "Invalid: Try Again";
//Outputs that users input is not valid
numOutput.setText(reTry);
}
}
});