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: Simple Encoder/Decoder issue

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Encoder/Decoder issue

    I am working on a code that would make talking in a made up language easier, and my problem ends up being right away in my Main class. For a simple test run to see if it would work (and would at least progress to the class instead of getting stuck), I used a simple "language," which is just the words constructed backwards.
    import java.util.*;
    public class languagedecode
    {
        public static void main(String [] args)
        {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter coded phrase: ");
            ArrayList<String>coded = new ArrayList<String>();
            while(in.hasNext())
            {
                coded.add(in.next());
                System.out.println("1");
            }
            backwards code = new backwards(coded);
            System.out.println(code.decode());
        }
    }

    Pretty basic stuff. When I have the print line in my while loop, it prints out the number of words, as I expect. However, when I move the print line outside of the while loop, it doesn't print any ones. This means that my code never reaches outside of the while loop.

    When the print line is in the while loop, here is the output:

    Enter coded phrase: i nac llits daer ti tuo duol
    1
    1
    1
    1
    1
    1
    1

    But when I have the print line on the line after the loop finishes, the output reads as:

    Enter coded phrase: i nac llits daer ti tuo duol

    In both instances, I am unable to proceed. I am assuming it has something to do with my while loop, but I don't see my problem with it.

    e: this is probably a simple solution that I'm missing since I haven't done any keyboard input programs since the first few weeks of my AP Comp Sci course 2 years ago.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple Encoder/Decoder issue

    Read the API for scanner, I'll quote a portion of the hasNext method for you:
    This method may block while waiting for input to scan.
    Have you tried any other methods described in the API?
    Scanner (Java Platform SE 6)

Similar Threads

  1. [SOLVED] Simple calculator issue
    By ikocijan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2012, 02:43 AM
  2. Trouble wil XML Encoder/Decoder using linkedlist<parameter>
    By javanoobb in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 15th, 2011, 05:59 PM
  3. [SOLVED] Java Network simple issue
    By Budlee in forum Java Networking
    Replies: 2
    Last Post: April 7th, 2011, 12:49 PM
  4. Java uberNoob, requesting help with simple loop issue
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 09:13 PM
  5. [SOLVED] Encoder output error heed help
    By SHStudent21 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 14th, 2011, 06:47 PM