Results 1 to 5 of 5
  1. #1
    alireza is offline Member
    Join Date
    Dec 2016
    Posts
    2
    Rep Power
    0

    Question problem with a java exercise

    hi
    why in this simple program (enter character) iterates for 3 times?

    it should be just one time so i dont know why this happened. please help me solve this problem.

    i have attached the picture of the simple program.

    thank u
    Attached Thumbnails Attached Thumbnails problem with a java exercise-java-sample-exercise.png  

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: problem with a java exercise

    When you do 'a' followed by <ENTER> you are actually sending 3 characters (in Windows).
    The 'a', '\r' and '\n', since a new line in Windows is a CR+LF, and not just LF ('\n').
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    alireza is offline Member
    Join Date
    Dec 2016
    Posts
    2
    Rep Power
    0

    Default Re: problem with a java exercise

    thank u for your answer

    so what should i do to fix this?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: problem with a java exercise

    Moved to New to Java section
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: problem with a java exercise

    Use a Scanner or a BufferedReader to read the line and not the individual characters.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Methods Problem Exercise
    By Jonna Rose Maala in forum New To Java
    Replies: 5
    Last Post: 07-31-2014, 07:48 AM
  2. problem with an exercise
    By psp300xxx in forum New To Java
    Replies: 1
    Last Post: 12-09-2013, 07:47 PM
  3. Problem with BlueJ Exercise
    By TomFahey in forum New To Java
    Replies: 4
    Last Post: 05-29-2013, 05:44 PM
  4. Problem with an exercise in java
    By chkontog in forum New To Java
    Replies: 2
    Last Post: 11-19-2012, 12:46 PM
  5. Problem in an exercise
    By Honest989 in forum New To Java
    Replies: 1
    Last Post: 11-10-2010, 09:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •