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: String O string where are you?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default String O string where are you?

    I'm trying to write a program that splits string up and prints it backwards. But it does not work.

    My questione: When I type domain name www.apple.com after pressing enter nothing happens until I press ctrl+z and then the correct output comes out: com.apple.www

    Why is that?

     
    import java.util.Arrays;
     
    public class Domain {//method reverseDomain
        private final String[] fields;
        private final int N;
     
        // store fields in reverse order
        public Domain(String name) {
            fields = name.split("\\.");
            N = fields.length;
        }
     
        // return string representation - fields, delimited by .
        public String toString() {
            if (N == 0) return "";
            String s = fields[0];
            for (int i = 1; i < N; i++)
                s = fields[i] + "." + s;
            return s;
        }
     
        // compare by reverse domain name
        public int compareTo(Domain that) {
            for (int i = 0; i < Math.min(this.N, that.N); i++) {
                String s = this.fields[this.N - i - 1];
                String t = that.fields[that.N - i - 1];
                int c = s.compareTo(t);
                if      (c < 0) return -1;
                else if (c > 0) return +1;
            }
            return this.N - that.N;
        }
     
         // test client
        public static void main(String[] args) {
     
            // read in domain names
            String[] names = StdIn.readAll().split("\\s+");
            Domain[] domains = new Domain[names.length];
            for (int i = 0; i < domains.length; i++) {
                domains[i] = new Domain(names[i]);
            }
     
            // sort
            Arrays.sort(domains);
     
            // print results
            for (int i = 0; i < domains.length; i++) {
                StdOut.println(domains[i]);
            }
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: String O string where are you?

    I suspect it is due to the way the StdIn.readAll() method works. Read the documentation for that call and see why it might be acting that way. I changed your code to use Scanner and System.out, and it works fine.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    JohnJohnson123 (October 16th, 2013)

Similar Threads

  1. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  2. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  3. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  4. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM