Jump to content

Need help with Java code

Epimetheus

Cattura.thumb.PNG.d306947362cc6e877c6f4463790e2153.PNG

Hey guys, i know the code is probably ass, but i just want to get it working. What's the problem with this code? I commented the lines 177-179, but it gave me the same error

Link to comment
Share on other sites

Link to post
Share on other sites

Post your code here In [ code ] tags it will make helping fair easier

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, vorticalbox said:

Post your code here In [ code ] tags it will make helping fair easier

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package esercizio2;

import java.util.*;

/**
 *
 * @author studente
 */
public class Esercizio2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        CartaDaGioco[] carte = new CartaDaGioco[53];
        int i;
        Random random=new Random();
        for(i=1;i<=52;i++){
            if(i<=13){
                if(i<=10){
                    carte[i]=new CartaDaGioco("cuori",Integer.toString(i));
                }
                else{
                    if(i==11){
                        carte[i]=new CartaDaGioco("cuori","J");
                    }
                    if(i==12){
                        carte[i]=new CartaDaGioco("cuori","Q");
                    }
                    if(i==13){
                        carte[i]=new CartaDaGioco("cuori","K");
                    }
                }
            }
            else if(i>13 && i<=26){
                if(i<=23){
                    carte[i]=new CartaDaGioco("quadri",Integer.toString(i-13));
                }
                else{
                    if(i-13==11){
                        carte[i]=new CartaDaGioco("quadri","J");
                    }
                    if(i-13==12){
                        carte[i]=new CartaDaGioco("quadri","Q");
                    }
                    if(i-13==13){
                        carte[i]=new CartaDaGioco("quadri","K");
                    }
                }
            }
            else if(i>26 && i<=39){
                if(i<=36){
                    carte[i]=new CartaDaGioco("fiori",Integer.toString(i-26));
                }
                else{
                    if(i-26==11){
                        carte[i]=new CartaDaGioco("fiori","J");
                    }
                    if(i-26==12){
                        carte[i]=new CartaDaGioco("fiori","Q");
                    }
                    if(i-26==13){
                        carte[i]=new CartaDaGioco("fiori","K");
                    }
                }
            }
            else if(i>39 && i<=52){
                if(i<=49){
                    carte[i]=new CartaDaGioco("picche",Integer.toString(i-39));
                }
                else{
                    if(i-39==11){
                        carte[i]=new CartaDaGioco("picche","J");
                    }
                    if(i-39==12){
                        carte[i]=new CartaDaGioco("picche","Q");
                    }
                    if(i-39==13){
                        carte[i]=new CartaDaGioco("picche","K");
                    }
                }
            }
            
        }
        System.out.println("Inserisci le carte che vuoi confrontare");
        String seme,valore;
        int pos1=1,pos2=1;
        Scanner tastiera=new Scanner(System.in);
        seme=tastiera.nextLine();
        valore=tastiera.nextLine();
        for(i=1;i<=52;i++){
            if(carte[i].getSeme().equals(seme) && carte[i].getValore().equals(valore)){
                pos1=i;
            }
        }
        System.out.println("Inserisci la seconda carta");
        seme=tastiera.nextLine();
        valore=tastiera.nextLine();
        for(i=1;i<=52;i++){
            if(carte[i].getSeme().equals(seme) && carte[i].getValore().equals(valore)){
                pos2=i;
            }
        }
        int ris;
        ris=carte[pos1].confronta(pos1,pos2);
        switch (ris) {
            case -1:
                System.out.println("La carta iniziale viene dopo");
                break;
            case 0:
                System.out.println("Sono la stessa carta");
                break;
            case 1:
                System.out.println("La carta iniziale viene prima");
                break;
            default: 
                System.out.println("C'è stato un errore");
                break;
        }
        Boolean ris2;
        ris2=controllaScala(carte);
        if(ris2==true){
            System.out.println("Le 5 carte estratte sono in scala");
        }
        else{
            System.out.println("Le 5 carte estratte non sono in scala");
        }
        
    }
    public static Boolean controllaColore(CartaDaGioco v[]){
        
        Random random= new Random();
        int posz[]=new int[5],j=0;
        int nestratti=0,sel;
        Boolean ris=false;
        for(int i=0;i<=52;i++){
            sel=random.nextInt(1);
            if(nestratti<5){
                if(sel==1){
                    posz[j]=i;
                    j++;
                }
            }
        }
        if(v[posz[0]].getSeme().equals(v[posz[1]].getSeme()) && v[posz[1]].getSeme().equals(v[posz[2]].getSeme()) && v[posz[2]].getSeme().equals(v[posz[3]].getSeme()) && v[posz[3]].getSeme().equals(v[posz[4]].getSeme()) && v[posz[4]].getSeme().equals(v[posz[5]].getSeme())){
            ris=true;
        }
        return ris;
    }
    public static Boolean controllaScala(CartaDaGioco v[]){
        
        Random random= new Random();
        CartaDaGioco carte[]= new CartaDaGioco[5];
        CartaDaGioco tmp;
        int posz[]=new int[5],j=0;
        int nestratti=0,sel;
        Boolean ris=false;
        for(int i=0;i<=52;i++){
            sel=random.nextInt(1);
            if(nestratti<5){
                if(sel==1){
                    carte[j]=new CartaDaGioco(v[i].getSeme(),v[i].getValore());
                    j++;
                    nestratti++;
                }
            }
        }
        Boolean nere=false,rosse=false;
        System.out.println(carte[0]);
        for(int i=0;i<5;i++){
            System.out.println("Questo è i"+i);
            /*if(carte[i].getSeme().equals("cuori") || carte[i].getSeme().equals("quadri")){
                rosse=true;
            }*/
            if(carte[i].getSeme().equals("fiori") || carte[i].getSeme().equals("picche")){
                nere=true;
            }
        }
        int val,valori[]=new int[5];
        if(rosse==true && nere==true){
            ris=false;
        }
        else{
            for(int i=0;i<5;i++){
                if(carte[i].getValore().equals("J")){
                    val=11;
                }
                else if(carte[i].getValore().equals("Q")){
                    val=12;
                }
                else if(carte[i].getValore().equals("K")){
                    val=13;
                }
                else{
                    val=Integer.parseInt(carte[i].getValore());
                }
                valori[i]=val;
            }
            ris=true;
            for(int i=1;i<5;i++){
                if(valori[i]<valori[i-1]){
                    ris=false;
                }
            }
            
        }
        
        return ris;
    }
}

