Results 1 to 4 of 4
  1. #1
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default ^ Escape character

    Hi there,

    I'm having some trouble with my code. Basically I need to know how to use the split method using the character "^" as cutting parameter. Actually I want to cut with "x^" because I am writing a program for polynoms. It looks like this:

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class Polynom {
    	ArrayList<Pair> terms;
    	
    	Polynom(String fileName) {
    		terms = new ArrayList<Pair>();
    		readPolynom(fileName, terms);
    	}
    	
    	public static void main(String args[]) {
    		Polynom p1 = new Polynom("p1.txt");
    		System.out.print(p1);
    	}
    	
    	void readPolynom(String fileName, ArrayList<Pair> terms) {
    		boolean negative = false;
    		String polynom = "";
    		try	{
    			FileInputStream fstream = new FileInputStream(fileName);
    			DataInputStream in = new DataInputStream(fstream);
    			BufferedReader br = new BufferedReader(new InputStreamReader(in));
    			polynom = br.readLine();
    			in.close();
    		} catch (Exception e) {
    			System.out.println("File " + fileName + " not found");
    		}
    		
    		String[] splittedPoly = polynom.split(" ");
    		
    		String[] splittedTerm = splittedPoly[0].split("x^");
    		terms.add(new Pair(Double.parseDouble(splittedTerm[0]),
    			Integer.parseInt(splittedTerm[1])));
    		
    		for (int i = 1; i < splittedPoly.length; i++) {
    			if (i % 2 != 0) {
    				if (splittedPoly[i].equals("-"))
    					negative = true;
    				else
    					negative = false;
    			} else {
    				splittedTerm = splittedPoly[0].split("x^");
    				double coefficient = Double.parseDouble(splittedTerm[0]);
    				int degree = Integer.parseInt(splittedTerm[1]);
    				if (negative)
    					coefficient *= -1;
    				terms.add(new Pair(coefficient, degree));	
    			}
    		}
    	}
    	public String toString() {
    		String result = "";
    		for (Pair term : terms){
    			result += term;
    		}
    		return result;
    	}
    }
    
    class Pair {
    	double coef;
    	int deg;
    	
    	Pair (double coef, int deg) {
    		this.coef = coef;
    		this.deg = deg;
    	}
    	public String toString(){
    		return coef + "x^" + deg;
    	}
    }
    My problem lays in the following line:

    Java Code:
    String[] splittedTerm = splittedPoly[0].split("x^");
    I want to split a polynom like 3x^3 + 8x^2 - x^1 + 1.5, so first y split cutting at the whitespaces, and then I want to cut using "x^" but split requires a regular expression, in which ^ is a special character. Also there is no such thing as \^ so I'm lost.

    Thanks for your help.

    PS: The solution is forced, there are better ways of working with polynoms, but it has to be like this, so avoid comments like "I would change everything this way" or "The design is not the best choice". Thanks again and forgive spell faults since I am not native speaker.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by GaBuG View Post
    Also there is no such thing as \^ so I'm lost.
    No, but "x\\^" :-)

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    Quote Originally Posted by eRaaaa View Post
    No, but "x\\^" :-)
    If you are told to use \, you always write it as \\ in the source code since \ is an escape character. If you write in a text field (or a resource file [but not a message file, for externalisation]) you just type \.
    Last edited by Hibernate; 08-05-2011 at 03:06 AM. Reason: Not all resource files
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  4. #4
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Worked perfectly! Thanks a lot!
    Last edited by GaBuG; 12-31-2010 at 02:08 AM. Reason: :)

Similar Threads

  1. Escape Characters in xml
    By higashij in forum XML
    Replies: 2
    Last Post: 09-23-2010, 09:58 AM
  2. Need help with escape characters
    By jayjones149 in forum New To Java
    Replies: 1
    Last Post: 02-15-2010, 09:10 AM
  3. Escape Sequence
    By Punter in forum New To Java
    Replies: 4
    Last Post: 02-10-2009, 08:04 AM
  4. Using escape sequence with like clause (_)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:55 AM
  5. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 09:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •