Card Game issue in Java - java

In my code I cannot find a way to assign my human and dealer variables in my method PlayGame() to their respective strings that are made from DealCards().
Whenever I run javac I get an error String[] cannot be converted to String. I don't understand this however because they are separate strings in the dealerOther array in the method DealCards(). Could someone please give me some help on this issue.
import java.util.Random;
public class GAME_8315085 {
static Random rand = new Random();
public static String[] MakeDeck() {
String[] deck=new String[51];
int k=0;
String[] suits={"\u2660", "\u2661", "\u2662", "\u2663"};
String[] ranks={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for (int i=0;i<suits.length;i++) {
for (int j=0;j<ranks.length;j++) {
if (suits[i].equals("\u2663") && ranks[j].equals("Q")) {
;
}
else {
deck[k]=ranks[j]+suits[i];
k++;
}
}
}
return deck;
}
public static void ShuffleDeck(String[] deck) {
for (int i=0;i<deck.length;i++) {
int random = rand.nextInt(deck.length);
String tmp=deck[i];
deck[i] = deck[random];
deck[random]=tmp;
}
}
public static String[][] DealCards(String[] deck) {
String[][] dealerOther = new String[2][26];
int j=0;
for (int i=0; i<=deck.length;i+=2) {
dealerOther[0][j]=deck[i];
j++;
}
j=0;
for (int i=1; i<deck.length;i+=2) {
dealerOther[1][j]=deck[i];
j++;
}
return dealerOther;
}
public static void PlayGame() {
String[] deck = MakeDeck();
ShuffleDeck(deck);
String[][] tmp=DealCards(deck);
String human=tmp[0];
String dealer=tmp[1];
}
public static void main(String[] args) {
PlayGame();
}
}

Problem is here
String[][] tmp=DealCards(deck);
String human=tmp[0];
String dealer=tmp[1];
Since tmp is array of arrays of string(kind of 2-D array), the human and dealer has to be array of strings.
Change it to
String[][] tmp=DealCards(deck);
String[] human=tmp[0];
String[] dealer=tmp[1];

Related

Array Method issue

having a problem with my java program. I am a newbie to Java and just can't figure out what is exactly the issue with it. In short I've declared an array and a variable in main, I've created my method call and would like my array be passed into my method with the variable. I would then like the method to take my array and count the number of times my variable "8" occurs, get rid of the 8 out of the array and return a new smaller array back to main. Here is my code below. I feel as if I am just missing one block code any suggestions?
public class Harrison7b
{
public static void main(String [] args)
{
int[] arrayA = {2,4,8,19,32,17,17,18,25,17,8,3,4,8};
int varB = 8;
// Call with the array and variable you need to find.
int[] result = newSmallerArray(arrayA, varB);
for(int x = 0; x < arrayA.length; x++)
{
System.out.print(arrayA[x] + " ");
}
}
public static int[] newSmallerArray( int[] arrayA, int varB)
{
int count = 0;
for(int x = 0; x < arrayA.length; x++)
{
if(arrayA[x] == varB)
{
count++;
}
}
int [] arrayX = new int[arrayA.length - count];
for(int B = 0; B < arrayA.length; B++)
{
if(arrayA[B] != varB)
{
}
}
return arrayX;
}
}
you do not actually need to return the array because when you pass an array to a method you also pass its memory address meaning its the same address that you change so, it will also change the arraysA of main method because you are just changing the values of the same memory adress
import java.util.*;
public class Help
{
public static void main(String[] args)
{
ArrayList<Integer> arraysA = new ArrayList<Integer>();
arraysA.add(Integer.valueOf(2));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(19));
arraysA.add(Integer.valueOf(32));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(18));
arraysA.add(Integer.valueOf(25));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(3));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
int varB=8;
newSmallerArray(arraysA,varB);
for(Integer i:arraysA)
{
System.out.println(i);
}
}
public static void newSmallerArray(ArrayList<Integer> arraysA,int varB)
{
for(int i=0;i<arraysA.size();++i)
{
if(Integer.valueOf(arraysA.get(i))==varB)
{
arraysA.remove(i);
}
}
}
}
Try this code it will not require for loop:
List<Integer> list = new ArrayList<Integer>(Arrays.asList(arrayA));
list.removeAll(Arrays.asList(8));
arrayA = list.toArray(array);

