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

Thread: How to use Array

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to use Array

    Hi, having trouble with this code. Can't figure out how to tell the code to keep printing firstname lastname and etc.. based on what number of students the user enters and how to keep storing it.



    package studentscoresapp;
     
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class StudentScoresapp {
     
     
        public static void main(String[] args) {
     
            // display a welcome message
            System.out.println("Welcome to the Future Value Calculator");
            System.out.println();
     
            // perform 1 or more calculations
            Scanner sc = new Scanner(System.in);
            String choice = "y";
            while (choice.equalsIgnoreCase("y")){
     
     
               System.out.println("Welcome to The Student Scores Application.");
     
               System.out.println("Enter number of students to score:  ");
               int numbofStudents = sc.nextInt();
     
     
     
               System.out.println("Student lastname:  ");
               String lastName = sc.nextLine();
     
               System.out.println("Student firstname:  ");
               String firstName = sc.nextLine();
     
               System.out.println("Student Score:  ");
               String studentScore = sc.nextLine();
     
     
     
     
            ArrayList<String> students = new ArrayList<>();
     
            //add number of strings
            students.add(lastName);
            students.add(firstName);
            students.add(studentScore);
     
            //print the array list
            for (int i = 0; i < students.size(); i++){
     
     
            String student = students.get(i);
            System.out.println(student);
     
     
     
            }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
                // see if the user wants to continue
                System.out.print("Continue? (y/n): ");
                choice = sc.next();
                System.out.println();
     
     
     
     
            }
     
     
     
        }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to use Array

    Where is the loop that is supposed to print each item?

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use Array

    Quote Originally Posted by jps View Post
    Where is the loop that is supposed to print each item?

    I'm sorry jps, I'm a little confused, do you mean where's the loop that will print each entry each time it's entered?

    I was under the impression from the instruction I was given for the assignment that when the user enters the number of students he wants to enter, lets say 4, somehow it's suppose to output the "enter first name, lastname" , to the user the amount of times he specified in the(so for the example 4 times), "enter number of students". Once that's done, it was suppose to output all the names, in that sequence. I'm stuck on not even understanding how to use the integer the user provides as the size of the array and how to output, "enter firstname" and etc the specified number of times automatically after. Even after that, I'm not sure how to get the array to store multiple strings of the same name (lastname, or firstname).

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to use Array

    I'm a little confused, do you mean where's the loop that will print each entry each time it's entered?
    I was just trying to answer the question you didn't really ask. Don't take my word for it, go by the instructions.
    Start out trying to read a number from the user and print it out. Then read a number from the user and use it in math, and print it out. Then work on using the number to control the loop. Build the program up in smaller steps.

Similar Threads

  1. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  2. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  3. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM