LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2006, 01:09 AM   #1
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Rep: Reputation: 30
Exclamation Comparing \ in java


Hi,
I have a function that compares the characters in a string 1 by 1. The problem is that I don't know how to compare the switch character "\". I tried using char but that also failed! Anyone knows how to do that?
 
Old 02-25-2006, 01:21 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
It's probably unwise to write your own character-by-character "strcmp()" in Java - my guess is that you'll probably be much better off using the built-in ".equals()" method, or ".equalsIgnoreCase()".

I'm not sure what you mean by "'\' switch character". If you mean "escape sequence" (as in "\n"), then it means exactly the same in Java as in C/C++ (except that it's a 16-bit Unicode character, e.g. '0x000a', instead of an 8-bit '0x0a'). If you mean "path separator", you can use the Java "File.separator" to determine if your platform uses a DOS "\" or a Linux "/".
 
Old 02-25-2006, 02:31 AM   #3
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Original Poster
Rep: Reputation: 30
No what i mean that for eg in a JTextField the user types "12\4" instead of "12/4"
now I want to compare the string inputed 1 by 1 and replace any "\" by "/".
That is only an eg. the real thing is even more complex! My problem is that when you put "\" in java it takes \ as a switch character and the next character which is " is not taken as the closure of the string. thus I get Unclosed string literal error!
 
Old 02-25-2006, 04:48 AM   #4
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Simply use "\\"
 
Old 02-25-2006, 04:49 AM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
This doesn't make any sense. What you are talking about is escape sequences in string literals in the Java language. This is purely something that relates to how you write the source code, and has nothing to do with how the program actually works.
 
Old 02-25-2006, 07:01 AM   #6
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Original Poster
Rep: Reputation: 30
Firstly you cannot compare 1 "\" with "\\" as this generates an error!!!
Then to answer to spooon my question is about how to write the source code itself as the behavior of the application is under control as long as the source code is correct!
 
Old 02-25-2006, 07:18 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by MRMadhav
Firstly you cannot compare 1 "\" with "\\" as this generates an error!!!
You cannot write "\", it is a syntax error.
Quote:
Then to answer to spooon my question is about how to write the source code itself as the behavior of the application is under control as long as the source code is correct!
You have been answered to use "\\" to represent a string composed of the single backslash character.
 
Old 02-25-2006, 07:40 AM   #8
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Original Poster
Rep: Reputation: 30
Question Here Check this out

Code:
import java.io.*;

public class Test
{
	public static void main(String args[])
	{
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String text="";
		try{text = bf.readLine();}catch(IOException ex){System.out.println(ex);}
		boolean foundbackslash =false;
		for(int i=0;i<text.length();i++)
		{
			if(text.substring(i,i+1).matches("\\"))
			{
				foundbackslash=true;
			}
		}
		System.out.println(foundbackslash);
	}
}
when i use this i get this error:
Quote:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.error(Pattern.java:1528)
at java.util.regex.Pattern.compile(Pattern.java:1286)
at java.util.regex.Pattern.<init>(Pattern.java:1035)
at java.util.regex.Pattern.compile(Pattern.java:779)
at java.util.regex.Pattern.matches(Pattern.java:865)
at java.lang.String.matches(String.java:1597)
at Test.main(Test.java:13)
 
Old 02-25-2006, 10:56 AM   #9
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
The method "matches" does not compare two Strings:
http://java.sun.com/j2se/1.5.0/docs/...a.lang.String)
But the method "equals" does, so:
Code:
if(text.substring(i, i+1).equals("\\")){
...
}
or even better:
Code:
if(text.charAt(i) == '\\'){
...
}
 
Old 02-25-2006, 09:51 PM   #10
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
as has been said.. \ is not a character.. it is an escape.. so you have to escape the escape to test it.. so \\ in code is \ in plaintext..

\\ is \
\ does not exist..

clear as mud now im sure..
 
Old 02-27-2006, 03:22 AM   #11
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Original Poster
Rep: Reputation: 30
Hey thanks for your anwers! So stupid of me not thinking about the equals method!
I tried it today and it works perfectly!! Thanks a lot guys!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing drives warinthepocket Linux - General 7 09-23-2005 05:56 PM
Comparing php5 to php3 mrobertson Programming 9 06-23-2005 02:06 PM
Comparing distros dpeirce Linux - General 17 01-26-2005 11:08 PM
Comparing 2 Files xianzai Programming 2 05-23-2004 11:50 AM
Comparing filesystems for changes Astryk Linux - Software 1 04-12-2004 10:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration