如何在不使用数组的Java中拆分字符串? [英] how do you split up a string in java without arrays?

查看:62
本文介绍了如何在不使用数组的Java中拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个家庭作业,在这里我需要创建一个计算器,该计算器接受这种形式的用户输入[1 + 2/3 * 5-4]。我不应该使用数组来存储字符串中的值。相反,即时通讯应该一次取3个数字和2个运算符。我的问题是如何存储数字和运算符,一旦存储它们并计算值,就从原始字符串中获得q个新数字和一个新运算符。这就是我到目前为止不确定即时消息是否朝着正确的方向前进的原因。

i have a homework assignment where i need to create a calculator that accepts the user input in this form [1+2/3*5-4]. im not supposed to use an array to store the values from the string. instead im supposed to take 3 numbers and 2 operators at a time. my question is how do i store the numbers and operators and once i store them and calculate the value get q new number and a new operator from the original string. this is what i have so far not sure if im in the right direction.

public class ExpresionEvaluation {

    private static String expresion;
    static double o1;
    static double o2;
    private static double o3;
    private static char operator1;
    private static char operator2;

    public static double getO1(String s){
        s=s.trim();
        String r ="";
        while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){

            r = r + s.charAt(0);
            s = s.substring(1);


        }
        o1 = Double.parseDouble(r);
        return(o1);
    }

    public static char getOperator1(String s){
        s=s.trim();
        char r;
        while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){

            r = s.charAt(1);
            s = s.substring(2);


        }
        r = operator1;
        return(r);
    }

    public static double getO2(String s){


        s=s.trim();
        String r ="";
        while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){

            r = r + s.charAt(2);
            s = s.substring(3);


        }
        o2 = Double.parseDouble(r);

        return(o2);
    }


    public static char getOperator2(String s){
        s=s.trim();
        char r;
        while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){

            r = s.charAt(3);
            s = s.substring(4);


        }
        r = operator2;
        return(operator2);
    }

    public static double getO3(String s){


        s=s.trim();
        String r ="";
        while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){

            r = r + s.charAt(4);
            s = s.substring(5);


        }
        o3 = Double.parseDouble(r);

        return(o3);
    }
}


推荐答案

您可以使用集合类来规避它。在java.util包中,有许多类可以模拟数组的行为,但是它们可调整大小并提供更多功能。其中包括java.util.ArrayList,java.util.LinkedList,java.util.Vector(不要使用那个)和java.util.Stack。 Java集合框架非常广泛,我建议阅读

Technically you could circumvent it by using a collection class. In the java.util package, there are many classes which emulate the behavior of an an array, but are re-sizable and offer much more functionality. These include java.util.ArrayList, java.util.LinkedList, java.util.Vector (don't use that one), and java.util.Stack. The java collections framework is very extensive, and I recommend reading the docs at

http://docs.oracle.com/javase/tutorial/collections/implementations/index.html

如果您也不被允许使用集合,那么我唯一想到的就是使用一个大字符串,并在各个部分之间插入某种特殊字符或序列,然后使用indexOf(char,fromIndex )和子字符串遍历各节,如下所示:

If you're not allowed to use collections either, the only thing i can think of is using one big string and inserting some kind of a special character or sequence in between the separate sections, then using the indexOf(char, fromIndex) and substring to iterate through the sections, something like this:

   final int maxwords = 5;
   String strlist = "this@should@be@an@array@";
   int lastindex = 0, index = 0;
   for(int n= 0; n < maxwords; n ++){

             index = strlist.indexOf("@",lastindex);
             String substr = strlist.substring(lastindex,index);
             //process substring here
             System.out.println(substr);
             lastindex = index + 1;
      }

这是非常丑陋的处理方式,但这是我唯一的方法考虑到。希望这会有所帮助。

This a very ugly way of doing things, but its the only thing I can think of. Hope this helps.

这篇关于如何在不使用数组的Java中拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