user2909791
user2909791

Reputation: 23

Creating a table from a loop (java)

How would I get this to print in a table? The way I have it written I can't figure out how to print all the results at the end. This is what I have :

import java.util.Scanner;
public class FFLAverage{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    System.out.println("Input the number of students");
    int numofstu = input.nextInt();
    double totalchange = 0;
    for (int count = numofstu; count != 0; count--){
      if (count == numofstu)
        System.out.println("Last Name \t\t StartWeight \t\t End Weight \t\t Weight Change");
      System.out.print("Enter student name: ");
      String name = input.next();
      System.out.print("Input start weight: ");
      double start = input.nextDouble();
      System.out.print("Enter end weight: ");
      double end = input.nextDouble();
      double change = (start - end);
      totalchange += change;
      System.out.println(name + "\t\t" + start + "\t\t\t" + end + "\t\t\t" + change);
    }
    double avg = totalchange / numofstu;
    System.out.println("Average weight change: " + avg);
  }
}

This is what the output should look like (console)

Last Name Start Weight End Weight Weight Change
Noble     222.5        202.4     -23.1
Tyler     189.5        194.4      +4.9
Pond      345.1        190.0     -155.1
Average weight change: --57.33

Upvotes: 2

Views: 10364

Answers (4)

Batakj
Batakj

Reputation: 12743

Five Step:

  1. Create A Class to store the value

    static class Student { private String name; private double start; private double end; private double change;

        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getStart() {
            return start;
        }
    
        public void setStart(double start) {
            this.start = start;
        }
    
        public double getEnd() {
            return end;
        }
    
        public void setEnd(double end) {
            this.end = end;
        }
    
        public double getChange() {
            return start - end;
        }
    
    }
    
  2. Create the object of each input of a student.

        Student student = new Student();
    
  3. Store the inputs for a student inside the object

            System.out.print("Enter student name[ "+(count+1) + "]: ");
            student.setName(input.next());
            System.out.print("Input start weight[ "+(count+1) + "]: ");
            student.setStart(input.nextDouble());
            System.out.print("Enter end weight[ "+(count+1)+ "]: ");
            student.setEnd(input.nextDouble());
    
  4. Store the object in array

     students[count] = student;
    
  5. Iterate the array

        System.out
                .println("Last Name\t\tStartWeight\t\tEnd Weight\t\tWeight Change");
        // PARSE THE ARRAY
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].getName() + "\t\t"
                    + students[i].getStart() + "\t\t" + students[i].getEnd()
                    + "\t\t" + students[i].getChange());
        }
    

Complete implementation

package com;

import java.util.Scanner;

public class FFLAverage {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Input the number of students");
        int numofstu = input.nextInt();
        double totalchange = 0;
        Student[] students = new Student[numofstu];
        for (int count = 0; count<numofstu;count++) {
            //Put data into A CLASS
            System.out.println("--------------------------------");
            Student student = new Student();
            System.out.print("Enter student name[ "+(count+1) + "]: ");
            student.setName(input.next());
            System.out.print("Input start weight[ "+(count+1) + "]: ");
            student.setStart(input.nextDouble());
            System.out.print("Enter end weight[ "+(count+1)+ "]: ");
            student.setEnd(input.nextDouble());
            totalchange += student.getChange();
            // PUT THE OBJECT IN ARRAY
            students[count] = student;
            System.out.println("--------------------------------");
        }

        System.out
                .println("Last Name\t\tStartWeight\t\tEnd Weight\t\tWeight Change");
        // PARSE THE ARRAY
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].getName() + "\t\t"
                    + students[i].getStart() + "\t\t\t" + students[i].getEnd()
                    + "\t\t\t" + students[i].getChange());
        }

        double avg = totalchange / numofstu;
        System.out.println("Average weight change: " + avg);
    }

    static class Student {
        private String name;
        private double start;
        private double end;
        private double change;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getStart() {
            return start;
        }

        public void setStart(double start) {
            this.start = start;
        }

        public double getEnd() {
            return end;
        }

        public void setEnd(double end) {
            this.end = end;
        }

        public double getChange() {
            return start - end;
        }

    }
}

Upvotes: 1

user2366923
user2366923

Reputation:

I suggest for you to use a new class :

    class student{
        public String name ;
        public double startWeight ;
        public double endWeight
        public student(){
             name = new String() ;
        }

        public double change(){
             return (endWeight - startWeight ) ;
        }
    }

after that in your main class :

    import java.util.Scanner;
    public class FFLAverage{
      public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Input the number of students");
        int numofstu = input.nextInt();
        student[] std = new student[numofstu] ;
        double totalchange = 0;
        for (int count = numofstu; count != 0; count--){
             std[count] = new student() ;


             System.out.print("Enter student name: ");
             std[count].name = input.next();
             System.out.print("Input start weight: ");
             std[count].startWeight = input.nextDouble();
             System.out.print("Enter end weight: ");
             std[count].endWeight = input.nextDouble();
       }
       for (int count = numofstu; count != 0; count--){
            if (count == numofstu)
                 System.out.println(name + "\t\t" + start + "\t\t\t" + end + "\t\t\t" + change);
            System.out.println(std[count].name + "\t\t" + std[count].startWeight + "\t\t\t" + std[count].endWeight + "\t\t\t" + std[count].change());
            totalchange+=std[count].change() ;
       }
       double avg = totalchange / numofstu;
       System.out.println("Average weight change: " + avg);
   }
  }

Upvotes: 1

Paul Samsotha
Paul Samsotha

Reputation: 209012

I think this exercise it to teach you about 2D arrays

You need to save your values into a 2D array

String[][] students = String[numofstu][4]

// inputs
// All of these inputs have to go inside a nested for loop using indexes i and j
String name = input.nextLine();
students [i][j] = name;

String beingWeight = String.valueOf(input.nextDouble());
students [i][j] = beginWeight;

String endWeight = String.valueOf(input.nextDouble());
students [i][j] = endWeight;

double wieghtChange = 
       Double.parseDouble(beginWeight)) - Double.parseDouble(endWeight));
String weightChange = String.valueOf(weightChange));
students [i][j] = weightChange;

When you want to print:

for (int i = 0; i < students.length; i++) {
    for (int j = 0; j < 4; j++) {
        // Print in your desired format
        System.out.print(students[i][j] + "     ");
    }
    System.out.println();
}

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32527

Create some sort of data container class lets call it Student, put aproopriate fields in it (name, weights etc.) with getters and settors or declare those fields public. Create new Studentin every loop pass, and add initiaized Student object to a common collection like List lets say LinkedList<Student> students. After your iteration is finished, iterate over created list by eg. for(Student s:students) and print data you want.

Upvotes: 1

Related Questions