Welcome to Dream.In.Code
Become a Java Expert!

Join 149,436 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,314 people online right now. Registration is fast and FREE... Join Now!




simplifying equations

 
Reply to this topicStart new topic

simplifying equations

blacksnake
17 Aug, 2007 - 07:35 PM
Post #1

New D.I.C Head
*

Joined: 23 Jul, 2007
Posts: 19


My Contributions
i was so confused the elimination of parenthesis in this equation just like:
(a+(b+c)+d) into a+b+c+d
CODE

import java.io.*;
import javax.swing.*;
import java.util.*;
public class filetest2{
public static void main(String[]args)throws IOException{
String read="";
char x, y, op=0;
int count=0;
String fileName=JOptionPane.showInputDialog("Enter filename:");
BufferedReader fr=new BufferedReader(new FileReader(fileName));
StringBuffer w;
while(read!=null)
{
    read = fr.readLine();
    w=new StringBuffer(read);
    String p=w.toString();
        for(int i=0;i<w.length();i++)
            {
                op=p.charAt(i);
                if(op==( || op==))//if the next character is a parenthesis
                {
                    delete op;//deleting character
                    
                }
                System.out.println(op);    
            }    
}       
fr.close();
} }

the outcome of this code: instead of deleting a parenthesis, it is an error...how to correct that problem since my approach is to convert complicated expressions to simpler form using elimination of parenthesis?

i hope, you partially answer this problem
User is offlineProfile CardPM
+Quote Post

n_b
RE: Simplifying Equations
18 Aug, 2007 - 03:47 AM
Post #2

New D.I.C Head
*

Joined: 9 Aug, 2007
Posts: 8


My Contributions
Hi there!

You should put character literals between single quotes, otherwise the compiler won't interpret them as characters.

Example:
CODE

char c = 'A';

if (c == 'A')
      System.out.print("The characters are equal.");


Inside your for-loop, the compiler thinks that your parenthesis are part of the code when, in reality, you wish to test if the variable op equals the character '(' or ')' (at least that is my interpretation).

Good luck with your application!
User is offlineProfile CardPM
+Quote Post

blacksnake
RE: Simplifying Equations
19 Aug, 2007 - 01:03 AM
Post #3

New D.I.C Head
*

Joined: 23 Jul, 2007
Posts: 19


My Contributions
QUOTE(n_b @ 18 Aug, 2007 - 04:47 AM) *

Hi there!

You should put character literals between single quotes, otherwise the compiler won't interpret them as characters.

Example:
CODE

char c = 'A';

if (c == 'A')
      System.out.print("The characters are equal.");


Inside your for-loop, the compiler thinks that your parenthesis are part of the code when, in reality, you wish to test if the variable op equals the character '(' or ')' (at least that is my interpretation).

Good luck with your application!


aside from detecting a '(' or ')' how to delete if it is equal to that character?
just like:
CODE

char op="a";
if ((c=='(')||(c==')')
{
        delete c;
}


output
CODE

content:
(a+(b+c)+d)
(a+(b/c))
(k*j)*p
(c/s)/d
simplified:
a+b+c+d
a+(b/c)
k*j*p
c/s/d


User is offlineProfile CardPM
+Quote Post

n_b
RE: Simplifying Equations
19 Aug, 2007 - 02:36 AM
Post #4

New D.I.C Head
*

Joined: 9 Aug, 2007
Posts: 8


My Contributions
I don't think delete is a word in Java. When you try to delete the characters, you write:
CODE
delete c;

but the compiler will think that it's a variable declaration were delete is the type and that c is the name of the variable. It will issue an error (unless you really do have a class called "delete" in your program smile.gif ).

You could, for each character, check to see if it is a '(' or ')', and if it isn't then you print it. For instance:
CODE

if (op == '(' || op == ')')
      //Do nothing
else
      System.out.println(op);

This approach doesn't really delete the characters, but it ignores them. If you really need to delete them, you can try some of the following:

If you want to store the output (e.g. in a String) rather than sending it to the screen, you should check out one overloaded method of the String class: substring().

The substring() method comes in two versions, this is what the Java API specification says about these two methods:
QUOTE

substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.


QUOTE

substring(int beginIndex, endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Example (also from the specification):
"hamburger".substring(4, 8) returns "urge"

Again, we don't delete the character, but we create a new string that contains all character except for the ')' or the '('. Sometimes it's a real pain that the String objects are immutable biggrin.gif.

If you have long expressions, the above method isn't really effective, because there will be a lot of copying, and you should look for other solutions. Another class that is great when analysing String objects is the StringTokenizer class in the java.util package. I encourage you to read about it, and that you can do here.

You seem to know a lot about Java, so I figured that general guidelines would be a better way to help rather than just posting a complete solution. I hope they were usefull to you, if you're still having problems, just post here and we'll see what can be done.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 12:07PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month