Referencing Two set of arrays with different data type String and Numb

Referencing Two set of arrays with different data type String and Numb

Page 1 of 1

13 Replies - 1438 Views - Last Post: 08 January 2010 - 07:01 PM Rate Topic: -----

#1 larrybalz4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-January 10

Referencing Two set of arrays with different data type String and Numb

Posted 06 January 2010 - 09:06 PM

I have two set of arrays one set is Double [] data type and the the other is String and are all 12 values each. The problem i have is to reference/compare/ check.
For example if two set of arrays
array a is {1.1, 1.2, 1.3}
array b is {aa,ab,ac}
If i use a formula to find the highest value of a which in this case is 1.3, print the String value in array b that references highest value in array a in this case is ac but wouldn't be easy to find out if you dealing with huge amount of data,

Is This A Good Question/Topic? 0
  • +

Replies To: Referencing Two set of arrays with different data type String and Numb

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,582
  • Joined: 27-December 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 06 January 2010 - 09:23 PM

Since the arrays are parallel, just return the highest index in the double[], which will be the corresponding index in the String[]. So 1.3 is located at index 2, so if the findMax portion of your code returns 2, you can access a[2] to get 1.3 and b[2] to get "ac". Also, remember that String literals are enclosed between double quotes. If you don't have to double quotes around the elements in your String[], then Java will look for a variable named aa, ab, ac, etc. and crash when it doesn't find these variables.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2552
  • View blog
  • Posts: 10,637
  • Joined: 20-September 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 12:54 AM

Sounds like you have a design problem: if you have a set of data that is connected to another set of data, they should be connected in the design of your program too. Your problem then disappears
Was This Post Helpful? 0
  • +
  • -

#4 larrybalz4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-January 10

Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 05:09 PM

WOW.. thanks guys.. For the quick response..
@macosxnerd101..
I do not quite understand your explanation.
Please i have added my code. I know the maximum value of the month but how do i check in the months (string) with stock (double) that corresponds with the highest value and print the month out.
Thanks in advance for your help

class calcStock

