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 5 of 5

Thread: User Input with a Do Loop

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

    Post User Input with a Do Loop

    Alright, so I'm trying to code a program that will have the user input a collection of names and scores and allow them to calculate the highest and second highest scores. The two main questions I have are as follows:
    CODE:
    package homework7;

    import java.util.Scanner;

    public class ScoreCalculator {
    public static void main(String[] args) {
    int i = 0;

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Please enter the number of students: ");
    int count = keyboard.nextInt();
    keyboard.nextLine();

    do {
    i++;
    System.out.print("Please enter score number " + i + ": ");
    int score = keyboard.nextInt();
    keyboard.nextLine();
    } while (i <= count);

    }
    }

    • How can I change it to "Please enter name and score number" in the do loop and still extract only the int value despite the fact that a String is also being entered?

    • How can I store each int the user inputs into a separate variable within the do loop, and then extract those values and use them at the end to calculate the two highest scores?


    Please keep it simple - this is for a class and we aren't very in depth yet. That means no arrays, no separate classes, no files or lists.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: User Input with a Do Loop

    To see how to use the Scanner class, write a small simple test program and try using different combinations of the next... methods and by entering different combinations of data before pressing Enter.

    How can I store each int the user inputs into a separate variable
    The best way would be to use an array and change the index to the array every time you store something in it.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User Input with a Do Loop

    Quote Originally Posted by RadiantChaos View Post
    Please keep it simple - this is for a class and we aren't very in depth yet. That means no arrays, no separate classes, no files or lists.
    Quote Originally Posted by Norm View Post
    The best way would be to use an array and change the index to the array every time you store something in it.
    As I said, we haven't covered arrays yet, so I need a solution that is more complicated, but not as in depth.

    Also, not really sure what you were suggesting at first - care to elaborate, please?

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User Input with a Do Loop

    Quote Originally Posted by RadiantChaos View Post
    Also, not really sure what you were suggesting at first - care to elaborate, please?
    Scratch that, I figured that part out. Still need help on the storing of multiple scores, though.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: User Input with a Do Loop

    Do you only need to save the two highest scores and none of the others? Then you will only need two variables to save those values in.

Similar Threads

  1. Need help with user input/output for GUI.
    By Tctwins in forum AWT / Java Swing
    Replies: 6
    Last Post: February 20th, 2012, 04:13 PM
  2. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  3. Valid user input
    By ChristopherLowe in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  4. User Input File Name
    By PineAppleKing in forum Java Theory & Questions
    Replies: 12
    Last Post: June 3rd, 2011, 10:23 AM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM

Tags for this Thread