@vorticalbox here it is

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package esercizio2;

/**
 *
 * @author studente
 */
public class CartaDaGioco {
    private String seme;
    private String valore;
    
    CartaDaGioco(String a,String b){
        seme=a;
        valore=b;
    }
    
    public void setSeme(String a){
        seme=a;
    }
    public void setValore(String a){
        valore=a;
    }
    public String getSeme(){
        return seme;
    }
    public String getValore(){
        return valore;
    }
    public String toString(){
        return valore+" di "+seme;
    }
    public int confronta(int pos1, int pos2){
        int ris;
        if(pos1>pos2){
            ris=-1;
        }
        else if(pos1==pos2){
            ris=0;
        }
        else{
            ris=1;
        }
        return ris;
    }
}

This is the class CartaDaGioco

Link to comment
Share on other sites

Link to post
Share on other sites

You've got a NullPointerException in this line:

if(carte[i].getSeme().equals("fiori") || carte[i].getSeme().equals("picche")) {

This means either "carte[ i ]" returns a null value or "getSeme()" returns a null values. Ideally I would rewrite this as:

String seme = carte[i].getSeme();
if(seme.equals("fiori") || seme.equals("picche")) {

This way you get a better idea which one is null. The first version is known as the "train wreck pattern" for a reason. To be null-safe you should use:

Objects.equals(seme, "fiori");

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

28 minutes ago, Eigenvektor said:

You've got a NullPointerException in this line:


if(carte[i].getSeme().equals("fiori") || carte[i].getSeme().equals("picche")) {

This means either "carte[ i ]" returns a null value or "getSeme()" returns a null values. Ideally I would rewrite this as:


String seme = carte[i].getSeme();
if(seme.equals("fiori") || seme.equals("picche")) {

This way you get a better idea which one is null. The first version is known as the "train wreck pattern" for a reason. To be null-safe you should use:


Objects.equals(seme, "fiori");

 

i've done further testing, and it looks like the object in position 0 is null, while the others aren't. Can't pinpoint the reason though

Link to comment
Share on other sites

Link to post
Share on other sites

ok, apparently i've fixed it? I just started the i at value 1 when i'm randomly generating numbers (since the original array, that in the function is called v[], occupies positions 1-53. That's due to how I generate the deck of cards). I though I had already tested doing that but it didn't work, but apparently it does now? Thank you anyways.

Quite frankly, (leaving out the function controllaColore that is pure garbage and then i've remade it from scratch in the other function much better), how is my code? Is it trash or am I doing things the right way?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Epimetheus said:

i've done further testing, and it looks like the object in position 0 is null, while the others aren't. Can't pinpoint the reason though

I've seen at least two for-loops that start at "1" instead of "0" (like they should)

for(i = 1; i <= 52; i++) {

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Epimetheus said:

ok, apparently i've fixed it? I just started the i at value 1 when i'm randomly generating numbers (since the original array, that in the function is called v[], occupies positions 1-53. That's due to how I generate the deck of cards). I though I had already tested doing that but it didn't work, but apparently it does now? Thank you anyways.

Quite frankly, (leaving out the function controllaColore that is pure garbage and then i've remade it from scratch in the other function much better), how is my code?

If you create an array like "new SomeClass[53]" the correct indexes are 0 - 52 not 1 - 53. Otherwise you'll run into an IndexOutOfBounds exception.

Quote

Is it trash or am I doing things the right way?

Edit: Well, you've got a lot of nested for-loops and if-conditions, which is considered bad style. I would try to make that more readable, personally.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Eigenvektor said:

If you create an array like "new SomeClass[53]" the correct indexes are 0 - 52 not 1 - 53. Otherwise you'll run into an IndexOutOfBounds exception.

Yeah, i know that - however, due to how I created the deck, i had to leave out the 0 position. If you want you can check the algorithm I used for it. I suppose I could have made a for cicle to move all the positions back by one once I finished creating it, but didn't feel like it

Link to comment
Share on other sites

Link to post
Share on other sites

28 minutes ago, Epimetheus said:

Yeah, i know that - however, due to how I created the deck, i had to leave out the 0 position. If you want you can check the algorithm I used for it. I suppose I could have made a for cicle to move all the positions back by one once I finished creating it, but didn't feel like it

Here's an idea how I'd initialize the instances, for example:

	private static String getSeme(final int index) {
		if (index < 13) {
			return "cuori";
		} else if (index < 26) {
			return "quadri";
		} else if (index < 39) {
			return "fiori";
		} else {
			return "picche";
		}
	}

	private static String getValore(final int index) {
		switch (index % 13) {
			case 13:
				return "K";
			case 12:
				return "Q";
			case 11:
				return "J";
			default:
				return String.valueOf(index);
		}
	}

	public static void main(final String[] args) {
		final CartaDaGioco[] carte = new CartaDaGioco[53];
		for (int i = 1; i <= 52; i++) {
			final String seme = getSeme(i);
			final String valore = getValore(i);
			carte[i] = new CartaDaGioco(seme, valore);
		}

This makes the whole block a lot shorter and easier to read. I would also look into potentially replacing Seme and Valore with Enums.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

50 minutes ago, Eigenvektor said:

Here's an idea how I'd initialize the instances, for example:


	private static String getSeme(final int index) {
		if (index < 13) {
			return "cuori";
		} else if (index < 26) {
			return "quadri";
		} else if (index < 39) {
			return "fiori";
		} else {
			return "picche";
		}
	}

	private static String getValore(final int index) {
		switch (index % 13) {
			case 13:
				return "K";
			case 12:
				return "Q";
			case 11:
				return "J";
			default:
				return String.valueOf(index);
		}
	}

	public static void main(final String[] args) {
		final CartaDaGioco[] carte = new CartaDaGioco[53];
		for (int i = 1; i <= 52; i++) {
			final String seme = getSeme(i);
			final String valore = getValore(i);
			carte[i] = new CartaDaGioco(seme, valore);
		}

This makes the whole block a lot shorter and easier to read. I would also look into potentially replacing Seme and Valore with Enums.

yeah that's definitely easier lol, didn't think about that. What does "final" stand for?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Epimetheus said:

yeah that's definitely easier lol, didn't think about that. What does "final" stand for?

final in this context means a variable can't be modified once initialized. https://softwareengineering.stackexchange.com/a/98703

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×