Java

ArrayList minimum and maximum value

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4159 Views - Last Post: 06 November 2008 - 11:03 PM Rate Topic: -----

#1 eagleuk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 08

Java

Posted 05 November 2008 - 08:40 PM

Hi there!

I would like to know how could I read data from a file. I have got something like that:

1 10
2 25
3 40
4 25
5 24
6 25
7 25
8 41
9 55
10 11
11 54
12 20

where numbers from 1-12 are actually months, which I have to convert to a String.

At the moment my program is reading all those integers, but I need to make it to read only second column for counting averages...

So far my code:

 

import java.util.*;
import java.io.*;

class Exercise{
	public static void main(String[] args)throws Exception{
		ArrayList<Integer> store = new ArrayList<Integer>();
		
		String FileName;
		int total = 0;
		int number = 0;
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please type in the name of your file: ");
		FileName = scanner.nextLine();
		
		File inputFile = new File(FileName);
		Scanner reader = new Scanner(inputFile);
		
		
		while(reader.hasNext()){
			int numbers = reader.nextInt();
			store.add(numbers);
			System.out.println(numbers);
		}
		Iterator<Integer> myIterator = store.iterator();
		
		while(myIterator.hasNext()){
			number = myIterator.next();
			total = total + number;
		}
		
		System.out.println("The total rainfall for this year is: " + total);
		int average = ((total)/12);
		
		System.out.println("The average of monthly rainfall is: " + average); 



Many thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Java

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java

Posted 05 November 2008 - 08:45 PM

not complicated at all
you already read one just read the second

int numbers = reader.nextInt();
int secondNumber = reader.nextInt();
Was This Post Helpful? 0
  • +
  • -

#3 eagleuk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 08

Re: Java

Posted 06 November 2008 - 06:13 AM

View Postpbl, on 5 Nov, 2008 - 07:45 PM, said:

not complicated at all
you already read one just read the second

int numbers = reader.nextInt();
int secondNumber = reader.nextInt();



Ok I have done this. I did not know that it could be so easy, do you know how to find from my SecondNumbers minimum and maximum value? I tried sth like that:

 		while(myIterator.hasNext()){
			number = myIterator.next();
		}
  
		int min = (int) Collections.min(Arrays.asList(number));
		int max = (int) Collections.max(Arrays.asList(number));
		
		System.out.println("Min number: " + min);
		System.out.println("Max number: " + max); 


many thanks
Was This Post Helpful? 0
  • +
  • -

#4 eagleuk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 08

Re: Java

Post icon  Posted 06 November 2008 - 05:21 PM

 import java.util.*;
import java.io.*;



public class Assignment5{
	public static void main(String[] args)throws Exception{
		ArrayList<Integer> store = new ArrayList<Integer>();
		
		String FileName;
		int total = 0;
		int number = 0;
			
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please type in the name of your file: ");
		FileName = scanner.nextLine();
		
		File inputFile = new File(FileName);
		Scanner reader = new Scanner(inputFile);
		
		
		while(reader.hasNext()){
			int numbers = reader.nextInt();
			int secondNumbers = reader.nextInt(); //This command reads second values from our file;
			store.add(secondNumbers);
			
		}
		Iterator<Integer> myIterator = store.iterator();
		
		
		
		while(myIterator.hasNext()){
			number = myIterator.next();
			total = total + number;
		}
		
		System.out.println("The total rainfall for this year is: " + total);
		int average = ((total)/12);
		
		System.out.println("The average of monthly rainfall is: " + average);
		
		while(myIterator.hasNext()){
			number = myIterator.next();
		}
  
int min = (int) Collections.min(Arrays.asList(number));
		int max = (int) Collections.max(Arrays.asList(number));
		
		System.out.println("Min number: " + min);
		System.out.println("Max number: " + max);
	
}
		
				
	}  


This program does do everything correctly, but I do not know why it prints max and min value as my last integer from my ArrayList :(


BTW input (my file has this data) :

1 10
2 25
3 40
4 25
5 24
6 25
7 25
8 41
9 55
10 11
11 54
12 20

any solutions?
Was This Post Helpful? 0
  • +
  • -

#5 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Java

Posted 06 November 2008 - 05:57 PM

I, myself, would just loop through the entire ArrayList and find the max/min values.

int min = store.get(0);
int max = store.get(0);

for (int x = 1; x < store.size(); x++) // loop thru all
{
    // comparisons
    if (store.get(x) < min)
    {
        min = store.get(x);
        continue;
    }

    if (store.get(x) > max)
        max = store.get(x);
}

System.out.println("Min number: " + min);
System.out.println("Max number: " + max);


Hope this helps! :D

This post has been edited by Locke37: 06 November 2008 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 eagleuk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 08

Re: Java

Posted 06 November 2008 - 06:39 PM

It doesn't work or I do not how to do it:/ Why my previous version does not work correctly? How to solve it? i am a noob!
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java

Posted 06 November 2008 - 06:45 PM

or easier:

int min = +99999999;
int max = -99999999;

read you data
and
if(value < min)
min = value;
if(value > max)
max = value;

....
Was This Post Helpful? 1
  • +
  • -

#8 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Java

Posted 06 November 2008 - 06:46 PM

To be honest, I don't know why your version doesn't work. I've never used an Iterator before, so I'm unfamiliar with how it does things. :unsure:

I know that mine will work though.
Was This Post Helpful? 0
  • +
  • -

#9 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Java

Posted 06 November 2008 - 06:49 PM

There's a duplicate thread...:rolleyes:

This post has been edited by Locke37: 06 November 2008 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#10 eagleuk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 08

Re: Java

Posted 06 November 2008 - 06:49 PM

I have to use Iterator in my exercise ;/
Was This Post Helpful? 0
  • +
  • -

#11 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Java

Posted 06 November 2008 - 06:50 PM

Oh...well nevermind me then. I could look it up...but by that time, pbl will probably have given you a solution. :unsure:
Was This Post Helpful? 0
  • +
  • -

#12 BigAnt   User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Java

Posted 06 November 2008 - 07:18 PM

change:
int min = (int) Collections.min(Arrays.asList(number));
int max = (int) Collections.max(Arrays.asList(number));



to:

int min = (int) Collections.min(store);
int max = (int) Collections.max(store);


Was This Post Helpful? 1
  • +
  • -

#13 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java

Posted 06 November 2008 - 07:25 PM

View PostLocke37, on 6 Nov, 2008 - 05:49 PM, said:

There's a duplicate thread...:rolleyes:

Merge done
Thanks Locke37

By the way is the "37" means that you were born in 1937 ?
Was This Post Helpful? 0
  • +
  • -

#14 eagleuk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 08

Re: Java

Posted 06 November 2008 - 07:41 PM

ok, it works :)

but if it is a possible how to make it to print out which value it was (e.g. The minimum was in the 3 row - and the 3rd row means March)
Was This Post Helpful? 0
  • +
  • -

#15 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java

Posted 06 November 2008 - 07:56 PM

View Postpbl, on 6 Nov, 2008 - 05:45 PM, said:

or easier:

int min = +99999999;
int max = -99999999;
int monthMin, montMax;

read you data
and
if(value < min) {
   min = value;
   monthMin = month;
}

if(value > max) {
   max = value;
   monthMax = month;
}



....

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2