尝试将数组发送到一个方法,该方法将使用String.split()拆分每个元素

时间:2014-09-24 23:19:34

标签: java arrays string split

我创建了一个数组,它接受txt文件的每一行并将其存储在每个索引中。现在,我需要使用String.split()拆分它们。我有一个方法可以正确地执行拆分操作,但我需要创建一个方法,将我的数组的每个元素存储在一个字符串中,以传递给该方法。事实上,数组的每个元素都是从外部txt文件存储的,这使得它更加棘手。有关如何做到这一点的任何建议?这是我的所有代码:

//store each line into an array
import java.io.*;
import java.util.*;

public class InfixToPostfix {
private ObjectStack operator;


public InfixToPostfix() {
    operator = new ObjectStack();
}   

/**
 * method takes each expression from infix.txt and stores them in a string array
 * @param none
 * @return none
 */
public String getInfix() {

//declare new filestream    
FileInputStream in = null;
try {
    in = new FileInputStream("infix.txt");
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
//declare a reader
BufferedReader br = new BufferedReader(new InputStreamReader(in));


//declare string array to store elements in
String[] list = new String[10];

try {
    do {
        for (int i = 0; i < list.length; i++){
            try {
                list[i] = br.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    while (br.readLine() != null);
}

catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
try {
    in.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return (Arrays.toString(list));
}

//and the method that would perform the split
public static void parseArray(String entries) {
    String delims = "[ ]+";
    String[] tokens = entries.split(delims);
}

这里是txt文件:8 + 4 * 2 - 6

7 * 2 - 4 * 3 + 2 * 5

2 * 3 * 4 - 8 + 9/3/3

5 + 7 * 4 - 6

4 *(3 + 2 * 4) - 7

(5 + 7)/(9 - 5)

3 *(5 *(5 - 2)) - 9

((5 *(4 + 2) - (8 + 8)/ 2) - 9)^ 3

((5 + 5 *(6-2)+ 4 ^ 2)* 8)

(((3 ^ 4)))

1 个答案:

答案 0 :(得分:0)

所以我得到了它的工作。几天之后我就知道了,但我忘记发布了。这是:

/**
 * @author Amir
 * Converts from infix to postfix
 */

import java.io.*;

public class InfixToPostfix {
    private ObjectStack realStack;
    private EvalPostFix eval;
    //private SuperOutput so;
    private PrintWriter pw;

/**
 * constructor
 * @param pw 
 */ 
public InfixToPostfix(PrintWriter pw) {
    realStack = new ObjectStack();
    //eval = new EvalPostFix(); 
    this.pw = pw;   
    }

/**
 * method takes each expression from infix.txt and stores them in a string array
 * @param none
 * @return none
 */
public void getInfix() {

//declare new filestream    
FileInputStream in = null;
try {
    in = new FileInputStream("infix.txt");
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
//declare a reader
BufferedReader br = new BufferedReader(new InputStreamReader(in));

//declare string array to store elements in
String[] list = new String[10];

try {
    do {
        for (int n = 0; n < list.length; n++){
            try {
                list[n] = br.readLine();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }       
         for (int i = 0; i < list.length; i++) {
            splitArray(list[i]);
         }
    }
    while (br.readLine() != null);
}

catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
try {
    in.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
//    //System.out.println(Arrays.toString(list));
    //splitArray(Arrays.toString(list));
}

/**
 * Method that splits each line by the spaces
 * @param entries
 */
public void splitArray(String entries) {

    String delims = "[ ]+";
    String[] tokens = entries.split(delims);
//      for (int i = 0; i < tokens.length; i++)
//          System.out.println(tokens[i]);
    convertToPostfix(tokens);
}

/**
 * method that converts from infix to postfix
 * @param tokens
 */
public void convertToPostfix(String[] tokens) {
    StringBuffer br = new StringBuffer();

    for (int n = 0; n < tokens.length; n++) {
        char element = tokens[n].charAt(0);
        if (element <= '9' && element >= '0') {
            br.append(element);
        }
        else if (element == '^' || element == '/' || element == '*' || element == '+' || element == '-' || element == '(') {
            while (!realStack.isEmpty() && (priorityVal(element) <= priorityVal((Character) realStack.top())) && (element !='('))
                br.append(realStack.pop());
            realStack.push(element);

        }
        else if (element == ')') {
            while ((!(realStack.top().equals('('))))
                br.append(realStack.pop());
            realStack.pop();

        }
    }
    while (!realStack.isEmpty())
        br.append(realStack.pop());


    System.out.println(br.toString());
    pw.println(br.toString());

    //eval.evaluate(br.toString());

}

/**
 * method that assigns priority value to each operator
 * @param operator
 * @return priority value
 */
private int priorityVal(char operator) {
    switch (operator) {
    case '^': return 3;
    case '/': 
    case '*': return 2;
    case '+': 
    case '-': return 1;
    default : return 0;
    }
}   

}

此外,这是我们在课堂上创建的ObjectStack类,以防任何人真正想知道我们创建的堆栈是什么样的以及我在此代码中实现的是什么。 Idk,可能对某人有用:

public class ObjectStack {
    private Object[] item;
    private int top;

public ObjectStack() {
    item = new Object[1];
    top = -1;
}

public boolean isEmpty() {
    return top == -1;
}

public boolean isFull() {
    return top == item.length-1;
}

public void clear() {
    item = new Object[1];
    top = -1;
}

public void push(Object o) {
    if (isFull())
        resize(2 * item.length);
    item[++top] = o;
}

public void resize(int size) {
    Object[] temp = new Object[size];
    for (int i = 0; i <= top; i++)
        temp[i] = item[i];
    item = temp;
}

public Object pop() {
    if (isEmpty()) {
        System.out.println("Stack Underflow");
        System.exit(1);
    }
    Object temp = item[top];
    item[top--] = null;
    if (top == item.length/4)
        resize(item.length/2);
    return temp;
}

public Object top() {
    if (isEmpty()) {
        System.out.println("Stack Underflow");
        System.exit(1);
    }    
    return item[top];
}
}

这是司机:

/**
* @author Amir
* Driver class
*/

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;


public class Driver {

/**
 * main static method
 * @param args
 */
public static void main (String [] args) throws IOException  {  
    //SuperOutput so = new SuperOutput("csis.txt");
    PrintWriter pw = new PrintWriter(new FileWriter ("csis.txt"));
    InfixToPostfix infixToPostfix = new InfixToPostfix(pw);

    System.out.println("Hello and welcome to Amir's magical infix-to-postfix converter!");
    pw.println("Hello and welcome to Amir's magical infix-to-postfix converter!");
    System.out.println("Please wait while I fetch 'infix.txt'...");
    pw.println("Please wait while I fetch 'infix.txt'...");

    try {
        System.out.println("The original txt file contains as follows: " + Files.readAllLines(Paths.get("infix.txt")));
        pw.println("The original txt file contains as follows: " + Files.readAllLines(Paths.get("infix.txt")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //infixToPostfix.getInfix();
    infixToPostfix.getInfix();
    pw.close();


}
}