Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: help with console input strnig....

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default help with console input strnig....

    i have a console input class that works on buff = new BufferedInputStream(System.in);
    it returns a string of the input ....
    now when i use it the string that comes back is in some way different .
    let say a input "abc" if i write the string it well print "abc" but if i use input.equals( "abc") it is false
    here is my main...

    import java.io.*;
    import java.util.*;
     
    public class DemoGeter
    {
    public static void main(String[] args)
       {
        String inputS = new String(ConsoleInput.readLine());
     
        String result = ReplaceString.replaceString(inputS); // replaces some of the chars (file separator)  prints 
                                                                                                the result.....
        System.out.println(result);
     
        String seeIf = new String("M:/iPod_Control/Music");
     
        System.out.println(seeIf);
     
        if (seeIf.equals(result))
        System.out.println("same");
        else 
        System.out.println("not same");


    here is the output for input " M:/iPod_Control/Music"

    M:/iPod_Control/Music \\input line
    M:/iPod_Control/Music \\ replaceString result and return string
    M:/iPod_Control/Music \\ seeIf is the string i what to get to....
    not same \\ if statement shows that string are different
    no files false


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: help with console input strnig....

    Hey,

    I edited the code slightly so it compiled and this works as expected:

    import java.io.*;
    import java.util.*;
     
    public class DemoGeter
    {
    public static void main(String[] args)
       {
        String inputS = "abc";
     
        String result = inputS;
     
        System.out.println(result);
     
        String seeIf = new String("M:/iPod_Control/Music");
     
        System.out.println(seeIf);
     
        if (seeIf.equals(result))
        System.out.println("same");
        else 
        System.out.println("not same");
     
       }
    }

    I think the problem is with ReplaceString.replaceString(inputS);

    Can we see this method?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with console input strnig....

    Just as a side note, please don't use new for a String, you might end up messing up.
    just use
    String seeIf = "M:/iPod_Control/Music";
    There would be disastrous results if u start using new for String, especially when you use == for comparing such strings rather than equals.

    Acumen,
    Lucid forums

  4. #4
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: help with console input strnig....

    i don't think that the problem is with replacestring method because even if i change the if statement to
    if (seeIf. equals(inputS)) the same output is shown ..... but here it is ...
     
    import java.io.*;
    class ReplaceString
    {
    static int count = 0;
    public static String replaceString(String str)
      {       
             if (str.indexOf(":") == 1 && count == 0)
                {
                 count ++;
                 String str1 = str.substring(2,str.length());
                 String str2 = replaceString(str1);
                 str1 = str.substring(0,2);
                 String str3 = str1.concat(str2);
                 return str3;
                }  
     
             String re = str.replace( '\\', File.separatorChar);
             str = new String(re.replace( '/', File.separatorChar));
             re = new String(str.replace( ':', File.separatorChar));
             if (File.separatorChar == '\\')
             { 
             String n = new String(re.replace('\\', '/'));
             return n;
             }
     
        return re;
       }
    }

  5. #5
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: help with console input strnig....

    well thanks every one i found out the problem there was some how some space that got caught at the end of the input when the input stream ended so all i did is add a trim() method to the return from the consoleInput method and it worked ...

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: help with console input strnig....

    Quote Originally Posted by mdstrauss View Post
    well thanks every one i found out the problem there was some how some space that got caught at the end of the input when the input stream ended so all i did is add a trim() method to the return from the consoleInput method and it worked ...
    Ahhhh I was going to suggest that! Wanted to check the other things first. Glad you solved your problem
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  3. Difference between input.next and findInLine(".")charat(0)
    By lotus in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 6th, 2009, 05:10 AM
  4. Different operation on Array
    By jempot in forum Collections and Generics
    Replies: 4
    Last Post: January 27th, 2009, 06:07 AM
  5. Program to mask input
    By sah in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2009, 06:43 PM