Android studio Currency converter coding - android

I'm trying to make currency converter app in android studio,but i can't start my app in emulator and i believe it is because some errors in coding.I will copy my code here so you can help me ,I would be grateful.Sorry for my bad English :)
public class MainActivity extends AppCompatActivity {
public void clickFunction(View view) {
Log.i ("Info", "Button pressed");
EditText editText = (EditText) findViewById(editText);
Log.i( "Values" ,editText,getText().toString());
String amountInEuros = editText.getText()
Log.i("Info" , String.valueOf(editText.getText()));
String getAmountInEuros ;
double amountInEuros double = double.parsedouble (amountInEuros);
double amountInDollars double = double amountInEuros double * 1.08 ;
String amountInDollars String = double.toString (amountInDollars double)
Log.i("Amount in Dollars", amountInDollars String);
Toast.makeText ( this , amountInEuros + "Euros" + "is" + amountInDollars String , LENGTH_LONG );
}

These lines are missing a semicolon :
String amountInEuros = editText.getText();
String amountInDollars String = double.toString (amountInDollars double);

Related

Math function Power from String ("2 power 3")

I have to perform a pow function while getting equation from string like "2 power 3". I know there is function Math.pow(a, b) and also I can use for loop to achieve this , but the problem is both methods needs integer and I have equation in string. I don't know how to parse this string and separate both variables. And there is another problem. my equation could get little bit complex as well. for instance it could be like "2+3*5/5 power 2"
public class CalculationActivity extends AppCompatActivity {
EditText editText;
Button btnCalc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculation);
editText=findViewById(R.id.et_regular_dep);
btnCalc=findViewById(R.id.btnCalculate);
btnCalc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String equation= editText.getText().toString();
CalculateResult(equation);
}
});
}
private void CalculateResult(String equation) {
// here to perform power function
}
}
Try this :
String eq = "2 power 3";
String no2 = eq.substring(eq.indexOf("power") + 6 , eq.length());
String no1 = eq.substring(0,eq.indexOf("power")-1);
Log.d("no1",no1);
Log.d("no2",no2);
Log.d("ans",Math.pow(Double.parseDouble(no1),Double.parseDouble(no2))+"");
another easy way to do it
String eq = "2 power 3";
eq = eq.trim(); // if it has extra space at the start or end
String no1 = eq.split(" ")[0];
String no2 = eq.split(" ")[2];
Log.e("no1", no1);
Log.e("no2", no2);
Log.e("ans", Math.pow(Double.parseDouble(no1), Double.parseDouble(no2)) + "");

