how do you split up a string in java without arrays?

耗尽温柔 提交于 2019-12-14 03:03:53

问题


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);
    }
}

回答1:


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

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.



来源:https://stackoverflow.com/questions/13095820/how-do-you-split-up-a-string-in-java-without-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!