안드로이드 코드 질문입니다. 튕기는 이유를 몰라서 질문올립니다.

조회수 853회

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    tools:ignore="MissingConstraints">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:layout_editor_absoluteX="32dp"
    tools:layout_editor_absoluteY="5dp"
    android:layout_margin="50px">

    <EditText
        android:id="@+id/editText_pay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType = "number"
        android:text="최저임금"
        tools:layout_editor_absoluteX="31dp"
        tools:layout_editor_absoluteY="30dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editText_overworkpay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType = "number"
            android:text="초과수당(배수시 배수 입력)"
            tools:layout_editor_absoluteX="32dp"
            tools:layout_editor_absoluteY="87dp" />

        <RadioGroup
            android:id="@+id/rgroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:layout_editor_absoluteX="300dp"
            tools:layout_editor_absoluteY="75dp">

            <RadioButton
                android:id="@+id/radioButton_bytime"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="시급" />

            <RadioButton
                android:id="@+id/radioButton_bymult"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="배수" />
        </RadioGroup>

    </LinearLayout>

    <EditText
        android:id="@+id/editText_normworktime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType = "number"
        android:text="기본근로시간"
        tools:layout_editor_absoluteX="32dp"
        tools:layout_editor_absoluteY="159dp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/editText_startdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType = "number"
        android:text="시작일"
        tools:layout_editor_absoluteX="138dp"
        tools:layout_editor_absoluteY="238dp" />

    <Spinner
        android:id="@+id/spinner_week1"
        android:entries="@array/week"
        android:layout_width="85dp"
        android:layout_height="49dp"
        tools:layout_editor_absoluteX="32dp"
        tools:layout_editor_absoluteY="238dp" />


</LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/editText_enddate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType = "number"
            android:text="끝나는 날"
            tools:layout_editor_absoluteX="138dp"
            tools:layout_editor_absoluteY="238dp" />

        <Spinner
            android:id="@+id/spinner_endOfMonth"
            android:entries="@array/endOfMonth"
            android:layout_width="85dp"
            android:layout_height="49dp"
            tools:layout_editor_absoluteX="32dp"
            tools:layout_editor_absoluteY="238dp" />
    </LinearLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow>

            <EditText
                android:id="@+id/editText_sun"
                android:layout_width="60dp"
                android:layout_height="50dp"
                android:ems="10"
                android:inputType="number"
                android:text="일" />

            <EditText
                android:id="@+id/editText_mon"
                android:layout_width="60dp"
                android:layout_height="50dp"
                android:ems="10"
                android:inputType="number"
                android:text="월" />

            <EditText
                android:id="@+id/editText_tue"
                android:layout_width="60dp"
                android:layout_height="50dp"
                android:ems="10"
                android:inputType="number"
                android:text="화" />
        </TableRow>

        <TableRow>

            <EditText
                android:id="@+id/editText_wed"
                android:layout_width="60dp"
                android:layout_height="50dp"
                android:ems="10"
                android:inputType="number"
                android:text="수" />

            <EditText
                android:id="@+id/editText_thr"
                android:layout_width="60dp"
                android:layout_height="50dp"
                android:ems="10"
                android:inputType="number"
                android:text="목" />

            <EditText
                android:id="@+id/editText_fri"
                android:layout_width="60dp"
                android:layout_height="50dp"
                android:ems="10"
                android:inputType="number"
                android:text="금" />

            <EditText
                android:id="@+id/editText_sat"
                android:layout_width="60dp"
                android:layout_height="50dp"
                android:ems="10"
                android:inputType="number"
                android:text="토" />

        </TableRow>

    </TableLayout>

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
        android:id="@+id/textView_total"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:textSize="30dp"
        android:text="Total"
        tools:layout_editor_absoluteX="36dp"
        tools:layout_editor_absoluteY="576dp" />

    <Button
        android:id="@+id/enter"
        android:text="계산"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </TableLayout>
</LinearLayout>

</ScrollView>

MainActivity

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

