bradleycmetz's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
39 (0.05 per day)
Joined:
17-September 12
Profile Views:
486
Last Active:
User is offline Feb 05 2013 04:56 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   bradleycmetz has not set their status

Posts I've Made

  1. In Topic: New to Java- implementation question

    Posted 25 Jan 2013

    View Postjon.kiparsky, on 25 January 2013 - 02:21 PM, said:

    View Postbradleycmetz, on 25 January 2013 - 03:44 PM, said:

    View Postmostyfriedman, on 25 January 2013 - 01:38 PM, said:

    public static void main(String[]args)
    {
        Cipher cipher = new Cipher();
        cipher.run();
    }
    
    


    Thank you Mosty. This is great and it makes m code actually runnable- however now my issue lies within the scanner
    Scanner sc = new Scanner(System.in);
    		int shift = sc.nextInt();
    		String plaintext = sc.nextLine();
    

    Should be able to take in an integer, followed by a string as far as I know; however the program terminates after I input an integer.



    Yes, that's what'll happen with this construction. Here's how it works (or fails):


    You enter "1<ENTER_KEY>"
    nextInt() grabs the token "1" and gives that value to shift. The scanner still has a newline yet to be read (newline is not part of the first token you read), so when you hit nextLine(), it grabs everything in the scanner up to the first newLine it finds, and discards the newline. So now plaintext = "", and you've satisfied the nextLine, so the program moves on through the rest of the code.

    Easiest fix is to insert a nextline after the nextInt call - don't even capture the value.


    Scanner sc = new Scanner(System.in);
    		int shift = sc.nextInt();
    		sc.nextLine();   // consume that newline token
    		String plaintext = sc.nextLine(); // get the real input here
    
    



    If you're doing a lot of input, you can define a quick nextIntLine() method which does basically this - reads a nextInt, then consumes a newline and returns the int. You can even set it to loop until an int is entered, rather than throwing a NumberFormatException (or actually an InputMismatch, I think it is) - but that's a bit more ambitious.

    View Postuseletters, on 25 January 2013 - 04:00 PM, said:

    You should always input an integer followed by a string, as you said. You can put an if clause or try..catch clause to manage on how you want the code will end.



    This would work, but it's not how the user wants this to work. It's correct, but bad design.


    Interesting...

    This excerpt sort of confuses me and ill break it down so maybe you could kindly explain :)/>/> (although do not feel obligated as I will figure out the answer eventually)
    "You enter "1<ENTER_KEY>" nextInt() grabs the token "1" and gives that value to shift. -- I understand
    _____
    The scanner still has a newline yet to be read (newline is not part of the first token you read) -- im confused between newline and nextline. Why when I ask for the scanner to read nextline as a string, it does not simply read the next line I type into a string until I hit 'enter' ?
    _____
    so when you hit nextLine(), it grabs everything in the scanner up to the first newLine it finds, and discards the newline. So now plaintext = "", and you've satisfied the nextLine, so the program moves on through the rest of the code. "

    I dont have any comments on the 3rd section.. I am just kind of confused by this concept because it seems clunky at this stage- unless it has more functionality elsewhere.

    So I type an int, hit enter nextint gathers the 1 (but does not go to a new line?) then nextline is executed and grabs the nothingness that follows the 1? Then my program terminates because there is nothing to output? (but if I eat that nothingness line following the 1 first, I can then ask it to take nextline again?

    Thank you guys SO much for the time you put into helping noobies like me... seriously it helps a LOT to have perspectives other than your own.


    Great song in your sig... Are you an Iron and Wine fan? I think you would really like them..
  2. In Topic: New to Java- implementation question

    Posted 25 Jan 2013

    is there an easy way to track variables in eclipse? I'm trying to track the string after i type it to make sure all of the data is being read into the string.
  3. In Topic: New to Java- implementation question

    Posted 25 Jan 2013

    View Postmostyfriedman, on 25 January 2013 - 02:01 PM, said:

    Can you post a sample of how the input should look like?

    IN: 3 A SAMPLE PLAINTEXT

    OUT: D VDPSOH SODLQWHAW

    Currently I only get 'D'

    if I type
    IN: WONDERFUL DAY

    i get

    OUT: YQPFGTHWN
  4. In Topic: New to Java- implementation question

    Posted 25 Jan 2013

    View Postmostyfriedman, on 25 January 2013 - 01:51 PM, said:

    try using next() instead of nextLine() to read the string


    This lets me proceed- however it only reads until I input " " (space) everything after a space is not included i the output.
  5. In Topic: New to Java- implementation question

    Posted 25 Jan 2013

    View Postmostyfriedman, on 25 January 2013 - 01:38 PM, said:

    public static void main(String[]args)
    {
        Cipher cipher = new Cipher();
        cipher.run();
    }
    
    


    Thank you Mosty. This is great and it makes m code actually runnable- however now my issue lies within the scanner
    Scanner sc = new Scanner(System.in);
    		int shift = sc.nextInt();
    		String plaintext = sc.nextLine();
    

    Should be able to take in an integer, followed by a string as far as I know; however the program terminates after I input an integer.

    /edit perhaps I need to use a method other than run?

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

bradleycmetz hasn't added any friends yet.

Comments

bradleycmetz has no profile comments yet. Why not say hello?