{
	  public static void main(String args[])
	  {
	   		double [] p;

			p = new double[12];
			p[0]=4.5;p[1]=4.63;p[2]=3.65;p[3]=4.70;p[4]=4.72;
			p[5]=4.21;p[6]=5.02;p[7]=4.81;p[8]=4.10;p[9]=4.11;
		p[10]=4.09;p[11]=4.01;

		String [] m;
		m = new String [12];
			m[0]="January";m[1]="Feburary";m[2]="March";
		m[3]="April"; m[4]="May";m[5]="June";m[6]="July"; 
		m[7]="Auguest";m[8]="September";m[9]="October";
		m[10]="November";m[11]="December";
			   
		double max=p[0]; // Initialise maximum to first value
		for (int i=1; i<p.length; i++) 
		{
		if (p[i] > max) 
			{
				max = p[i];   // new maximum
			}
		}
		
		double min=p[0]; // Initialise minimum to first value
		for (int i=1; i<p.length; i++) 
		{
		if (p[i] < min) 
			{
				min = p[i];   // new minimum
			}
		}
		p.equals(m);
		  
	System.out.println("The Worst month value is= " + min +m);
	System.out.println("The Best month value is= " + max+m);
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 05:48 PM

First your initialization is horrible

			double [] p = {4.5, 4.63, 3.65, ...., 4.09, 4.01};
			String [] m = {"January", "February", ...., "November", "December"};


would do the job a lot clearer

Now you want to write code in Java ... Java is an OO language so instead of comparing to arrays you should create an object that contains your pair of data

class CalcStock {
	private double p;
	private String m;

	// constructor 
	CalcStock(double price, String month) {
		 p = price;
		 m = month;
	}

	public static void main(String[] args) {
			double [] p = {4.5, 4.63, 3.65, ...., 4.09, 4.01};
			String [] m = {"January", "February", ...., "November", "December"};	

			// create the 12 CalcStock object
			CalcStock[] cs = new CalcStock[12];
			for(int i = 0; i < 12; i++) 
			   cs[i] = new CalcStock(p[i], m[i]);

			// now you can do your loops
			double max = cs[0].p;
			int maxIndex = 0;
			for(int i = 1; i < 12; i++) {
			   if(cs[i].p > max) {
				   max = cs[i].p;
				   maxIndex = i;
			   }
			}
			// ok now cs[maxIndex] is the biggest month and its name is cs[maxIndex].m
		}
}



Or event better
class CalcStock {
	private double p;
	private String m;

	// constructor 
	CalcStock(double price, String month) {
		 p = price;
		 m = month;
	}
	// returns the max of two CalcStock
	CalcStock getMax(CalcStock other) {
		if(p > other.p)
			return this;
		else
			return other;
		}

	public static void main(String[] args) {
			double [] p = {4.5, 4.63, 3.65, ...., 4.09, 4.01};
			String [] m = {"January", "February", ...., "November", "December"};	

			// create the 12 CalcStock object
			CalcStock[] cs = new CalcStock[12];
			for(int i = 0; i < 12; i++) 
			   cs[i] = new CalcStock(p[i], m[i]);

			// now you can do your loops
			CalcStock max = cs[0];
	
			for(int i = 1; i < 12; i++) {
				 max = max.getMax(cs[i]);
			}
			// ok now max.p = is the largest value and max.m is the month
		}
}


This post has been edited by pbl: 07 January 2010 - 06:16 PM

Was This Post Helpful? 1
  • +
  • -

#6 larrybalz4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-January 10

Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 06:19 PM

Thanks for your help.
Works well. I have edited the code to get Highest and Lowest Month.
Out of curiosity.. Is there are way of getting the same result by comparing array with different datatypes (Sting and Double). Because i would like to know if that is possible.
Please all response would be appreciated.

class CalcStock {
	private double p;
	private String m;

	// constructor
	CalcStock(double price, String month) {
		 p = price;
		 m = month;
	}

	public static void main(String[] args) {
			double [] p = {4.5, 4.63, 3.65,4.70,4.72,4.21,5.02,4.81,4.10,4.11,4.09,4.01};
			String [] m = {"January","February","March","April","May", "June","July","August","September","October","November", "December"};


			// create the 12 CalcStock object
			CalcStock[] cs = new CalcStock[12];
			for(int i = 0; i < 12; i++)
			   cs[i] = new CalcStock(p[i], m[i]);

			// now you can do your loops
			double max = cs[0].p;
		int maxIndex = 0;
		int minIndex =0;
			for(int i = 1; i < 12; i++) {
			   if(cs[i].p > max) {
				   max = cs[i].p;
				   maxIndex = i;
			   }
		if(cs[i].p< max) {
		   double min = cs[i].p;
		   minIndex = i;
		} 
			}
			// ok now cs[maxIndex] is the biggest month and its name is cs[maxIndex].m
// ok now cs[minIndex] is the lowest month and its name is cs[minIndex].m
	System.out.println("The Best month value is= " + cs[maxIndex].m);
System.out.println("The Worst month value is= " + cs[minIndex].m);
		}
}


This post has been edited by larrybalz4life: 07 January 2010 - 06:20 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 06:30 PM

View Postlarrybalz4life, on 7 Jan, 2010 - 05:19 PM, said:

Thanks for your help.
Works well. I have edited the code to get Highest and Lowest Month.
Out of curiosity.. Is there are way of getting the same result by comparing array with different datatypes (Sting and Double). Because i would like to know if that is possible.
Please all response would be appreciated.

No way.... you cannot compare double with String
but finding the highest of double you can use it's index for the second one
and... I have edited my previous post for an even cleaner way
Was This Post Helpful? 0
  • +
  • -

#8 larrybalz4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-January 10

Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 06:57 PM

View Postpbl, on 7 Jan, 2010 - 05:30 PM, said:

View Postlarrybalz4life, on 7 Jan, 2010 - 05:19 PM, said:

Thanks for your help.
Works well. I have edited the code to get Highest and Lowest Month.
Out of curiosity.. Is there are way of getting the same result by comparing array with different datatypes (Sting and Double). Because i would like to know if that is possible.
Please all response would be appreciated.

No way.... you cannot compare double with String
but finding the highest of double you can use it's index for the second one
and... I have edited my previous post for an even cleaner way


Ok, looks good but i have not tried it yet.. tho..
So can you compare using the index without creating objects as seen in you code above..
I mean, just a little tweak of my first post.. Because i was playing around with it and i got results of months/prices but couldn't get the same result as the one you helped me with..

java script:emoticon(':ph34r:',%20'smid_8')
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,582
  • Joined: 27-December 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 08:08 PM

You can compare indices to each other, which are type int. However, you can never compare String and double unless you want your compiler to barf all over you (a fancy way of saying fail to compile your program ;)).

Also, you should start to think about using classes instead of parallel arrays in your program design as Pbl has already pointed out. I've written a tutorial on the subject. If you hit the My Contributions link under my post, you should immediately see it. Working with classes will make your life a whole lot easier as you develop (no pun intended) as a programmer.

This post has been edited by macosxnerd101: 07 January 2010 - 08:19 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 08:10 PM

View Postmacosxnerd101, on 7 Jan, 2010 - 07:08 PM, said:

Also, you should start to think about using classes instead of parallel arrays in your program design.

Macos have you read my post ? :D
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,582
  • Joined: 27-December 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 08:18 PM

View Postpbl, on 7 Jan, 2010 - 11:10 PM, said:

View Postmacosxnerd101, on 7 Jan, 2010 - 07:08 PM, said:

Also, you should start to think about using classes instead of parallel arrays in your program design.

Macos have you read my post ? :D

Sorry, I have. I forgot to include the "as Pbl has already pointed out." I'll edit the above post. :D
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2552
  • View blog
  • Posts: 10,637
  • Joined: 20-September 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 08 January 2010 - 01:17 AM

Quote

Sorry, I have. I forgot to include the "as Pbl has already pointed out." I'll edit the above post.


Errr, and what about THIS? ;-)
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 08 January 2010 - 06:11 PM

View Postg00se, on 8 Jan, 2010 - 12:17 AM, said:

Quote

Sorry, I have. I forgot to include the "as Pbl has already pointed out." I'll edit the above post.


Errr, and what about THIS? ;-)

The principle is good :^: but... I am the one who provided the example :D
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,582
  • Joined: 27-December 08

Re: Referencing Two set of arrays with different data type String and Numb

Posted 08 January 2010 - 07:01 PM

View Postpbl, on 8 Jan, 2010 - 09:11 PM, said:

The principle is good :^: but... I am the one who provided the example :D

I'm just trying to off the OP a little more reinforcement on using classes by directing him to my tutorial, not upstage anyone. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1