trying to display message without (# and hashcode) , but this error comes "non static variable this cannot be referenced from a static context"

I want to display the character array answer by converting into string , but the tostring method prints it with # and hashcode, so I override the tostring method, but it is still not able to display that.
public void func(View view)
{
EditText textfield=(EditText) findViewById(R.id.input);
String msg =textfield.getText().toString();
HashMap<Character,Character> mapping=new HashMap<Character,Character>();
mapping.put('a','.');
mapping.put('b',',');
mapping.put('c','p');
mapping.put('d','q');
mapping.put('e','g');
mapping.put('f','7');
mapping.put('g','0');
mapping.put('h','a');
mapping.put('i','f');
mapping.put('j','i');
mapping.put('k','z');
mapping.put('l','o');
mapping.put('m','r');
mapping.put('n','y');
mapping.put('o','x');
mapping.put('p','-');
mapping.put('q','*');
mapping.put('r','_');
mapping.put('s','w');
mapping.put('t','b');
mapping.put('u','l');
mapping.put('v','e');
mapping.put('w','h');
mapping.put('x','j');
mapping.put('y','v');
mapping.put('z','6');
int len=msg.length();
helper a=new helper();
for(int i=0;i<len;i++)
{
if(msg.charAt(i)>=97&&msg.charAt(i)<=122)
a.answer[i] = mapping.get(msg.charAt(i));
else
a.answer[i]=msg.charAt(i);
}
msg=a.toString();
//now encoded string is ready
TextView out=(TextView) findViewById(R.id.output);
out.setText(msg);
}
class helper{
public char[] answer;
public String toString(){
return " "+answer;
}
}
To get a string from a char array do msg = new String(a.answer);

Code crashes my application for unknown reason

I am making a simple currency calculator for learning purposes and I just have no idea why my code won't work. Currently, when I press the "Convert" button there is a message saying "Appname stopped working"
Here is the code :
public class MainActivity extends AppCompatActivity {
//Button convertButton;
public void convert(View view) {
EditText dkk = (EditText) findViewById(R.id.dkkField);
EditText gbp = (EditText) findViewById(R.id.gbpField);
EditText eur = (EditText) findViewById(R.id.eurField);
EditText usd = (EditText) findViewById(R.id.usdField);
Double dkkNum = Double.parseDouble(dkk.getText().toString());
Double gbpNum = Double.parseDouble(gbp.getText().toString());
Double eurNum = Double.parseDouble(eur.getText().toString());
Double usdNum = Double.parseDouble(usd.getText().toString());
TextView textView = (TextView) findViewById(R.id.resultView);
double dkkTogbp;
double dkkToeur;
double dkkTousd;
double gbpToDkk;
double eurToDkk;
double usdToDkk;
if(dkk!=null){
dkkTogbp = dkkNum * 0.114001;
dkkToeur = dkkNum * 0.134489;
dkkTousd = dkkNum * 0.142729;
textView.setText(dkk+ "DKK is "+ dkkTogbp+" GBP, "+dkkToeur+"EUR, "+ dkkTousd +"USD");
}else if(gbp!=null){
gbpToDkk = gbpNum * 8.77189;
textView.setText(dkk+ "GBP is "+ gbpToDkk+" DKK");
}else if(eur!=null){
eurToDkk = eurNum * 7.43555;
textView.setText(dkk+ "EUR is "+ eurToDkk+" DKK");
}else if(usd!=null){
usdToDkk = usdNum * 7.00630;
textView.setText(dkk+ "USD is "+ usdToDkk+" DKK");
Toast.makeText(getApplicationContext(), dkk.toString() + " DKK", Toast.LENGTH_LONG).show();
}
dkk.setText("");
gbp.setText("");
eur.setText("");
usd.setText("");
}
I discovered that when i remove the code block below and the if statements the program can run without crashing :
Double dkkNum = Double.parseDouble(dkk.getText().toString());
Double gbpNum = Double.parseDouble(gbp.getText().toString());
Double eurNum = Double.parseDouble(eur.getText().toString());
Double usdNum = Double.parseDouble(usd.getText().toString());
Can someone,please, explain why is this happening?
I don't get any errors.
The problem is by parsing the text to a double value.
At first, you should use a try-catch block to save your application for crashing, like this:
try
{
Double dkkNum = Double.parseDouble(dkk.getText().toString());
Double gbpNum = Double.parseDouble(gbp.getText().toString());
Double eurNum = Double.parseDouble(eur.getText().toString());
Double usdNum = Double.parseDouble(usd.getText().toString());
}
catch(NumberFormatException ex)
{
// If the format of the input string is wrong, then you get here the error message
}
catch(NullPointerException ex)
{
// If the input string is null, then you get here the error message
}
Check, whether the text, that should be parsed, has the right syntax, like "1.9".
UPDATE:
If the input string is null, the "parseDouble" method throws a "NullPointerException". For more informations see this link. I have updated the code.

TextView misbehaving when appending two strings

I am working on writing a simple temperature conversion program, to familiarize myself with Android programming. The user types in a number to an EditText, and it converts it from Fahrenheit to Celsius, or vice versa, then puts the answer in a TextView. I want to append a Unicode Celsius/Fahrenheit symbol to the end of the answer before displaying it. When I don't have it appending the symbol, it works fine and displays the correct number, but when it is trying to append the symbol to the end, the output displays all wrong, with a long string of numbers at the end (and still no Unicode symbol).
Here's my code:
This is the converter utility class:
public class ConverterUtil {
//Convert to celsius
public static String convertFahrenheitToCelsius(float fahrenheit) {
float temperature = (fahrenheit - 32) * 5 / 9;
DecimalFormat df = new DecimalFormat("#.#");
return df.format(temperature) + R.string.celsius_symbol;
}
//Convert to fahrenheit
public static String convertCelsiustoFahrenheit(float celsius) {
float temperature = (celsius * 9) / 5 + 32; //Append the unicode Celsius symbol (\u2103), then return
DecimalFormat df = new DecimalFormat("#.#");a
return df.format(temperature) + R.string.fahrenheit_symbol; //Append the unicode Fahrenheit symbol (\u2109), then return
}
}
And this is where I call it:
public void calculateTemperature(){
RadioButton celsiusButton = (RadioButton) findViewById(R.id.button2);
TextView output = (TextView) findViewById(R.id.output);
if (text.getText().length() == 0) {
output.setText("");
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
String outputText = celsiusButton.isChecked() ? ConverterUtil.convertFahrenheitToCelsius(inputValue) : ConverterUtil.convertCelsiustoFahrenheit(inputValue);
output.setText(outputText);
}
If I take out the part where I append the Unicode symbols, it looks like this:
And if I put that back in, I get this:
How do I fix that?
Looks like the resourceID of your fahrenheit_symbol & celsius_symbol are getting appended to your text than the actual character.
Try this,
public class ConverterUtil {
//Convert to celsius
public static String convertFahrenheitToCelsius(Context context, float fahrenheit) {
float temperature = (fahrenheit - 32) * 5 / 9;
DecimalFormat df = new DecimalFormat("#.#");
return df.format(temperature) + context.getResources().getString(R.string.celsius_symbol);
}
//Convert to fahrenheit
public static String convertCelsiustoFahrenheit(Context context, float celsius) {
float temperature = (celsius * 9) / 5 + 32; //Append the unicode Celsius symbol (\u2103), then return
DecimalFormat df = new DecimalFormat("#.#");a
return df.format(temperature) + context.getResources().getString(R.string.fahrenheit_symbol); //Append the unicode Fahrenheit symbol (\u2109), then return
}
}
Change where you call it like this,
public void calculateTemperature(){
RadioButton celsiusButton = (RadioButton) findViewById(R.id.button2);
TextView output = (TextView) findViewById(R.id.output);
if (text.getText().length() == 0) {
output.setText("");
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
String outputText = celsiusButton.isChecked() ? ConverterUtil.convertFahrenheitToCelsius(YourActivity.this, inputValue) : ConverterUtil.convertCelsiustoFahrenheit(YourActivity.this, inputValue);
output.setText(outputText);
}
Change
return df.format(temperature) + R.string.fahrenheit_symbol;
return df.format(temperature) + R.string.celsius_symbol;
to
return df.format(temperature) + getString(R.string.fahrenheit_symbol);
return df.format(temperature) + getString(R.string.celsius_symbol);
R.string.fahrenheit_symbol and R.string.celsius_symbol are both integers. You will need to look up the relevant string resource using Context.getResources().getString().
You will need to pass a Context (such as the calling Activity) to your ConverterUtil.

Unicode Encoding and Decoding

I want an android code or algorithum which will accept a Marathi text as input and convert the marathi text into unicode code. Also reconvert the unicode into marathi text.
i tried some code like:-
string marathi = "मी लाइक आहे";
UnicodeEncoding ue = new UnicodeEncoding(true,true);
string s1 = BitConverter.ToString(ue.GetBytes(marathi.ToCharArray())).Replace("-", "");
but this code is not working in case of android.
Help me as soon as possible. Thanks in advance.
Convert a string to unicode -
public String toUnicode(String text) {
String txt = "";
for (int i = 0; i < text.length(); i++) {
Log.d("Unicode", (int) text.charAt(i));
txt = txt + "\\" + text.charAt(i);
}
return txt;
}
To convert unicode into string, use -
public String toString(String uni){
String tt = "";
String[] parts = uni.split("\\");
for(String x:parts){
x = "\\" + x;
char un = x.toCharArray()[0];
tt = text + un;
}
return tt;
}

Categories

Resources