I'm trying to set items from a method called FootballClub and so far it's fine.
but then I created an arrayList from it and I somehow can't find a way to store this information into a JTable.
The problem is that i cant find a way to set a fixed number of rows
Here is my code:
Class StartLeague:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class startLeague implements LeagueManager{
//setting the frame and other components to appear
public startLeague(){
JButton createTeam = new JButton("Create Team");
JButton deleteTeam = new JButton("Delete Team");
JFrame frame = new JFrame("Premier League System");
JPanel panel = new JPanel();
frame.setSize(1280, 800);
frame.setVisible(true);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String col[] = {"Pos","Team","P", "W", "L", "D", "MP", "GF", "GA", "GD"};
panel.setLayout(new GridLayout(20, 20));
panel.add(createTeam);
panel.add(deleteTeam);
panel.add(new JLabel(""));
//JLabels to fill the space
}
}
FootBall Club Class:
import java.util.ArrayList;
public class FootballClub extends SportsClub{
FootballClub(int position, String name, int points, int wins, int defeats, int draws, int totalMatches, int goalF, int goalA, int goalD){
this.position = position;
this.name = name;
this.points = points;
this.wins = wins;
this.defeats = defeats;
this.draws = draws;
this.totalMatches = totalMatches;
this.goalF = goalF;
this.goalA = goalA;
this.goalD = goalD;
}
The SportsClub Class(abstract):
abstract class SportsClub {
int position;
String name;
int points;
int wins;
int defeats;
int draws;
int totalMatches;
int goalF;
int goalA;
int goalD;
}
And finally, LeagueManager, which is an interface:
import java.util.ArrayList;
public interface LeagueManager {
ArrayList<FootballClub> originalLeagueTable = new ArrayList<FootballClub>();
FootballClub arsenal = new FootballClub(1, "Arsenal", 35, 11, 2, 2, 15, 30, 11, 19);
FootballClub liverpool = new FootballClub(2, "Liverpool", 30, 9, 3, 3, 15, 34, 18, 16);
FootballClub chelsea = new FootballClub(3, "Chelsea", 30, 9, 2, 2, 15, 30, 11, 19);
FootballClub mCity = new FootballClub(4, "Man City", 29, 9, 2, 4, 15, 41, 15, 26);
FootballClub everton = new FootballClub(5, "Everton", 28, 7, 1, 7, 15, 23, 14, 9);
FootballClub tot = new FootballClub(6, "Tottenham", 27, 8, 4, 3, 15, 15, 16, -1);
FootballClub newcastle = new FootballClub(7, "Newcastle", 26, 8, 5, 2, 15, 20, 21, -1);
FootballClub south = new FootballClub(8, "Southampton", 23, 6, 4, 5, 15, 19, 14, 5);
}
Can somebody please help me?
I've trying and trying for days.
Thank you.
"The problem is that i cant find a way to set a fixed number of rows"
You don't need to set the number of rows. Use a TableModel. A DefaultTableModel in particular.
String col[] = {"Pos","Team","P", "W", "L", "D", "MP", "GF", "GA", "GD"};
DefaultTableModel tableModel = new DefaultTableModel(col, 0);
// The 0 argument is number rows.
JTable table = new JTable(tableModel);
Then you can add rows to the tableModel with an Object[]
Object[] objs = {1, "Arsenal", 35, 11, 2, 2, 15, 30, 11, 19};
tableModel.addRow(objs);
You can loop to add your Object[] arrays.
Note: JTable does not currently allow instantiation with the input data as an ArrayList. It must be a Vector or an array.
See JTable and DefaultTableModel. Also, How to Use JTable tutorial
"I created an arrayList from it and I somehow can't find a way to store this information into a JTable."
You can do something like this to add the data
ArrayList<FootballClub> originalLeagueList = new ArrayList<FootballClub>();
originalLeagueList.add(new FootballClub(1, "Arsenal", 35, 11, 2, 2, 15, 30, 11, 19));
originalLeagueList.add(new FootballClub(2, "Liverpool", 30, 9, 3, 3, 15, 34, 18, 16));
originalLeagueList.add(new FootballClub(3, "Chelsea", 30, 9, 2, 2, 15, 30, 11, 19));
originalLeagueList.add(new FootballClub(4, "Man City", 29, 9, 2, 4, 15, 41, 15, 26));
originalLeagueList.add(new FootballClub(5, "Everton", 28, 7, 1, 7, 15, 23, 14, 9));
originalLeagueList.add(new FootballClub(6, "Tottenham", 27, 8, 4, 3, 15, 15, 16, -1));
originalLeagueList.add(new FootballClub(7, "Newcastle", 26, 8, 5, 2, 15, 20, 21, -1));
originalLeagueList.add(new FootballClub(8, "Southampton", 23, 6, 4, 5, 15, 19, 14, 5));
for (int i = 0; i < originalLeagueList.size(); i++){
int position = originalLeagueList.get(i).getPosition();
String name = originalLeagueList.get(i).getName();
int points = originalLeagueList.get(i).getPoinst();
int wins = originalLeagueList.get(i).getWins();
int defeats = originalLeagueList.get(i).getDefeats();
int draws = originalLeagueList.get(i).getDraws();
int totalMatches = originalLeagueList.get(i).getTotalMathces();
int goalF = originalLeagueList.get(i).getGoalF();
int goalA = originalLeagueList.get(i).getGoalA();
in ttgoalD = originalLeagueList.get(i).getTtgoalD();
Object[] data = {position, name, points, wins, defeats, draws,
totalMatches, goalF, goalA, ttgoalD};
tableModel.add(data);
}
You probably need to use a TableModel (Oracle's tutorial here)
How implements your own TableModel
public class FootballClubTableModel extends AbstractTableModel {
private List<FootballClub> clubs ;
private String[] columns ;
public FootBallClubTableModel(List<FootballClub> aClubList){
super();
clubs = aClubList ;
columns = new String[]{"Pos","Team","P", "W", "L", "D", "MP", "GF", "GA", "GD"};
}
// Number of column of your table
public int getColumnCount() {
return columns.length ;
}
// Number of row of your table
public int getRowsCount() {
return clubs.size();
}
// The object to render in a cell
public Object getValueAt(int row, int col) {
FootballClub club = clubs.get(row);
switch(col) {
case 0: return club.getPosition();
// to complete here...
default: return null;
}
}
// Optional, the name of your column
public String getColumnName(int col) {
return columns[col] ;
}
}
You maybe need to override anothers methods of TableModel, depends on what you want to do, but here is the essential methods to understand and implements :)
Use it like this
List<FootballClub> clubs = getFootballClub();
TableModel model = new FootballClubTableModel(clubs);
JTable table = new JTable(model);
Hope it help !
I created an arrayList from it and I somehow can't find a way to store this information into a JTable.
The DefaultTableModel doesn't support displaying custom Objects stored in an ArrayList. You need to create a custom TableModel.
You can check out the Bean Table Model. It is a reusable class that will use reflection to find all the data in your FootballClub class and display in a JTable.
Or, you can extend the Row Table Model found in the above link to make is easier to create your own custom TableModel by implementing a few methods. The JButtomTableModel.java source code give a complete example of how you can do this.
You can do something like what i did with my List< Future< String > > or any other Arraylist, Type returned from other class called PingScan that returns List> because it implements service executor. Anyway the code down note that you can use foreach and retrieve data from the List.
PingScan p = new PingScan();
List<Future<String>> scanResult = p.checkThisIP(jFormattedTextField1.getText(), jFormattedTextField2.getText());
for (final Future<String> f : scanResult) {
try {
if (f.get() instanceof String) {
String ip = f.get();
Object[] data = {ip};
tableModel.addRow(data);
}
} catch (InterruptedException | ExecutionException ex) {
Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
Basic method for beginners like me.
public void loadDataToJtable(ArrayList<String> liste){
rows = table.getRowCount();
cols = table.getColumnCount();
for (int i = 0; i < rows ; i++) {
for ( int k = 0; k < cols ; k++) {
for (int h = 0; h < list1.size(); h++) {
String b = list1.get(h);
b = table.getValueAt(i, k).toString();
}
}
}
}
Related
This question already has answers here:
Java: moving items in array
(6 answers)
Closed 6 years ago.
How to move element of array to specific position on Android JAVA.
We have
int oldPosition, int newPosition
and some like
JSONObject[] tmp = new JSONObject[999];
array of JSONObjects
If you want to just move
tmp[newPosition]=tmp[oldPosition];
Swap
JSONObject jo= tmp[oldPosition];
tmp[oldPosition]=tmp[newPosition];
tmp[newPosition]=jo;
EDIT: There are other ways but you can go through this as well to get your result :)
Exercise : You can understand this logic and use JSONObject type and do necessary changes, Watch out for NullPointerExceptions , handle everything i am lazy to do them
let's say you have int array --> private int array[];
array = new int[]{10,20,30,40,50,60,70,80,90,100};
call this method if you want to swap elements,
swapNumbers(array,9,1);
.
public int[] swapNumbers(int [] arr, int possition1, int possition2){
int temp = arr[possition2];
arr[possition2] = arr[possition1];
arr[possition1] = temp;
System.out.println("array -->" + Arrays.toString(array));
return arr;
}
out put : array[10, 100, 30, 40, 50, 60, 70, 80, 90, 20]
But that doesn't satisfy you?
you need out put to be like this : array[10, 100, 20, 30, 40, 50, 60, 70, 80, 90]
you can use below method
resetUpMyArray(array, array[9],1);
System.out.println("array Finally changed-->" + Arrays.toString(array));
enjoy,
public int[] resetUpMyArray(int[] inputArray, int deleteMeValue,int addMePosition) {
List resultLinkedList = new LinkedList();
for (int itemValue : inputArray)
if (deleteMeValue == itemValue) {
System.out.println("Do not add this value"+itemValue);
} else {
System.out.println("Do add this value "+itemValue +"position-"+deleteMeValue);
resultLinkedList.add(itemValue);
}
System.out.println("array -as new L.L->" + resultLinkedList);
resultLinkedList.add(addMePosition,deleteMeValue);
System.out.println("array -as new L.L all set->" + resultLinkedList);
array = new int[resultLinkedList.size()];
for (int i = 0; i < resultLinkedList.size(); i++) {
array[i] = (int) resultLinkedList.get(i); // Watch out for NullPointerExceptions!
}
return array;
}
Here is two simple algorithm.
Switch values :
switch(array, from, to)
tmp = array[to]
array[to] = array[from]
array[from] = tmp
That will give something like
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|--<---->--|
[10, 20, 60, 40, 50, 30, 70, 80, 90, 100]
This will simply store the values that will be replace at the index to to be place at index from
Move and shift values:
This one will move one values a shift the values next.
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|------------^
[10, 20, , 40, 50, 60, 30, 70, 80, 90, 100]
<-----------
[10, 20, 40, 50, 60, 30, 70, 80, 90, 100]
For this, the solution is quite the same, store the values in tmp but shift every value to fill the gap. This code will only work if from < to
tmp = array[to]
i = from
while(i < to)
array[i] = array[i+1]; --Shift the value
i = i + 1
array[to] = tmp;
I'm stitching a bitmap from a layout array, that puts the a larger bitmap together as a guide. Using multiple bitmaps into one bitmap. What I rotate the whole bitmap, my solution is to restitch it but have the array account for this rotation. The making of the bitmap is determined by the order of the array. The stitching assumes that the array starts from zero and that is the first index or the left top most corner and then goes to the right to the end and goes to beginning of the next row. I would like to have a 90, 180, 270, and 360 functions to call on. I'm thinking 360 is easy I iterate backwards. I'm using 11 by 11 which is constant.
For example
0, 1, 3, 4
5, 6, 7, 8,
9, 10, 11, 12
13, 14, 15, 16
when I rotate 90
4, 8, 12, 16
3, 7, 11, 15
1, 6, 10, 14
0, 5, 9, 13
Try this. This may have performance impact but its a simple solution.
import java.util.Arrays;
public class RotateMatrix {
public static void main(String[] args) {
int original[][] = { { 0, 1, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
printArray(original);
System.out.println("After 90 Degree");
printArray(rotate(original, 90));
System.out.println("After 180 Degree");
printArray(rotate(original, 180));
System.out.println("After 270 Degree");
printArray(rotate(original, 270));
System.out.println("After 360 Degree");
printArray(rotate(original, 360));
}
private static int[][] rotate(int[][] original, int angle) {
int[][] output = original;
int times = 4 - angle/90;
for(int i=0; i<times; i++){
output = rotate(output);
}
return output;
}
private static int[][] rotate(int[][] original) {
int n = original.length;
int m = original[0].length;
int[][] output = new int[m][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
output[j][n - 1 - i] = original[i][j];
return output;
}
private static void printArray(int[][] array) {
for (int i = 0; i < array.length; i++) {
System.out.println(Arrays.toString(array[i]));
}
}
}
This rotates the array anti-clockwise direction just as in your example. However if you want to change this to clockwise just change int times = 4 - angle/90; to int times = angle/90; in rotate(int[][] original, int angle) method.
I want to use SharedPreferences in the code for LineGraph:
public class LineGraph{
Context applicationContext;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
public Intent getIntent(Context context) {
// Our first data
int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values!
int[] y = { 30, 34, 45, 57, 77, 89, 100, 111 ,123 ,145 }; // y values!
TimeSeries series = new TimeSeries("Line1");
for( int i = 0; i < x.length; i++)
{
series.add(x[i], y[i]);
}
// Our second data
int[] x2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values!
int[] y2 = { 145, 123, 111, 100, 89, 77, 57, 45, 34, 30}; // y values!
TimeSeries series2 = new TimeSeries("Line2");
for( int i = 0; i < x2.length; i++)
{
series2.add(x2[i], y2[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
dataset.addSeries(series2);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
mRenderer.setZoomButtonsVisible(true);
XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
XYSeriesRenderer renderer2 = new XYSeriesRenderer(); // This will be used to customize line 2
mRenderer.addSeriesRenderer(renderer);
mRenderer.addSeriesRenderer(renderer2);
// Customization time for line 1!
renderer.setColor(Color.WHITE);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
// Customization time for line 2!
renderer2.setColor(Color.YELLOW);
renderer2.setPointStyle(PointStyle.DIAMOND);
renderer2.setFillPoints(true);
Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Line Graph Title");
return intent;
}
}
But the SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
does not work, it stopped the application. Why?
Call this within your method, not at the class level.
Something like this:
public class LineGraph {
public Intent getIntent(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Our first data
int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values!
...
The way you had it, applicationContext was always going to be null.
Please provide your logcat-output.
Apart from this I assume that you receive a nullpointer exception when itializing the shared preferences, because the applicationContext-variable is null.
Place the initialization of your shared preferences into the getIntent()-method and pass the context-argument.
I tried adding another set of values to the demo example of stacked bar charts with achartengine, but the new values introduced by me don't appear on the chart. Is Stacking bars limited to two bars?
public Intent getIntent(Context context) {
String[] titles = new String[] { "Good", "Defect", "Repair" };
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 14230, 12300, 1424, 15240, 15900, 19200,
22030, 21200, 19500, 15500, 12060, 14000 });
values.add(new double[] { 14230, 12300, 14240, 15244, 15900, 19200,
22030, 21200, 19500, 15500, 12600, 14000 });
values.add(new double[] { 5230, 7300, 9240, 10540, 7900, 9200, 12030,
11200, 9500, 10500, 11600, 13500 });
int[] colors = new int[] { Color.GREEN, Color.YELLOW, Color.RED };
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
setChartSettings(renderer, "Machine Efficiency Rates",
"Machine", "NET produce", 0.5, 12.5, 0, 24000, Color.GRAY,
Color.LTGRAY);
renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
renderer.getSeriesRendererAt(2).setDisplayChartValues(true);
renderer.setXLabels(12);
renderer.setYLabels(5);
renderer.setXLabelsAlign(Align.LEFT);
renderer.setYLabelsAlign(Align.LEFT);
renderer.setPanEnabled(true, false);
renderer.setZoomEnabled(true);
renderer.setZoomRate(1.1f);
renderer.setBarSpacing(0.5f);
return ChartFactory.getBarChartIntent(context,
buildBarDataset(titles, values), renderer, Type.STACKED);
}
There is a misunderstanding in the way AChartEngine displays stacked bar charts. It doesn't really stack them, but displays them one over the other. This means that you will want to always add the bigger values first then the smaller and so on as it renders the first series, the second one above the first and so on.
UPDATE: As of version 1.2.0, there is the new HEAP stacked bar charts, which are displayed in the manner you need.
I could not find a good answer. However, you can use the getCombinedXYChartIntent
to create a multiple stacked bars.
I use the CombinedTemperatureChart as a template and modified to achieve this.
The initial raw code is this
package org.achartengine.chartdemo.demo.chart;
import java.util.ArrayList;
import java.util.List;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.BubbleChart;
import org.achartengine.chart.CubicLineChart;
import org.achartengine.chart.LineChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.model.XYValueSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
/**
* Combined temperature demo chart.
*/
public class CombinedTemperatureChart extends AbstractDemoChart {
/**
* Returns the chart name.
*
* #return the chart name
*/
public String getName() {
return "Combined temperature";
}
/**
* Returns the chart description.
*
* #return the chart description
*/
public String getDesc() {
return "The average temperature in 2 Greek islands, water temperature and sun shine hours (combined chart)";
}
/**
* Executes the chart demo.
*
* #param context the context
* #return the built intent
*/
public Intent execute(Context context) {
String[] titles = new String[] { "Crete Air Temperature", "Skiathos Air Temperature" };
List<double[]> x = new ArrayList<double[]>();
for (int i = 0; i < titles.length; i++) {
x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
}
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2, 13.9 });
values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });
int[] colors = new int[] { Color.GREEN, Color.rgb(200, 150, 0) };
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND };
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
//renderer.setPointSize(5.5f);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
r.setDisplayChartValues(true);
r.setChartValuesTextSize(25);
//r.setLineWidth(2);
r.setFillPoints(true);
}
setChartSettings(renderer, "Weather data", "Month", "Temperature", -1, 12.5, 0, 40,
Color.LTGRAY, Color.LTGRAY);
renderer.setXLabels(12);
renderer.setYLabels(10);
renderer.setShowGrid(true);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setZoomButtonsVisible(true);
renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
XYValueSeries sunSeries = new XYValueSeries("Sunshine hours");
sunSeries.add(0.5, 17);
sunSeries.add(1.5, 18);
sunSeries.add(2.5, 19);
sunSeries.add(3.5, 19);
sunSeries.add(4.5, 20.8);
sunSeries.add(5.5, 24.9);
sunSeries.add(6.5, 26);
sunSeries.add(7.5, 27.8);
sunSeries.add(8.5, 28.4);
sunSeries.add(9.5, 25.5);
sunSeries.add(10.5, 23.5);
sunSeries.add(11.5, 19.5);
XYSeriesRenderer lightRenderer = new XYSeriesRenderer();
lightRenderer.setColor(Color.YELLOW);
XYSeries waterSeries = new XYSeries("Water Temperature");
waterSeries.add(0.5, 16);
waterSeries.add(1.5, 15);
waterSeries.add(2.5, 16);
waterSeries.add(3.5, 17);
waterSeries.add(4.5, 20);
waterSeries.add(5.5, 23);
waterSeries.add(6.5, 25);
waterSeries.add(7.5, 25.5);
waterSeries.add(8.5, 26.5);
waterSeries.add(9.5, 24);
waterSeries.add(10.5, 22);
waterSeries.add(11.5, 18);
renderer.setBarSpacing(2);
renderer.setBarWidth(2);
XYSeriesRenderer waterRenderer = new XYSeriesRenderer();
waterRenderer.setColor(Color.argb(250, 0, 210, 250));
XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
dataset.addSeries(0, sunSeries);
dataset.addSeries(1, waterSeries);
renderer.setBarWidth(0.5f);
renderer.addSeriesRenderer(lightRenderer);
renderer.addSeriesRenderer(waterRenderer);
waterRenderer.setDisplayChartValues(true);
waterRenderer.setChartValuesTextSize(25);
lightRenderer.setDisplayChartValues(true);
lightRenderer.setChartValuesTextSize(25);
String[] types = new String[] { BarChart.TYPE, BarChart.TYPE, BarChart.TYPE, BarChart.TYPE };
Intent intent = ChartFactory.getCombinedXYChartIntent(context, dataset, renderer, types,
"Weather parameters");
return intent;
}
}
I hope someone still need this, it take a some time figure it out.
Unfortunately, I don't have the points to paste an image.
I need to create an array of integers contained within an array of words. I keep getting an error
cannot convert from int[] to java.lang.String[]
where it says
private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};
This is my code
public class Budget{
///////////////fields////////////////
int expenseAmount[] = {1000, 2000, 3000, 4000, 5000, 6000};
int monthNumber[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int clothes[]= {100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
int tuition[] = {200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200};
int transportation[]={100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
int food[]={80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};
int housing[]={150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150};
int books[]= {200, 0, 0, 0, 0, 0, 0, 300, 0, 0, 0, 0};
int i=0; // this is arbitrary. Never hard code numbers unless that number is never going to change. in that case you make a variable and define it.
int month_num;
private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};
/////////constructors///////////////
public Budget() {}
public Budget(int name) {this.expenseName[i][i] = name;}
public Budget(String name, int clothes, int tuition, int transportation, int food, int housing, int books)
{
this.expenseName[i] = name;
this.clothes[i] = clothes;
this.tuition[i] = tuition;
this.transportation[i] = transportation;
this.food[i] = food;
this.housing[i] = housing;
this.books[i] = books;
this.monthNumber[i] = month_num;
}
/////////////methods///////////
public String getexpenseName() {return expenseName[i][i];}
public int getclothes() {return clothes[i];}//change the I
public int gettuition() {return tuition[i];}
public int gettransporation() {return transportation[i];}
public int getfood() {return food[i];}
public int gethousing() {return housing[i];}
public int books() {return books[i];}
public int getmonthNumber() {return monthNumber[i];}//change the i
public void setExpenseName(String name)
{
this.expenseName[i][i] = name;
}
public boolean setexpenseAmount(int num)
{
if (this.expenseAmount[i] == 0)
{this.expenseAmount[i] = num;
return true;
}
else
return false;
You need quotes around your strings:
private String[] expenseName = {"clothes", "tuition", "etc"};
or you need to declare it int[][]
private int[][] expenseName = {clothes, tuition};
As the error message clearly states, you can't put an int[] into an array of String[]s.
Change
private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};
to
private int[][] expenseName = {clothes, tuition, transportation, food, housing, books};
In Java, you cannot use an array of int as an array of String.
That will just not work, like the error says.
I guess that you must be familiar with some language where there is no type checking and you can do whatever you want. But this is Java.
You have to convert the int[] to String[], for example like this.
public class ArrayIntToString {
public static void main(String[] args) {
int[] numbers = { 1, 2, 3 };
String[] numbersAsStrings = new String[numbers.length];
int i = 0;
for (Integer number : numbers) {
numbersAsStrings[i++] = number.toString();
}
}
}