-2

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

output

Community
  • 1
  • 1
Stevi G
  • 257
  • 1
  • 4
  • 13
  • Use LogCat to examine the Java stack trace associated with the crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Dec 09 '17 at 18:28
  • update the post and added the log. I get the NumberFormatException but dont know how to fix it. help please – Stevi G Dec 09 '17 at 18:57
  • 1
    Um, well, your input is not a number. You cannot parse it as a number. You will need to write code that parses this whitespace-delimited set of text. – CommonsWare Dec 09 '17 at 19:12
  • Possible duplicate of [What is a NumberFormatException and how can I fix it?](https://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – Zoe Dec 10 '17 at 10:07

1 Answers1

0

Its getting crash because of the below line

rate = Double.parseDouble(inputdata.getText().toString());

You are trying to parse the string and parseDoubel() expects the string value which only holds numeric value, but in your case inputdata.getText().toString() contains character other than numbers.

Try extracting digits form the inputdata before passing to parseDouble() method

str.replaceAll("[^0-9]", "");
Durga M
  • 534
  • 5
  • 17
  • I know its a pain in the ass but since i am new i dont exactly understand what you mean. I understand your explanation perfectly but not the suggested code snippet. Perhaps if you could edit the line `rate = Double.parseDouble(inputdata.getText().toString());` with the code you meant. That would be really helpful for a noob like me. Thanks – Stevi G Dec 09 '17 at 19:27
  • rate = Double.parseDouble(inputdata.getText().toString().replaceAll("[^0-9]", "")); – Durga M Dec 09 '17 at 19:29
  • ok so did that and it worked but now i get weird output. picture attached in the post – Stevi G Dec 09 '17 at 19:34
  • I also tried to solve the problem by creating a separte textbox that will only take numbers and nothing else but still get the same error even for all numbers – Stevi G Dec 09 '17 at 22:06