ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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 "/".
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!
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.
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!
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.