Replace an element in an array

input a new amount that will replace a previous amount in an array.

Page 1 of 1

7 Replies - 19298 Views - Last Post: 03 November 2009 - 11:04 PM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Replace an element in an array

Post icon  Posted 03 November 2009 - 09:45 PM

I have looked all over in my book and the internet, I cannot find how to code the following:
on the inputDialog, it asks what is the correct amount for month, month being the month entered. But at this point how do I get it to change the element in the array?


You will now change this so that the user can correct the income for any month.  The program should  ask the user to
	 enter the number of a month (1 - 12). If the number is between 1 and 12, next ask the user for the correct income amount. Use the month name, for example if the 
	 user asks to change month 3, your program should display "What is the correct amount for March?". 
	
	The program should receive the correct amount and  replace the amount in the  appropriate array location. The program should then display a suitable message. For 
	example if the user entered 3 for the month and 4500 for the correct amount, the program would display "The amount for March has been changed to $4,500.00". This 
	statement should refer to the appropriate array element so that you can be sure that the value was changed correctly in the array. 
	
*/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Ch08Ex04
{
	public static void main (String [] args)
	{
		// your code here		
		DecimalFormat currency = new DecimalFormat("$#,##0.00");
		 double total = 0;
		 int month;
		 double newAmount;
		// your code here
		double [] monthlySales = {4000.00, 4000.00, 5000.00, 3000.00, 5000.00, 4000.00, 
								4000.00, 5000.00, 6000.00, 4000.00, 3000.00, 3000.00};			//Array of monthlySales
							
			String[] monthNames = {	"January", "February", "March", "April", "May", "June", 
									"July", "August", "September", "October", "November", "December"};//Array of monthNames
			
									
			String input = JOptionPane.showInputDialog("Please enter a number for a month (1 - 12).");
			month = Integer.parseInt(input);
			
			if (month < 1 || month > 12)
			JOptionPane.showMessageDialog(null, "You must enter a number between 1 and 12");
			
			else 
			JOptionPane.showInputDialog("What is the correct amount for " + (monthNames[month -1]) + "?");
			
			
			JOptionPane.showMessageDialog(null, "The amount for " + (monthNames[month -1]) + " has been changed to " + currency.format());
			
	 // end main

}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Replace an element in an array

#2 hackterr  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 293
  • Joined: 13-August 09

Re: Replace an element in an array

Posted 03 November 2009 - 10:23 PM

Simply Overwrite it
just assign the new value to the array
array[index of element to be changed]=new value

This post has been edited by hackterr: 03 November 2009 - 10:23 PM

Was This Post Helpful? 1
  • +
  • -

#3 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Replace an element in an array

Posted 03 November 2009 - 10:31 PM

View Posthackterr, on 3 Nov, 2009 - 09:23 PM, said:

Simply Overwrite it
just assign the new value to the array
array[index of element to be changed]=new value

When I type in the new sales amount from the inputDialog box how does it know.
My question is what am I supposed to reference after this
else 
			JOptionPane.showInputDialog("What is the correct amount for " + (monthNames[month -1]) + "?");


Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Replace an element in an array

Posted 03 November 2009 - 10:38 PM

That will give you a String input, if you parseDouble it, then it will read it in as a double and then you can assign it to the appropriate month in the monthlySales array.

Ex. salesArray[someMonth] = parseDoubleMethod(//the JOptionPane prompt);

This post has been edited by Fuzzyness: 03 November 2009 - 10:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 hackterr  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 293
  • Joined: 13-August 09

Re: Replace an element in an array

Posted 03 November 2009 - 10:45 PM

monthlySales[month the user entered-1]=amount the user entered
or accept the value entered into monthlySales[month the user entered-1] directly

This post has been edited by hackterr: 03 November 2009 - 10:46 PM

Was This Post Helpful? 1
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Replace an element in an array

Posted 03 November 2009 - 10:54 PM

Well, it will be the first one. However slight problem, the JOptionpane InputDIalog wil return a String. So you need to parse it.
= Double.parseDouble(JOptionPane...)
That way you will get a double out of what they enter.
Was This Post Helpful? 1
  • +
  • -

#7 gokujames  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 03-November 09

Re: Replace an element in an array

Posted 03 November 2009 - 11:00 PM

Better option would be to add the month and Salary into a database .If possible sqlserver or use Microsoft Access that comes with microsoft Office.
Then design a VB Winform to do the operation.


If you like the idea reply .I will get u the code.
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Replace an element in an array

Posted 03 November 2009 - 11:04 PM

public class Ch08Ex04
If you look at that class name that means this is definitely a lab that he needs help with not a personal project. Where areas that would be a good idea for personal project, I think he is restricted simply to Java ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1