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 2 of 2

Thread: Question about Scanner and String values

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about Scanner and String values

    Hello! I'm a relative beginner and this is my first post, and it's somewhat of an obscure question, so hopefully I've got all my wording right!

    I'm working on a program, and in this program I have a Scanner object set up using an empty delimiter. The input is a date (e.g. 1997), so I have 4 String objects set to receive input so that I can use each numeral of the date seperately.

    Scanner scanner = new Scanner(System.in);
    String delim = new String();
    scanner.useDelimiter(delim);
     
    String a = new String();
    String b = new String();
    String c = new String();
    String d = new String();
     
    System.out.print("Please enter a year: ")
    a = scanner.next();
    b = scanner.next();
    c = scanner.next();
    d = scanner.next();

    Now, I had assumed that if, for example, I entered a 3-character string for my input, my fourth String, d, would remain "null" but that is not in fact the case. When concatenated with other strings it seems to be a newline character of some sort. When checked for length it has a length of 1. But when compared to System.getProperty("line.seperator") or "\n" it returns false, using both the .equals method of the String class and the boolean == operator. I'd really like to know what the value that is being assigned to this String is (or rather how to represent that value in Java) so that I can use it for the sake of comparison in my program.

    If anyone can help, that'd be great. Or, if you think I need to further clarify what I'm trying to ask, please let me know! Thanks!


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Question about Scanner and String values

    May be the following will shed some light:

     
    import java.util.Scanner;
     
    public class Foo {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String delim = new String();
            scanner.useDelimiter(delim);
     
                // (A)
            String a = new String();
            String b = new String();
            String c = new String();
            String d = new String();
     
                // (B)
            System.out.print("Please enter a year: ");
            a = scanner.next();
            b = scanner.next();
            c = scanner.next();
            d = scanner.next();
     
                // (C)
            System.out.println("-->" + d + "<--");
            System.out.println("\n".equals(d));
            System.out.println((int)d.charAt(0));
            System.out.println((int)"\n".charAt(0));
     
                // (D)
            String e = scanner.next();
            System.out.println("\n".equals(e));
     
                // (E)
            String real = System.getProperty("line.separator");
            System.out.println("d+e is the real line separator: " + (d+e).equals(real));
            String bogus = System.getProperty("line.seperator");
            System.out.println("d+e is the bogus line separator: " + (d+e).equals(bogus));
        }
    }

    (I'm assuming a Windows OS)

    I was going to comment on all of that, but am too lazy! Ask if anything is unclear. Notice I didn't bother to initialise e with "new String()" as it seems a bit redundant to give a variable a value and then assign it another.

    I think I would read the whole line and use the string method toCharArray() for a task like this.

Similar Threads

  1. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  2. Replies: 3
    Last Post: November 20th, 2011, 02:33 PM
  3. Double to string giving extra values
    By sinsand in forum Collections and Generics
    Replies: 1
    Last Post: August 23rd, 2011, 05:57 AM
  4. [SOLVED] Question About Using Scanner Object Twice
    By derekeverett in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: July 16th, 2011, 10:36 AM
  5. Question concerning nextLine() method in Scanner Class
    By r_sardinas in forum Java SE APIs
    Replies: 2
    Last Post: October 20th, 2010, 08:35 PM