-3

TextFile

[Line 1] a=5

[Line 2] b=2

[Line 3] c=3

[Line 4] a+b*c

I want to read and output only the Line 4. How?

And also, I want to read and pass those a=5, b=2, c=3 into an Integer values(variables)

public static void main(String[] args) throws IOException {
    try {
        File file = new File("D:\\TextFile.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line);
        }
        fileReader.close();

        String input = stringBuffer.toString();
        String output;
        InToPost theTrans = new InToPost(input);
        output = theTrans.doTrans(); 
        System.out.println("Postfix is: " + output + '\n');

    } catch (IOException e) {
        e.printStackTrace();
    }
}
blackpanther
  • 10,998
  • 11
  • 48
  • 78
Jake
  • 25
  • 6

3 Answers3

0

You may want to try out apache commons library:

String expression = FileUtils.readLines("D:\\TextFile.txt").get(3);

Not very performant, but the API looks elegant.

aquaraga
  • 4,138
  • 23
  • 29
  • Use the apache commons library (http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html) – aquaraga Sep 08 '13 at 11:23
0

A very trivial way:

public static void main(String[] args) 
throws IOException {
try {               File file = new File("D:\\TextFile.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            StringBuffer stringBuffer = new StringBuffer();
            String line;
            int lineNumber=0;
            Map<String,Integer> valueMap=new HashMap<String,Integer>();
            while ((line = bufferedReader.readLine()) != null&& ++lineNumber<4) {

             String[] sArr=line.split("=");
             //Here you have the number, do whatever you want with it.
             Integer iNumber=Integer.parseInt(sArr[1]);

             valueMap.put(sArr[0],iNumber);


            }


        String expression = bufferedReader.readLine();//Read 4th line, expression, delimited by space
        String[] sArr=input.split(" ");
        int operand1=valueMap.get(sArr[0]);
        int operand2=valueMap.get(sArr[2]);
        int operand3=valueMap.get(sArr[3]);

        char operator1=sArr[1].charAt(0);
        char operator2=sArr[1].charAt(0);
  // Now Calculate and output.
  fileReader.close();
} catch (IOException e) {
    e.printStackTrace();
}

}
mawia
  • 9,169
  • 14
  • 48
  • 57
  • //Here you have the number, do whatever you want with it. Integer iNumber=Integer.parseInt(sArr[1]); I want to calculate those values using the given expression on line[4] – Jake Sep 08 '13 at 11:15
  • This really helps a lot bro. But if you dont mind, would you please give me the whole code? Im so tired with this sht ~.~ – Jake Sep 08 '13 at 11:36
  • @user2758669 I guess you asking for too much spoon feeding :) – Gyanendra Dwivedi Sep 08 '13 at 11:57
  • Yea, but I hope you understand. Im just a student who needs a lot of more knowledge. Without the help of someone, I wont be able to do this project. Actually this was my first time working with this kind of programming. :( – Jake Sep 08 '13 at 12:12
  • If you are a student, it is better that you should learn it by taking hint/helps here. Believe me, it is worth doing that from your future perspective. Just asking for a complete solution is not a right direction. Anyway, you have already told it a *sht* , and who would like to try for a *sht* – Gyanendra Dwivedi Sep 08 '13 at 12:17
0

This is like language processing, and purely parsing data and processing the same. I would not provide you the code, but would like to provide an algorithm and love to see you implement that.

  1. Read each line from line 1-3 in a loop.
  2. Find the start index position of the '=' (Hint: Use Java indexOf).
  3. Read from startIndex to end of string (Hint: Use Java subString).
  4. Parse the value using Java Integer.parseInt() (for integer vale) and Double.parseDouble() (for double value) and store in a local variables. (Hint: See here)
  5. Finally read the expression line string.
  6. Split the expression string into individual character (See an example here).
  7. Iterate through the character array and figure out each character and calculate the value through the expression.

I hope this should work for you.

Community
  • 1
  • 1
Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53