I'm trying to make a java array code

i'm trying to build a code for different actions in arrays with different methods (Constructors) but i cannot seem to find a way to link them...I've made a array with 100 elements and a random variable to fill it....i'd like to know how to get my random elements from the first methods to the second one to make the comparing.... and also i'd like to not include in the 0 element in the random generator..any help?
this is my code
import java.util.*;
public class prova1{
public int min;
public void tabele(){
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
System.out.println(e[i]);
}
}
public void emin(){
Random r = new Random();
int d;
int e[]=new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
if(e[i]<min){
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
prova.tabele();
prova.emin();
}
}
Return the array from tabele to a local variable and send it as parameter to emin
import java.util.*;
public class prova1 {
public int min;
public int[] tabele() {
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(99) + 1;
e[i]=d;
System.out.println(e[i]);
}
return e;
}
public void emin(int[] e) {
Random r = new Random();
int d;
for(int i=0; i<e.length;i++) {
if(e[i]<min) {
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
int[] arr = prova.tabele();
prova.emin(arr);
}
}

Displaying a shuffled integer array in java

sorry I'm really new to all of this. I know this is a stupid/easy question, but how would I display the shuffled array after i've set it all up. I have my code below and made the class that creates the array and has the algorithm for shuffling the integers inside the array. But I can't figure out how to display the shuffled array. Heres my code below:
My main:
package lab4b;
import java.util.Scanner;
public class Lab4B {
public static void main(String[] args) {
Shuffler test = new Shuffler(15);
test.Shuffle();
test.display();
}
}
and my Shuffle class:
package lab4b;
import java.security.SecureRandom;
public class Shuffler {
private static final SecureRandom randomNumbers = new SecureRandom();
private int [] data;
public Shuffler (int size){
data = new int [size];
for(int i = 0; i<data.length;i++){
data[i]= i+1;
}
}
public void Shuffle(){
int temp;
for(int first = 0; first<data.length; first++){
int second = randomNumbers.nextInt(data.length);
temp = data[first];
data[first] = data[second];
data[second] = temp;
}
}
public void display()
{
for(int counter =0; counter<data.length; counter++ ){
System.out.print(data[counter]+ " ");
}
System.out.println();
}
}
In this loop you are reset the value of the dataarray
for(int counter =0; counter<data.length; counter++ ){
// data[counter] = counter + 1; - do not do this
System.out.print(data[counter]+ " ");
}

Cannot find symbol (method), same code in different classes/files, 1 compiles, 1 dooesn't

The problem is in method "calculusRank". If I delete that method and related parts to it, aplication will work, if I create a different notepad with same code(method and main code related to that method) it's work.
Also I wanna say I'm a beginner and this is my first "relative" large application.
This is the code with problem that doesn't compile:
import java.util.Random;
import java.lang.Math;
public class MultilevelSystem1 {
static String[][] createMembers (int a) {
Random nr = new Random ();
String[][] membersName = new String [a][2];
for(int j=0; j<2;j++)
for(int i=0; i<a; i++) {
if(j==0||i==0) membersName[i][j]="Nume"+(i+1);
else membersName[i][j]="Nume"+(nr.nextInt(i)+1);
}
return membersName;
}
static String[][] createIncomes (String[][] a,int b){
Random nr = new Random ();
String[][] incomes = new String [b][2];
int j=0;
for(int i=0;i<a.length && j<b;i++)
if(nr.nextInt(2)==1){ incomes[j][0]= a[i][0];
incomes[j][1]=Integer.toString((nr.nextInt(10)+1)*50);
j++;}
return incomes;
}
static class Members extends MultilevelSystem1 {
double capital;
String name;
int rank;
String superior;
int ID;
int calculateRank (int r, Members[] membersArray) {
if(this.superior!=null)
for(int i=0;i<membersArray.length;i++)
if((this.superior).equals(membersArray[i].name)){r=membersArray[i].calculateRank(r,membersArray);
r++;
break;}
return r;
}
static int[] calculate (int[] a, int[] b) {
int[] c= new int [a.length];
for (int i=0; i<a.length; i++)
c[i]=a[i]+b[i];
return c;
}
}
public static void main(String[] args) {
final int n = 30;
final int m = 10;
int[] a={1,2,3,4,5};
int[] b={1,2,3,4,5};
int[] c= calculate(a,b);
for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
String[][] entryDataMembers = createMembers(n);
String[][] entryDataIncomes = createIncomes(entryDataMembers,m);
Members[] membersArray = new Members[n];
for (int i=0; i<n; i++) {
membersArray[i]=new Members();
membersArray[i].name = entryDataMembers[i][0];
if(i!=0) membersArray[i].superior= entryDataMembers[i][1];
else membersArray[0].superior= null;
}
for (int i=0; i<n; i++)
membersArray[i].rank = membersArray[i].calculateRank(1,membersArray);
}
}
And this work:
public class test {
static int[] calculate(int[] a, int[] b) {
int[] c= new int [a.length];
for (int i=0; i<a.length; i++)
c[i]=a[i]+b[i];
return c;
}
public static void main (String[]args) {
int[] a={1,2,3,4,5};
int[] b={1,2,3,4,5};
int[] c= calculate(a,b);
for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
}
}
int[] c = Members.calculate(a,b);
In you code there is compilation issue for calculate(a,b) method, this method present inside Members class. calculate(a,b) is static method so you can call this method using Members class.
The method which compiler couldn't find is "calculate".
Use
int[] c= Members.calculate(a,b);
to access static method of member class. (Around 62 line of code inside main method)

Issues with adding a random numbers to an array

I am trying to add random numbers to an empty array 20 numbers 0-99. When I run the code below it prints out 51 numbers and they are all 0.
Can someone please help me figure out what I am doing wrong here.
import java.util.Random;
public class SortedArray
{
int randomValues;
int[] value;
public SortedArray()
{
}
public int getRandom()
{
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues = random.nextInt(100);
}
return randomValues;
}
public int getArray()
{
int result = 0;
value = new int[randomValues];
for(int item : value)
{
System.out.println("The array contains " + item);
}
return result;
}
}
Here is my main method
public class ReturnSortedArray
{
public static void main(String[] args)
{
SortedArray newArray = new SortedArray();
int random = newArray.getRandom();
int array = newArray.getArray();
System.out.println(array);
}
}
In your method getArray
the code
value = new int[randomValues];
is simply creating a new empty int array of size ramdomValues.
As the default value of an int is 0, that is what you are getting
Also in your method getRandom you are setting the same value time and time again
for (...)
randomValues = random.nextInt(100);
try
public int[] getRandomArr()
{
int randomValues [] = new int [20];
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues[j] = random.nextInt(100);
}
return randomValues;
}
I see a few issues, you should probably set the values in your constructor. You could also call it a set method (since it's not actually a get). Also, your getArray() doesn't return an array. So, I think you really wanted something like this,
public class SortedArray {
private Random random = new Random();
private int[] value = new int[20];
public SortedArray() {
super();
setRandomValues();
}
public void setRandomValues() {
for (int j = 0; j < value.length; j++) {
value[j] = random.nextInt(100);
}
}
public int[] getArray() {
return value;
}
}
And then your main method, should be updated like
public static void main(String[] args) {
SortedArray newArray = new SortedArray();
int[] array = newArray.getArray();
System.out.println(Arrays.toString(array));
}

Categories