Spinner spinner_week1, spinner_endOfMonth;
ArrayList <String> arrayList;
EditText editText_pay;
EditText editText_overworkpay;
EditText editText_normworktime;
EditText editText_enddate, editText_startdate;
EditText editText_sun, editText_mon, editText_tue, editText_wed, editText_thr, editText_fri, editText_sat;
TextView textView_total;
Button enter;
RadioGroup rgroup;
RadioButton radioButton_bytime, radioButton_bymult;
int pay, overworkpay, normworktime, startdate, enddate, total; //수당, 초과근무, 일반근무시간, 시작일, 끝일, 합계.
int calculated_date;
int week[] = new int[7]; //일 ~ 토까지 배열
int varEndOfMonth; // 달의 끝날
int converted_week; //요일
String week1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle("최저임금 월급 계산기");
    final String[] week = getResources().getStringArray(R.array.week);
    final String[] endOfMonth = getResources().getStringArray(R.array.endOfMonth);
    ArrayAdapter<String> adapter1;
    ArrayAdapter<String> adapter2;
    adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, week);
    adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, endOfMonth);

    spinner_week1 = (Spinner) findViewById(R.id.spinner_week1);
    spinner_week1.setAdapter(adapter1);
    spinner_endOfMonth = (Spinner) findViewById(R.id.spinner_endOfMonth);
    spinner_endOfMonth.setAdapter(adapter2);

    editText_pay = (EditText) findViewById(R.id.editText_pay);
    editText_overworkpay = (EditText) findViewById(R.id.editText_overworkpay);
    editText_normworktime = (EditText) findViewById(R.id.editText_normworktime);
    editText_startdate = (EditText) findViewById(R.id.editText_startdate);
    editText_enddate = (EditText) findViewById(R.id.editText_enddate);
    editText_sun = (EditText) findViewById(R.id.editText_sun);
    editText_mon = (EditText) findViewById(R.id.editText_mon);
    editText_tue = (EditText) findViewById(R.id.editText_tue);
    editText_wed = (EditText) findViewById(R.id.editText_wed);
    editText_thr = (EditText) findViewById(R.id.editText_thr);
    editText_fri = (EditText) findViewById(R.id.editText_fri);
    editText_sat = (EditText) findViewById(R.id.editText_sat);
    rgroup = (RadioGroup) findViewById(R.id.rgroup);
    radioButton_bytime = (RadioButton) findViewById(R.id.radioButton_bytime);
    radioButton_bymult = (RadioButton) findViewById(R.id.radioButton_bymult);
    textView_total = findViewById(R.id.textView_total);
    enter = (Button) findViewById(R.id.enter);

    enter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            week1 = spinner_week1.getSelectedItem().toString();
            varEndOfMonth = Integer.parseInt(spinner_endOfMonth.getSelectedItem().toString());
            input();
            date_calculate();
            calculate_dayOfWeek();
            switch (rgroup.getCheckedRadioButtonId()){
                case R.id.radioButton_bytime:
                    calculateWorktimeByTime();
                    textView_total.setText(total);//시간으로 계산한 시급 출력
                    break;
                case R.id.radioButton_bymult:
                    calculateWorktimeByMult();
                    textView_total.setText(total);//배수로 계산한 시급 출력
                    break;
            }
        }
    });// 함수 호출 실행

}
public void input() {
    week[0] = Integer.parseInt(editText_sun.getText().toString());
    week[1] = Integer.parseInt(editText_mon.getText().toString());
    week[2] = Integer.parseInt(editText_tue.getText().toString());
    week[3] = Integer.parseInt(editText_wed.getText().toString());
    week[4] = Integer.parseInt(editText_thr.getText().toString());
    week[5] = Integer.parseInt(editText_fri.getText().toString());
    week[6] = Integer.parseInt(editText_sat.getText().toString());
    pay = Integer.parseInt(String.valueOf(editText_pay.getText()));
    overworkpay = Integer.parseInt(String.valueOf(editText_overworkpay.getText()));
    normworktime = Integer.parseInt(String.valueOf(editText_normworktime.getText()));
    startdate = Integer.parseInt(String.valueOf(editText_startdate));
    enddate = Integer.parseInt(String.valueOf(editText_enddate));
} //값들을 사용할 수 있도록 변수에 넣어줌.

public void date_calculate(){
    if (enddate <= startdate){
        calculated_date = (enddate + varEndOfMonth) - startdate;
    }
    else if(enddate > startdate){
        calculated_date = (enddate + startdate);
    }
}//며칠 일했는지 계산. 한달 단위로 계산하며 시작일보다 끝나는 날이 작으면 그 달의 끝날 더해서 계산.

