9 Replies - 613 Views - Last Post: 02 October 2009 - 08:18 PM Rate Topic: -----

#1 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

help w/ arrays and input

Posted 30 September 2009 - 08:01 AM

I am having trouble creating an array that will accept input.
I want to make a simple program with an array and the only import is import javax.swing.JOptionPane; so now scanners or anything.....trying to keep this as simple as possible
the program is for tracking monthly sales
the program will ask the user for the monthly income of sales.......for each month
i am trying to keep the numbers low so each monthly income will between 0 and 50
each of these values need to be stored in the array
only one array and not a 2D
after all the input i need to calculate and output the correct average for the monthly sales
i also need to output a bar graph for the monthly sales as well

the following code i have was just to test my understanding of arrays......which isnt much
i am pretty sure i should NOT declare each of the array values as a string
prolly should do everything as an int
i was trying to declare each array entry as an string for the months and then i was gonna go and add int values but im sure that would interfere with all the calculations n such
oh well i know my code is a mess and i had the println statement only to see if it would output wut i put in for values

any help would be welcome

import javax.swing.JOptionPane;

public class Lab5
{
  public static void main(String[] args)
  {
	String[] months = new String[12];
	months[0] = ("January");
	months[1] = ("February");
	months[2] = ("March");
	months[3] = ("April");
	months[4] = ("May");
	
	int[] sales = new int[12];
	int input = (Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the total sales for this month")));
	
	for(int index = 0; index < sales.length; index++)
	{
	  sales[index] = input;
	  System.out.println(sales[index]);
	}
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: help w/ arrays and input

#2 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: help w/ arrays and input

Posted 30 September 2009 - 08:06 AM

Month's can be identified by their array position + 1 e.g. month[0] would be January where month[2] would be March.

Create an array just like you have done for the month's without any values. Then ask the user for input and assign the input to the variables much like you have in the above example.

I haven't played around with Java at all so I can only help out with the logic ... I am sure someone else could help you out with exact syntax if you need it

This post has been edited by danny_kay1710: 30 September 2009 - 08:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

Re: help w/ arrays and input

Posted 30 September 2009 - 08:14 AM

i was originally just going to set int values anywayz but i thought it might make it confusing with array[12] starting at [0]
but it doesnt matter to me as long as i can get the program to work
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help w/ arrays and input

Posted 30 September 2009 - 04:48 PM

An you can't separate your loops

Here is how to input your numbers
now you can use another loop for the results... avearge, total, ...

  public static void main(String[] args) {
  
	String[] months = {"January", "February", "March", .....};
	int[] sales = new int[months.length];
	for(int i = 0; i < sales.length; i++) {
	   sales[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the total sales for " + months[i]));
	}
  }


Was This Post Helpful? 1
  • +
  • -

#5 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

Re: help w/ arrays and input

Posted 01 October 2009 - 07:27 AM

ok i tried to make something that would calculate the total of all the months sales and it did not work at all
thought the way i had the code was good but i guess i messed up somewhere
any help would be welcome


import javax.swing.JOptionPane;

public class array2
{

public static void main(String[] args) {
 
	String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	int[] sales = new int[months.length];
	for(int i = 0; i < sales.length; i++) {
	   sales[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the total sales for " + months[i]));
	}
	 calculateTotal();
}
public static voide calculateTotal()
{
	int total=0;
for(int i=0; i < months.length; i++)
{
total=months[i]+months[i+1];
System.out.println(total);
	
  }


}
}




This post has been edited by kilowog: 01 October 2009 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#6 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: help w/ arrays and input

Posted 01 October 2009 - 08:31 AM

there is slight change and done

import javax.swing.JOptionPane;
public class sam
{
  public static void main(String[] args)
  {
	String[] months = new String[12];
	months[0] = ("January");
	months[1] = ("February");
	months[2] = ("March");
	months[3] = ("April");
	months[4] = ("May");
   
	int[] sales = new int[12];
	int input,total=0,avg;
	for(int index = 0; index < sales.length; index++)
	{
input= (Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the total sales for this month no  "+index)));
sales[index] = input;
total=sales[index]+total;
	  System.out.println("the sales for this month is"+sales[index]);
	}
avg=total/sales.length;

System.out.println("the total sales of all the months is"+total);
System.out.println("avg is"+avg);  
}
}



Was This Post Helpful? 1
  • +
  • -

#7 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

Re: help w/ arrays and input

Posted 01 October 2009 - 03:43 PM

ok the code works im pretty sure for now
the only problem now is to output all the data as a bar graph
not sure how i do this if i cant import anything

should prolly do it by printing out things like :
============
====
======
=========
===

import javax.swing.JOptionPane;

public class Lab5
{

public static void main(String[] args) {
 
	String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	int[] sales = new int[months.length];
	int input,total=0,avg;
	for(int i = 0; i < sales.length; i++) {
	  input = (Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the total sales for  " + months[i])));
	   sales[i] = input;
	   total = sales[i]+total;
	  System.out.println("the sales for this month is "+sales[i]);
	  }
avg=total/sales.length;

System.out.println("the total sales of all the months is "+total);
System.out.println("avg is "+avg);  




}
}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help w/ arrays and input

Posted 01 October 2009 - 03:46 PM

View Postkilowog, on 1 Oct, 2009 - 06:27 AM, said:

ok i tried to make something that would calculate the total of all the months sales and it did not work at all
thought the way i had the code was good but i guess i messed up somewhere
any help would be welcome


You will have to move the variables months and sales out of the main method so that the other method can see them
So better to create an Array2 object and calls its methods so we can also get ride of the static stuff


import javax.swing.JOptionPane;

public class array2
{
	String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	int[] sales = new int[months.length];

 
private void inputData() {

	for(int i = 0; i < sales.length; i++) {
	   sales[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the total sales for " + months[i]));
	}
}
	

public void calculateTotal()
{
	int total=0;
for(int i=0; i < months.length; i++)
{
total = total + months[i]	 // no need to access that month -> +months[i+1];
}
System.out.println(total);
}	


public static void main(String[] args) {
			 // to have an Array2 object with the variables declared 
			 Array2 ar = new Array2();
			 ar.inputData();
			 ar.calculateTotal();
}

}




Was This Post Helpful? 0
  • +
  • -

#9 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

Re: help w/ arrays and input

Posted 02 October 2009 - 02:10 PM

so for the printing of the array into a horizontal bar graph im not sure what the code would be like
=====
====
========
==
=====

sort of like that but i am not sure wut to do
would it be a for loop?
my book only has printing an array as a list and not much explanation on that as it is
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help w/ arrays and input

Posted 02 October 2009 - 08:18 PM

Not difficult
depend number of = you want for a sale. Depends if your sales are between 1 and 50 or 1 and 10,000
// for each mont
for(int i = 0; i < month.length; i++) {
   // print month name
   System.out.print(months[i] + ": ");
   // a dash for every 10 value may be
   int nbDash = sales[i] / 10;
   for(j = 0; j < nbDash; j++)
	  System.out.print("=");
   // put a new line
   System.out.println();
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1