The Artima Developer Community
Sponsored Link

Java Answers Forum
need help with a basic program

2 replies on 1 page. Most recent reply: Mar 20, 2002 7:27 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Julie

Posts: 13
Nickname: champ
Registered: Mar, 2002

need help with a basic program Posted: Mar 20, 2002 5:39 PM
Reply to this message Reply
Advertisement
using SumAverage.java as a base

class SumAverage {
public static void main(String[] arguments) {
int sum = 0;

if (arguments.length > 0) {
for (int i = 0; i < arguments.length; i++) {
sum += Integer.parseInt(arguments);
}
System.out.println("Sum is: " + sum);
System.out.println("Average is: " +
(float)sum / arguments.length);
}
}
}

how do I create a program (SortInput) that will read numbers entered into a command line and then count how many times the number appears in the input....example:

java SortInput 45 67 34 56 45 23 34 89 67

The Program should output:
45 2
67 2
34 2
56 1
23 1
89 1

Thanks for your help..


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: need help with a basic program Posted: Mar 20, 2002 5:52 PM
Reply to this message Reply
This is a good application for practicing using a Map. Use each new value as a key in a HashMap; if it is not already there, initialize it to new Integer(1), if it is already there, increment its value (well, you can't really increment an Integer, since it is immutable, but you'd get its value and store a new Integer value there that is one greater). At the end, just iterate through the map and display the results.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: need help with a basic program Posted: Mar 20, 2002 7:27 PM
Reply to this message Reply
Here is the program that uses basic constructs to count the number of occurances of a numbers read from command line. Modify it accordning to your need.

Thanks
Kishori
class Counter {
	public static void main(String[] args) {
		if ( args.length == 0 ) {
			System.out.println ( "You must enter some integers on command line" ) ;
			System.exit(0) ;
		}
		
		// Create an array to store all commonline integers
		int[] nums = new int[args.length]  ;
		
		// Now store all nummbers in array
		for ( int i = 0 ; i < args.length ; i++ ) {
			try {
				nums[i] = Integer.parseInt ( args[i] ) ;
			}
			catch ( NumberFormatException e ) {
				// Here you may store zero for invalid numbers
				// or you may decide to quit the application
				// I am just continuing by storing zero for all invalid nubers
				System.out.println ( "Invalid number:\"" + args[i] + 
									 "\". Using zero instead..." ) ;
				nums[i] = 0 ;
			}
		}
		
		// Now you need to sort the array/ I am using
		// java.util.Arrays class to sort the array. 
		// If you want, you can write your own code to sort the array
		java.util.Arrays.sort ( nums ) ;
		
		// Now your array is sorted and you can display the
		// occurance of any number easily, whic is as follows
		int current = nums[0] ;
		int count = 0 ;
		for ( int i = 0 ; i < nums.length ; i++ ) {
			if ( nums[i] == current ) {
				// The number is same as previous
				// Increment the counter
				count++ ;
			}
			else {
				// Print the Number and its occurances and
				// reset the count and current
				System.out.println ( current + " times " + count ) ;
				count = 1 ;
				current = nums[i] ;
			}
		}
		
		// The loop didn't print the last number in sorted array
		System.out.println ( current + " times " + count ) ;
		
	}
}
 
 

Flat View: This topic has 2 replies on 1 page
Topic: String to char... or another feasable solution Previous Topic   Next Topic Topic: about the credit score decision engine in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use