I am new to android programming and am trying to the program to run but the problem is that the app builds and runs successfully but crashes whenever I press the GO button. My professor gave me this to accomplish as something to get started with android programming as he thought it would be challenging task that will be motivational and interesting. I looked everywhere for the solution but could not find it. My professor himself is unable to debug the code and hence.
The code is as given below:
**CODE 1**
package com.example.thispc.myapplication;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import java.util.Scanner;
import edu.princeton.cs.algs4.EdgeWeightedDigraph;
import edu.princeton.cs.algs4.DirectedEdge;
import edu.princeton.cs.algs4.BellmanFordSP;
public class Arbitrage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arbitrage);
Button Check_Arbitrage = findViewById(R.id.Check_Arbitrage);
Check_Arbitrage.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {
EditText inputcurrencynumber;
EditText inputdata;
TextView outputarbitrage;
inputcurrencynumber = findViewById(R.id.GetNoCurrencies);
inputdata = findViewById(R.id.currency_table_input);
outputarbitrage = findViewById(R.id.result);
inputcurrencynumber = findViewById(R.id.GetNoCurrencies);
int V;
V = Integer.parseInt(inputcurrencynumber.getText().toString());
outputarbitrage = findViewById(R.id.result);
double rate,stake;
inputdata = findViewById(R.id.currency_table_input);
EdgeWeightedDigraph G = new EdgeWeightedDigraph(V);
outputarbitrage.setText("Hello World");
String [] name = new String[V];
// The error occures here when i try to take an input
for (int v = 0; v < V ; v++)
{`enter code here`
name[v] = inputdata.getText().toString();
for (int w = 0; w < V ; w++)
{
rate = Double.parseDouble(inputdata.getText().toString());
DirectedEdge e = new DirectedEdge(v, w, -Math.log(rate));
G.addEdge(e);
}
}
BellmanFordSP spt = new BellmanFordSP(G,0);
if (spt.hasNegativeCycle())
{
stake = 1000;
for (DirectedEdge e : spt.negativeCycle())
{
//outputarbitrage.setText((Double.toString(stake) + name[e.from()]) + " " + (Double.toString(stake) + name[e.to()]) + "\n");
outputarbitrage.setText("%10.5f %s " + stake + name[e.from()]);
stake = stake * Math.exp(-e.weight());
outputarbitrage.setText("= %10.5f %s\n" + stake + name[e.to()]);
}
}
else
{
outputarbitrage.setText("No Arbitrage Opportunity Found");
}
}
});
}
}
Note : This code works perfectly in java IDE (I use netbeans) but crashes in android studio.
====================================================================================================================================================== EDIT 1
Added the log as below :
com.example.thispc.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.thispc.myapplication, PID: 4876
java.lang.NumberFormatException: For input string: "USD 1 1.6 4
CAD 2.3 1 1.3
CHF 4 8 1"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:539)
at com.example.thispc.myapplication.Arbitrage$1.onClick(Arbitrage.java:57)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
====================================================================================================================================================== EDIT 2