public void calculate_dayOfWeek(){
    if (week1 == "일")
        converted_week = 0;
    else if (week1 == "월")
        converted_week = 1;
    else if (week1 == "화")
        converted_week = 2;
    else if (week1 == "수")
        converted_week = 3;
    else if (week1 == "목")
        converted_week = 4;
    else if (week1 == "금")
        converted_week = 5;
    else if (week1 == "토")
        converted_week = 6;
}//시작날의 요일을 숫자로 변환.

public void calculateWorktimeByTime() {
    int temp;
    int j = converted_week;
    for (int i=0;i<calculated_date;i++){
        if (week[j] > normworktime){
            temp = week[j] - normworktime;
            total += normworktime * pay;
            total += temp * overworkpay;
        }
        else {
            total += week[j] * pay;
        }
        j++;
        if (j >= 7)
            j=0;
    }
}//시간에 따라 시급가산. 노동시간 초과시 지정해 놓은 값이 가산.
public void calculateWorktimeByMult() {
    int temp;
    int j = converted_week;
    for (int i=0;i<calculated_date;i++){
        if (week[j] > normworktime){
            temp = week[j] - normworktime;
            total += normworktime * pay;
            total += temp * (overworkpay * pay);
        }
        else {
            total += week[j] * pay;
        }
        j++;
        if (j >= 7)
            j=0;
    }//시간에 따라 가산. 노동시간 초과시 지정해 놓은 배수에 따라 가산.
}
}

arrays

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="week">
    <item>일</item>
    <item>월</item>
    <item>화</item>
    <item>수</item>
    <item>목</item>
    <item>금</item>
    <item>토</item>
</string-array>

<string-array name="endOfMonth">
    <item>28</item>
    <item>29</item>
    <item>30</item>
    <item>31</item>
</string-array>
</resources>

크래시 코드

--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calculator, PID: 6908
java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{63e9982 VFED..CL. ........ 0,5-550,123 #7f08005a app:id/editText_startdate aid=1073741830}"
    at java.lang.Integer.parseInt(Integer.java:615)
    at java.lang.Integer.parseInt(Integer.java:650)
    at com.example.calculator.MainActivity.input(MainActivity.java:107)
    at com.example.calculator.MainActivity$1.onClick(MainActivity.java:78)
    at android.view.View.performClick(View.java:7125)
    at android.view.View.performClickInternal(View.java:7102)
    at android.view.View.access$3500(View.java:801)
    at android.view.View$PerformClick.run(View.java:27336)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 6908 SIG: 9

제가 만든 안드로이드 계산기 코드입니다. 실행을 시키면 튕깁니다. 제가 잘못한 부분이 어딘지를 모르겠어서 질문합니다. 초보라서 고수님들께 질문올립니다.ㅠㅠ

이 앱은 최저임금값을 받아서 만약 일한 시간이 정해진 시간보다 많으면 급여를 추가(정해진 금액이나<시급> 최저임금에 float를 곱한 값<배수>을 추가)하여 계산하는 급여계산기입니다. 일~토까지 입력 후, 시작하는 날의 날짜와 요일 그리고 끝나는 날의 날짜를 받아서 만약 끝나는 날짜값이 시작날보다 작으면 해당 달의 마지막 날까지 더해서 계산하는 형식입니다.

혹시 수정할 부분이 있으면 수정해서 알려주셨으면 합니다.(저도 이렇게 큰건 안해봐서 체계적으로 알려주시면 감사하겠습니다.)

  • 에러코드를 올리세요. 주동혁 2020.6.15 20:01
  • 에러로그를 올려주셔야 답변하기 수월합니다. 알 수 없는 사용자 2020.6.16 11:24
  • 에러코드 올렸습니다. 조택민 2020.6.17 10:12

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기
      startdate = Integer.parseInt(String.valueOf(editText_startdate));
        enddate = Integer.parseInt(String.valueOf(editText_enddate));
    

    editText_startdate 라는 EditText에서 문자열을 가져오신 후 Int로 변환하려고 하시는 것 같으신데

    지금 코드를 보시면 EditText에서 텍스트를 가져오지않아서 에러가 발생하는겁니다.

    input() 메소드에 작성하신 다른 코드들하고 비교해보세요.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)