Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Tolls

Thread: Does any one know how to write this in Java?

  1. #1
    Nixter1997 is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Unhappy Does any one know how to write this in Java?

    Hey I am 15 and i am quite new to java and i wish to convert this program from Pascal to Java,but leaving it to do the exact same function.

    Pascal code:

    Program chose;

    Uses CRT;

    Var

    choice:byte;

    Lable

    start;

    Begin;

    start:

    Clrscr;

    Writeln('1....Male');

    Writeln('2....Female');

    Write('Select 1 or 2:');

    Readln(Choice):

    If (Choice<>1)and(Choice<>2)then

    Begin

    Writeln('Illegal entry');

    Delay(3000);

    Goto Start;

    End.




    So that was th simple program in Pascal,does anyone know how to do the BOLD bit in java cause i need to loop the program back to the beginning.

    Tkx

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,087
    Rep Power
    20

    Default Re: Does any one know how to write this in Java?

    When translating from one language to another, especially two quite different languages like Pascal and Java, your best bet is to simply write out what the original code does functionally and then rewrite it to duplicate that. Attempting to copy chunks of logic like the above will simply end in madness.

    So for the above, you need to print two lines to the console for the two options.
    Then read in the user selection.
    Then validate it is a 1 or a 2.
    All that needs to be in a loop (Java doesn't have GOTOs), which will continue until a valid selection is made.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: Does any one know how to write this in Java?

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Nixter1997 is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Re: Does any one know how to write this in Java?

    what loop do i need to use which will continue ?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: Does any one know how to write this in Java?

    Start here: The Java™ Tutorials

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Nixter1997 is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Re: Does any one know how to write this in Java?

    ok so what i did was this :

    if(choice <1 || choice >2){
    System.out.println("Incalid choice");
    }

    Ok now that i did that, how do i make it reboot the program so the person can select again?

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,087
    Rep Power
    20

    Default Re: Does any one know how to write this in Java?

    That's what the loop is for.

    Java Code:
    // Pseudo
    while (invalid input)
        ask for input
        validate input
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Does any one know how to write this in Java?

    (A bit off topic but a tip!)

    We've had scenarios at my university where we are asked in an assignment to write code in C or Java, that works the same way as a subroutine written in Assembly. When new to this, trial-and-error seem to be a good way to go, but after a while we all started to use flowcharts to get a good understanding on what we actually were supposed to code.

    I'm not saying that you have to do it in this case, but from making a flowchart where you have written down what happens in your Pascal code, you can easily identify how to code the same program and algorithm in i.e. Java.

    To make your program loop to the start, you can easily use a while loop and a boolean value to check whether or not it should "reboot" or exit.

    ---

    For flowcharts, I've used Gliffy before which is an online tool that requires no registration, though sometimes it is best to just stick with pen and paper for smaller and simpler algorithms.

    Gliffy can be found here.

    Good luck!

  9. #9
    Nixter1997 is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Re: Does any one know how to write this in Java?

    thank you some much for your time, it has helped me soooooo much

  10. #10
    Nixter1997 is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Re: Does any one know how to write this in Java?

    Thanks I love you lol !!!

  11. #11
    Nixter1997 is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Re: Does any one know how to write this in Java?

    -----Thank you Moderators + all that helped!!!... Success ----



    p.s any tips on how to make nice visual effects on eclipse?

Similar Threads

  1. Replies: 7
    Last Post: 11-30-2011, 06:58 PM
  2. How to write it in Java
    By agajantorayev in forum New To Java
    Replies: 8
    Last Post: 08-11-2010, 07:37 AM
  3. how to write except in java??
    By napi1234 in forum New To Java
    Replies: 1
    Last Post: 08-09-2010, 04:49 AM
  4. need help to write Program in JAVA
    By maneibr in forum New To Java
    Replies: 1
    Last Post: 03-13-2008, 01:28 PM
  5. How do i write this in java...?
    By johnny7white in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 07:05 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
  •