1

I'm trying to make a program to analyze data, but the amount of data changes each time. Here is an example output:

Please enter the sample size: 3
Enter numbers for Trial 0
Enter sample #0:50
Enter sample #1:49
Enter sample #2:51
Enter numbers for Trial 1
Enter sample #0:30
Enter sample #1:31
Enter sample #2:32
Enter numbers for Trial 2
Enter sample #0:25
Enter sample #1:26
Enter sample #2:27
Enter numbers for Trial 3
Enter sample #0:15
Enter sample #1:16
Enter sample #2:17
Sample # Trial 1 Trial 2 Trial 3 Trial 4
0 50 30 25 15
1 49 31 26 16
2 51 32 27 17
-------------------------------------------------------
Average: 50 31 26 16
Min Average: 16
Max Average: 50
The trials do NOT concur!

This is the code I have currently:

import java.util.Scanner;

public class DataAnalyze {
    public static void main(String[] args) {
        int sample = 0;
        int avg;
        int t1 = 0;
        int t2;
        int t3;
        int t4;
        int val;
        int max;
        int min;

        Scanner input = new Scanner(System. in );
        System.out.println("Please enter the sample size: ");
        sample = input.nextInt();
        System.out.println("Enter the numbers for Trial 0");

        for (int i = 0; i < sample; i++) {
            System.out.println("Enter sample # " + i + " : ");
            t1 = input.nextInt();

        }

        System.out.println("\tSample #\tTrial 1\tTrial 2\tTrial 3\tTrial 4");
        System.out.println("\t" + (sample - 1) + "\t" + t1);

    }
}

I guess what I'm asking is how to make the for loop collect the input for t1 the first time around, t2 the second time around, and t3 the third time around, etc. Basically, how do I change what input a value is being assigned to each time the loop iterates? I assume that some sort of array is the best way, but I'm not well versed in java so if you could provide me an example that would be great, thanks.

Garennnn
  • 11
  • 3
  • Learn how to use [arrays](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) or [collections](https://docs.oracle.com/javase/tutorial/collections/) to store your data. – Jesper Dec 05 '15 at 06:03
  • Possible duplicate of [Getting random numbers in Java](http://stackoverflow.com/questions/5887709/getting-random-numbers-in-java) – Jaskaranbir Singh Dec 05 '15 at 06:03

2 Answers2

0

I think you mean to say generate unique random number when you say

make the for loop print out a different variable each time it iterates

This you can achieve by first adding ranges in list and then using shuffle() method of Collections class to get unique number.

 ArrayList<Integer> list = new ArrayList<Integer>();
    for (int i=1; i<11; i++) {
        list.add(new Integer(i));
    }
    Collections.shuffle(list);
    for (int i=0; i<3; i++) {
        System.out.println(list.get(i));
    }
Naruto
  • 4,221
  • 1
  • 21
  • 32
0

In Java you can store an arbitrary number of objects in List classes. The generic ArrayList class would probably be suited to your needs. You would append numbers onto the list as you read them in.

You can replace the for loop with a while loop and check for end-ofinput by e.g. reading in as a string then first checking for an empty string ( user just hit return ) and if not convert the string to an int.

I don't know how to make the for loop print out a different variable each time it iterates

I do no understand what you mean here. Do you mean print t1 on the first pass, then t2, then t3 and so on ? There's no simple way to do that and you would need to e.g. store those t-variables in an array and use an index calculated from your loop variable's value to do it, for example :

int t[4] ;

int i = 0 ;
while( .. )
{
    System.out.println( "var = " + t[ i % t.length ] ) ;

    i++ ;
};
  • Basically I mean the first time the loop goes through, I enter the input for t1, the second time it goes through I enter the input for t2, etc, and when it ends it prints out t1 t2 t3 and t4 all at once on the same line – Garennnn Dec 05 '15 at 06:21
  • This sounds like you need to store the t-values in an array ( as I gave an example ). You index the t-array by using the loop variable modulus ( % operation ) to the size of the t-array. – StephenG - Help Ukraine Dec 05 '15 at 07:28