I'm declate the multidimensional array propertyArray = new int[numbHuman][40]
where [numbHuman] is the number of rows, and the [40] is the number of columns (or vice versa doesnt really matter). Anyway I create those propertyArray's in a nested for loop, but I'm not sure How I'd pass propertyArray[][] into the player object constructor. Here is my code, I will try to clarify if needed.
import java.util.Scanner;
import java.util.Random;
public class PlayerArray
{
Scanner scan = new Scanner(System.in);
private int numbHuman;
private Player[] arr;
private String[] userName;
//private
private int[] testArray;
private int[][] propertyArray;
private int[] userID;
private int startingMoney;
private int startingPosition;
private int b;
private int c;
private int i;
//private int d;
public PlayerArray()
{
Scanner scan = new Scanner(System.in);
System.out.println("How many players wish to play? Values of 2 through 8 accepted.");
numbHuman = scan.nextInt();
System.out.println(numbHuman + " players selected.");
while (numbHuman < 2 || numbHuman > 8)
{
System.out.println("Invalid entry, try again.");
numbHuman = scan.nextInt();
}
arr = new Player[numbHuman];
i=0;
testArray = new int[40];
propertyArray = new int[numbHuman][40];///// WORK ON DIS
userName = new String[numbHuman];
userID = new int[numbHuman];
startingMoney = 1500;
startingPosition = 0;
b=0;
c=0;
for(int i = 0; i < arr.length; i++)
{
userID[i] = b;
System.out.println("Player " + (i + 1) + ", Please enter your first name:");
userName[i] = scan.next();
for(c = 0; c < 40; c++)
{
propertyArray[i][c] = 0;
}
arr[i] = new Player(userName[i],startingMoney,userID[i],startingPosition,propertyArray[i][c]);
b++;
}
}
I'm trying to pass it into, how do i go about this? All the other variables work but the multidimensional array.
public Player(String userName, int changeInMoney, int userID, int startingPosition, int[][]propertyArray)
{
myID = userID;
myName = userName;
currentPosition += startingPosition;
currentBal -= payBank(changeInMoney);
}
You need to specify a set of square brackets for each dimension, e.g.:
public Player(String userName, int changeInMoney, int userID,
int startingPosition, int[][] propertyArray)
Just add an extra pair of brackets:
public Player(String userName, int changeInMoney, int userID, int startingPosition, int[][] propertyArray)
Related
I have an array and I want to assign values in group of 3s
Name, Id, Population, Name, Id, Population, Name, Id, Population etc.
Is there a way to do that?
This is what I have
while (scanner.hasNext()) { `
scanner.useDelimiter(",");`
list.add(scanner.nextLine());}`
for(int i=0; i<list.size(); i++){
String n = list.get(i);
System.out.println("Hopefully going thru " + n);} //for me to check
String ar =list.toString();
Object [] a = ar.split(",");// splitting the array for each string
for(int h=0;h<a.length;h+=3) { // for [0] += 3 is Name
for(int j=1;j<a.length; j+=3) { // for [1] += 3 is Id
for(int k=2; k<a.length;k+=3) { //for[2]+= is Population
String name = a[h].toString();
String id = a[j].toString();
String population = a[k].toString();
System.out.println("name is "+ name);// this is just to check correct values
System.out.println("id is "+ id);// this is just to check correct values
System.out.println("population is " +population);// this is just to check correct values
CityRow cityRow = new CityRow(name,id,population); //?? I want every set of [0][1][2] to create a new object`
I don‘t think that ar has the correct data and I don‘t understand why you don’t work with list directly, but assuming that ar has the correct data, it should be possible to use:
for(int = 0; i < ar.length ; ) {
var cityRow = new CityRow(
ar[i++],
ar[i++],
ar[i++]
);
// remember to add cityRow to an
// appropriate list
}
You use Scanner so no need to split an array. You can read each separate value one-by-one directly from it.
public class Main {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\\n|,");
System.out.print("Total groups: ");
int total = scan.nextInt();
List<City> cities = readCities(scan, total);
printCities(cities);
}
private static List<City> readCities(Scanner scan, int total) {
List<City> cities = new ArrayList<>(total);
System.out.println("Enter each city on a new line. Each line should be: <id>,<name>,<population>");
for (int i = 0; i < total; i++) {
String id = scan.next();
String name = scan.next();
int population = scan.nextInt();
cities.add(new City(id, name, population));
}
return cities;
}
private static void printCities(List<City> cities) {
System.out.println();
System.out.format("There are total %d cities.\n", cities.size());
int i = 1;
for (City city : cities) {
System.out.format("City №%d: id=%s, name=%s, population=%d\n", i++, city.id, city.name, city.population);
}
}
static class City {
private final String id;
private final String name;
private final int population;
public City(String id, String name, int population) {
this.id = id;
this.name = name;
this.population = population;
}
}
}
I want to implement a two-dimensional array using user input. I have a Book class. It has two variables: int price and String name. I want to store 5 books information in a two-dimensional array. Book class code is below:
public class Book {
String name;
int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
Main Class code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int l = 5;
Book[][] bk = new Book[l][l];
for (int i = 0; i < l; i++) {
for (int j = 0; j < l; j++) {
// here i want to take user input.
System.out.println("Enter Song: ");
String sname = in.next();
System.out.println("Enter price: ");
int sprice = in.nextInt();
// in this line i am getting type
// error int can't convert to string
song[i][j] = song[sname][price];
}
}
}
You never declared the array song, maybe you wanted to write
bk[i][j] = ...
Now you want to create a new "Book" for every sname and sprice that you read, so you have two options:
1) In each iteration create a new empty Book
Book tmp = new Book();
then you set his Name and his Price
tmp.setName(sname);
tmp.setPrice(sprice);
and then you assign the new Book to the current element of bk
bk[i][j] = tmp;
or
2) Add a constructor to the class Book that has Name and Price as parameters
public Book(String n, int p){
name = n;
price = p;
}
and use it to instantly create a new Book and assign it to the current element of bk
bk[i][j] = new Book(sname, sprice);
So what you require is Book[String name][int price]. That is not how 2D arrays work.
While declaring:
int l = 5;
Book[][] bk = new Book[l][l];
You are implementing a 2D Book array that can have 25 book records.
1D array of Books is sufficient for your requirements.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int l = 25;//any size you can have
Book[] bk = new Book[l];
for (int i = 0; i < l; i++) {
System.out.println("Enter Song: ");
String sname = in.next();
System.out.println("Enter price: ");
int sprice = in.nextInt();
Book book = new Book();
book.setName(sname);
book.setprice(sprice);
bk[i] = book;
}
}
I have a test class below that calls my shuffle class which creates an ArrayList of random numbers and orders them. When I call this ArrayList from my testing class nothing happens.
static ArrayList yup = new ArrayList();
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a num to create a random list of numbers: " );
int guess = input.nextInt();
shuffle y = new shuffle(guess);
System.out.println(y.toString());
ArrayList<Integer> jh = y.getnew_list();
for(int d = 0; d < jh.size();d++){
//System.out.println(three.get(d));
System.out.println(jh.get(d));
}
shuffle one = new shuffle(guess);
}
Shuffle class:
import java.util.*;
import java.util.Random;
public class shuffle implements Comparable <shuffle> {
static int size;
static ArrayList new_list = new ArrayList();
shuffle(int size){
this.size = size;
Random rand = new Random();
for(int i = 0; i<new_list.size();i++){
int t = rand.nextInt(50) + 1;
new_list.add(t);
}
}
public ArrayList getnew_list(){
return this.new_list;
}
public int getSize(){
return size;
}
public String toString(){
String str = new String();
for(int i = 0; i<new_list.size();i++){
str += " " + new_list.get(i);
System.out.println(str);
}
return str;
}
public int compareTo(shuffle that) {
if(this.getSize() > that.getSize()){
return 1;
}
if(this.getSize() < that.getSize()){
return -1;
}
else
return 0;
}
}
in the shuffle class you are not initializing the size variable and you are using it as uninitialized variable that may be the one of the reason.
import java.util.*;
import java.util.Random;
public class shuffle implements Comparable <shuffle> {
static int size;
static ArrayList new_list = new ArrayList();
shuffle(int size){
this.size = size;
Random rand = new Random();
for(int i = 0; i<new_list.size();i++){
int t = rand.nextInt(50) + 1;
new_list.add(t);
I'm trying to send a one D array to a class. I want the mutator method to copy the array, save it and then send it back to my tester class...so far I keep getting errors from the compiler. I know how to write a method that copies the array in main but I can't seem to get it done with the class though. Here is the code:
public class Class1D {
private int degree;
private int [] coefficient;
public Class1D(int degree){
this.degree =degree;
}
public void setCoefficent( int[] a, int degree){
this.coefficient[degree] = a[degree];
for ( int i=0; i<=a.length-1; i++)
{
this.coefficient[i] = a[i];
}
}
public int [] getCoefficient() {
return coefficient;
}
}
import javax.swing.JOptionPane;
public class copy1D{
public static void main(String[]args)
{
String input;
int degree;
input = JOptionPane.showInputDialog(" what is the degree of the polynomial?");
degree = Integer.parseInt(input);
degree= degree+1;
int [] array = new int[degree];
for ( int i =0; i<=array.length-1; i++)
{
input = JOptionPane.showInputDialog(" Enter coefficients:");
array[i] = Integer.parseInt(input);
}
for ( int i =0; i<=array.length-1; i++)
{
System.out.print(array[i] + " ");
}
Class1D array2 = new Class1D(degree);
}
}
}
You should send the a array to the constructor of Class1D and set it to your member variable as follows:
public Class1D(int degree, int[] a){
this.degree =degree;
this.coefficient = a.clone();
// You can also another way that is faster, use:
// System.arraycopy(a, 0, this.coefficient, 0, a.length);
}
And your call to the construction will be:
Class1D c1d = new Class1D(degree, array);
import javax.swing.JOptionPane;
public class copy1D{
public static void main(String[]args)
{
String input;
int degree;
input = JOptionPane.showInputDialog(" what is the degree of the polynomial?");
degree = Integer.parseInt(input);
degree= degree+1;
int [] array = new int[degree];
for ( int i =0; i<=array.length-1; i++)
{
input = JOptionPane.showInputDialog(" Enter coefficients:");
array[i] = Integer.parseInt(input);
}
for ( int i =0; i<=array.length-1; i++)
{
System.out.print(array[i] + " ");
}
makecopy(array,degree);
Class1D c1d = new Class1D(degree, array);
}
public static void makecopy(int[]a, int deg)
{
int [] b = new int [deg];
for ( int i =0; i<=a.length-1; i++)
{
b[i] = a[i];
}
System.out.println(" I have copied the array ");
for ( int i =0; i<=a.length-1; i++)
{
System.out.print(b[i]+ " ");
}
}
}
public class Class1D {
private int degree;
private int [] coefficient;
public Class1D(int degree){
this.degree =degree;
}
public Class1D(int degree, int[] a){
this.degree =degree;
this.coefficient = a.clone();
}
public int []getCoefficient() {
return coefficient;
}
}
Below is my code. Here, i have to switch two of the names in the 2D array but i'm not sure how to do this.
Anyone knows how to do?
import java.util.Scanner;
public class Homeworktest {
public static void main(String[] args) {
String[][] people = new String[3][3];
people[0][0] = "April";
people[0][1] = "Jenny";
people[0][2] = "Charlie";
people[1][0] = "Maya";
people[1][1] = "Daniel";
people[1][2] = "Felix";
people[2][0] = "Jack";
people[2][1] = "Charlotte";
people[2][2] = "Nick";
for(int i = 0; i < people.length; i++) {
String[] subarrays = seatingChart[i];
for(int y = 0; y < people.length; y++) {
System.out.print(subarrays[y] + " ");
}
System.out.println();
}
public static void switchSeats(int row1, int col1, int row2, int col2) {
}
}
}
To swap values of two array locations, first, hold one value in a temporary variable, assign the second location's value to the first location and lastly assign the temporary variable's value to the second location.
public static void switchSeats(String[][] people, int row1,
int col1, int row2, int col2) {
String tmp = people[row1][col1];
people[row1][col1] = people[row2][col2];
people[row2][col2] = tmp;
}
In the above code, tmp serves as the temporary variable.