Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Anyone who can help me with some amateur Java?

  • 19-10-2012 4:57pm
    #1
    Registered Users, Registered Users 2 Posts: 1,695 ✭✭✭


    Im having a problem understanding where to use what type of loops.

    programming a menu similar to a cash machine.

    menu will keep asking to select option 1-4 until user presses 0 for exit.

    I might need to send on files to whoever can help im not sure. I have a fully working program with no source and i have to replicate it.


Comments

  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    There are 3 types of loops.

    FOR
    WHILE
    DO WHILE

    FOR loops should be used where you know the amount of times you need to loop before exiting.

    WHILE loops should be used where you want it to go on forever until a certain condition is met. You must evaluate the confition before executing any code within the loop.

    DO WHILE loops should be used in the same instance as WHILE but they allow you to execute the code within the loop once before evaluating the condition.

    These are fairly basic points and you will need to understand them before attempting a full program but you are likely to need a WHILE.

    Here is a bit of a website which will help you understand some of the point.
    http://www.tutorialspoint.com/java/java_loop_control.htm


  • Registered Users, Registered Users 2 Posts: 1,695 ✭✭✭Media999


    i think the problem is i need to use a Loop inside a loop.

    for example

    Student Grade System

    1) Enter student details
    2) Display student grades
    3) Display student statistics
    4) Display full transcript
    0) Exit system

    Select an option [0-4] >> 1

    // Choosing 1 brings up this menu

    Entering Student Details
    Student name: Media999
    Student number: 123123
    Programming Grade: 99
    Web Development Grade: 99
    Mathematics Grade: 99
    Critical Thinking Grade: 99
    Operating Systems Grade: 99
    Computer Architecture Grade: 99

    // When complete it should go to the first menu again but it repeats the submenu and asks for the student details again.
    // This is copy and pasted from correct code. Mine just repeats the menu just above this instead of the one below.

    Student Grade System

    1) Enter student details
    2) Display student grades
    3) Display student statistics
    4) Display full transcript
    0) Exit system

    Select an option [0-4] >>

    // I assume its a do while but cant figure out where exactly to place it.


  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    I would think you dont need a loop for the sub menu but attached your source code so we can see whats going wrong.


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    First of all the best thing to do is to write down with pen and paper (or in a simple text editor) each step of the program. This will force you to consider each aspect of the program and will also break the logic into smaller pieces which makes it easier to see what Java features can be used to implement each step.

    From what you have described you should only need a single loop. There is no need for a nested loop (i.e. a loop inside a loop) for the sub menu. This is because the main loop, which I'll call the program loop, should keep bringing you back to the main menu which then gives you the option to access the submenu again. As you've already guessed the program loop can be implemented with a do while loop. The program loop should start right before the point in code that you want to loop back to. It should complete after all the program logic has been run. The thing about a do while loop is that on completion of a single run of the loop it checks to see if it should loop again. For you this check should be if the Exit System option was taken or not.


  • Registered Users, Registered Users 2 Posts: 1,695 ✭✭✭Media999


    Right if i put a do while loop around the main menu
    do {
    System.out.println ("Student Grade System");
    System.out.println ("
    ");
    System.out.println (" ");
    System.out.println ("1) Enter student details");
    System.out.println ("2) Display student grades");
    System.out.println ("3) Display student statistics");
    System.out.println ("4) Display full transcript");
    System.out.println ("0) Exit system");
    System.out.println (" ");

    System.out.print ("Select an option [0-4] >> ");
    option = cm.nextInt();}

    while (option !=0 );

    It just keeps on looping back and asking the questions again if i press 1 rather than going into the submenu.

    Is the argument wrong maybe? Im guessing so as when 1 is selected the argument is correct so its looping.


  • Advertisement
  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    Media999 wrote: »
    Right if i put a do while loop around the main menu



    It just keeps on looping back and asking the questions again if i press 1 rather than going into the submenu.

    Is the argument wrong maybe? Im guessing so as when 1 is selected the argument is correct so its looping.

    I think you need a System.out.print ("Select an option [0-4] >> ");
    option = cm.nextInt();}

    at the start of your program as well, before the loop


  • Registered Users, Registered Users 2 Posts: 1,695 ✭✭✭Media999


    Think i figured it out. Moved the while (option !=0 ); right down after everything rather than just the first menu.

    Cheers for the help folks. We all have to start somewhere. :)


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    Heh, just finished writing out a reply roughly saying just that. Good going anyhow.


  • Registered Users, Registered Users 2 Posts: 1,695 ✭✭✭Media999


    Now onto the next problem. Im gonna have a good think about it before i look for help as im trying top learn here.

    Cheers for the help.


  • Registered Users, Registered Users 2 Posts: 1,695 ✭✭✭Media999


    Another small question that ive been trying to figure out but cant get this one right.

    I need it to print out something like "Invalid answer. Please enter a number between 0 and 100."

    if i put in SOP ("Invalid answer. Please enter a number between 0 and 100.") after the while it prints out even when the answer is between 0 and 100 and moves on. Way it is now it just repeats the question until the answer is correct.
    do {System.out.print (" Grade 1 - 100 ");
    Grade = cm.nextInt();

    }
    while( Grade < 0 || Grade > 100);


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    Media999 wrote: »
    Another small question that ive been trying to figure out but cant get this one right.

    I need it to print out something like "Invalid answer. Please enter a number between 0 and 100."

    if i put in SOP ("Invalid answer. Please enter a number between 0 and 100.") after the while it prints out even when the answer is between 0 and 100 and moves on. Way it is now it just repeats the question until the answer is correct.

    You want to place a condition after the users entry is read in to check if the answer is between 0 and 100 and if not then output the message.
    
    
    do {System.out.print (" Grade 1 - 100 ");
     Grade = cm.nextInt();
     
     IF(Grade > 100 || Grade < 0)
        System.out.print("Invalid answer. Please enter a number between 0 and 100.");
    
     
    }
     while( Grade < 0 || Grade > 100); 
    
    


  • Registered Users, Registered Users 2 Posts: 1,695 ✭✭✭Media999


    I kind of guessed it was an if statement but i had it like :
    do {System.out.print (" Grade 1 - 100 ");
    Grade = cm.nextInt();

    IF(Grade > 100 || Grade < 0)
    System.out.print("Invalid answer. Please enter a number between 0 and 100.");


    }
    while( Grade < 0 || Grade > 100);

    if (Grade > 100 || Grade < 0)
    System.out.print("Invalid answer. Please enter a number between 0 and 100.");

    Ah well. Live and learn.


    Cheers for the help.


  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    Media999 wrote: »
    I kind of guessed it was an if statement but i had it like :



    Ah well. Live and learn.


    Cheers for the help.


    When looking at the code just try and understand where in the code the user would be when its running.

    In your attempt you had it outside of the while loop but when you think about the way you have it programmed you can't get to that bit of code unless the user has already entered a valid number.


  • Registered Users, Registered Users 2 Posts: 1,695 ✭✭✭Media999


    Its very hard to think logically sometimes.

    It just sounds so simple when you actually hear the answer. Im only at this a few weeks now so im sure my mind will change.

    Like HTML and CSS you start to see the same patterns emerging.


Advertisement