Quantcast
Channel: BTSoru.com - Bilisim ve Yazilim Teknolojileri Soru/Cevap Platformu - latest questions
Viewing all articles
Browse latest Browse all 4270

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

$
0
0

import java.util.Scanner;

public class stringIfade {

public static String[] convert(String[] text) {
    if (text.length == 1) {
        return text;
    }
    String temp;
    String[] temp2 = new String[text.length - 2];
    String ara = "";
    for (String a : text) {
        ara += a;
    }
    System.out.println(ara);

    if (ara.indexOf("*") != -1) {
        for (int i = 0; i < text.length; i++) {
            if (text[i] == "*") {

                temp = String.valueOf(Integer.parseInt(text[i - 1]) * Integer.parseInt(text[i + 1]));

                temp2[i - 1] = temp;

                for (int j = i; j < text.length - 2; j++) {

                    temp2[j] = text[j + 2];
                }

                return convert(temp2);
            } else {
                temp2[i] = text[i];
            }
        }
    } else if (ara.indexOf("/") != -1) {
        for (int i = 0; i < text.length; i++) {
            if (text[i] == "/") {

                temp = String.valueOf(Integer.parseInt(text[i - 1]) / Integer.parseInt(text[i + 1]));

                temp2[i - 1] = temp;

                for (int j = i; j < text.length - 2; j++) {

                    temp2[j] = text[j + 2];
                }

                return convert(temp2);
            } else {
                temp2[i] = text[i];
            }
        }
    } else if (ara.indexOf("+") != -1) {
        for (int i = 0; i < text.length; i++) {
            if (text[i] == "+") {

                temp = String.valueOf(Integer.parseInt(text[i - 1]) + Integer.parseInt(text[i + 1]));

                temp2[i - 1] = temp;

                for (int j = i; j < text.length - 2; j++) {

                    temp2[j] = text[j + 2];
                }

                return convert(temp2);
            } else {
                temp2[i] = text[i];
            }
        }
    } else if (ara.indexOf("-") != -1) {
        for (int i = 0; i < text.length; i++) {
            if (text[i] == "-") {

                temp = String.valueOf(Integer.parseInt(text[i - 1]) - Integer.parseInt(text[i + 1]));

                temp2[i - 1] = temp;

                for (int j = i; j < text.length - 2; j++) {

                    temp2[j] = text[j + 2];
                }

                return convert(temp2);
            } else {
                temp2[i] = text[i];
            }
        }
    }

    return null;

}

public static String[] diziyecevir(String ifade) {
    int kactanesayi = 0;
    char f;
    for (int i = 0; i < ifade.length(); i++) {
        f = ifade.charAt(i);
        if (f == '*' || f == '/' || f == '+' || f == '-') {
            kactanesayi++;
        }
    }
    int diziboyutu = kactanesayi + kactanesayi + 1;
    String[] sayiDizisi = new String[diziboyutu];
    int d = 0;
    for (int i = 0; i < sayiDizisi.length; i += 2) {
        char a;
        String b = "";
        for (int j = d; j < ifade.length(); j++) {
            a = ifade.charAt(j);

            if (a == '*' || a == '/' || a == '+' || a == '-') {
                sayiDizisi[i] = b;
                sayiDizisi[i + 1] = a + "";
                d++;
                break;
            } else if (j == ifade.length() - 1) {
                b += a + "";
                sayiDizisi[i] = b;
            } else {
                b += a + "";
                d++;
            }
        }
    }
    return sayiDizisi;
}

public static void main(String[] args) {

    /*String[] text = {"15", "*", "3", "+", "7", "*", "2", "-", "8", "/", "3"};*/
    Scanner scn = new Scanner(System.in);
    String sayi = scn.next();
    String[] dizi = diziyecevir(sayi);
    for (String s: args) {
        System.out.println(s);
    }
    String[] yeni = convert(dizi);

    System.out.print("İşleminizin cevabı :");
    for (int i = 0; i < yeni.length; i++) {
        System.out.print(yeni[i]);
    }

}

}


Viewing all articles
Browse latest Browse all 4270

Trending Articles


huddam daveti


Tekasür Suresi


"Yâ Kuddûs!" Esması Havâs ve Esrârı


Kasemi Mübelliğa


Peri44


Kuvvetli muhabbet ve aşk tılsımı


El-Mütekebbir


Papaz büyüsü


SCCM 2012 Client Installation issue


Zilhicce Ayının Namaz Duası ÖNEMLİ