Results 1 to 7 of 7
Thread: Weird Behaviour of String
- 08-08-2010, 05:37 PM #1
Member
- Join Date
- May 2010
- Posts
- 25
- Rep Power
- 0
Weird Behaviour of String
Hi there,
I have a tricky qn.
I have a string declared like the following:
String a = "\010";
and if i do a toCharArray() i will get a char array of size 1 which is what i want.
however when i did a scanner like the following:
String input;
Scanner inputReader = new Scanner(System.in);
input = inputReader.nextLine();
and when i enter \010 into the scanner, even when i print input,
it gives me
\010
but when i do a toCharArray(), it ended up giving me 4 char; \ , 0, 1, 0
Why is this so? if i need to do a scanner, how can i make sure that when i do a toCharArray(), i will get a size 1 char array with \010 as the char element.
- 08-08-2010, 05:43 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Well, you typed in those four characters: \, 0, 1 and 0 and that's what the Scanner gives you. The character with code 010 is 8 in decimal and that should be the 8th character so it should be Cntrl-H if I'm not mistaken. Your keyboard driver may interpret it a "erase last character".
kind regards,
Jos
- 08-08-2010, 05:56 PM #3
Member
- Join Date
- May 2010
- Posts
- 25
- Rep Power
- 0
then how can i use scanner to put the 4 char \ , 0, 1, 0 into \010?
- 08-08-2010, 05:57 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-08-2010, 06:05 PM #5
Member
- Join Date
- May 2010
- Posts
- 25
- Rep Power
- 0
what if i don't use scanner, can i use system.in.readline()?
I want the following behaviour:
String a = "\010";
- 08-08-2010, 06:32 PM #6
The compiler is what is turning the String "\010" into a single character.
To get that single character by inputing a bunch of characters is going to require you to do what the compiler does: look for the escape char, look if the next is an octal number and do the conversion.
Perhaps there is a way to use the JavaCompiler class to do this for you.
- 08-08-2010, 11:08 PM #7
Member
- Join Date
- May 2010
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
BoxLayout Behaviour
By PetalumaBoy in forum AWT / SwingReplies: 4Last Post: 06-10-2009, 01:27 PM -
Strange behaviour in serialization
By Wolverine in forum NetworkingReplies: 0Last Post: 05-23-2009, 12:03 PM -
AffinedTransform strange behaviour
By Echilon in forum AWT / SwingReplies: 3Last Post: 12-11-2008, 09:58 AM -
Strange behaviour in swing
By cbalu in forum AWT / SwingReplies: 1Last Post: 05-23-2008, 09:23 PM -
Un expected behaviour when reading from inputstream
By adoorsarath in forum Advanced JavaReplies: 3Last Post: 08-10-2007, 05:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks