Results 1 to 7 of 7
  1. #1
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by diskhub View Post
    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.
    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

  3. #3
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    then how can i use scanner to put the 4 char \ , 0, 1, 0 into \010?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by diskhub View Post
    then how can i use scanner to put the 4 char \ , 0, 1, 0 into \010?
    AFAIK you can't; you have to parse and change the String yourself.

    kind regards,

    Jos

  5. #5
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    what if i don't use scanner, can i use system.in.readline()?

    I want the following behaviour:
    String a = "\010";

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  7. #7
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    Thanks everyone for telling me it's impossible...
    hence, forcing myself to rewrite it... luckily done it in 30 mins :D

    Thanks to all expert

Similar Threads

  1. BoxLayout Behaviour
    By PetalumaBoy in forum AWT / Swing
    Replies: 4
    Last Post: 06-10-2009, 02:27 PM
  2. Strange behaviour in serialization
    By Wolverine in forum Networking
    Replies: 0
    Last Post: 05-23-2009, 01:03 PM
  3. AffinedTransform strange behaviour
    By Echilon in forum AWT / Swing
    Replies: 3
    Last Post: 12-11-2008, 10:58 AM
  4. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 10:23 PM
  5. Un expected behaviour when reading from inputstream
    By adoorsarath in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 06:02 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
  •