LukaTheLegend
LukaTheLegend

Reputation: 89

Java, need help printing a string out of an array

When i try to print out the ''Prim object'' in the first switch as case 1 i get a print error looking like this:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
            at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
            at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
            at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
            at java.util.Formatter.format(Formatter.java:2520)
            at java.util.Formatter.format(Formatter.java:2455)
            at java.lang.String.format(String.java:2927)
            at dn09$Tip.toString(dn09.java:82)
            at java.lang.String.valueOf(String.java:2981)
            at java.io.PrintStream.println(PrintStream.java:821)
            at dn09.izpisi(dn09.java:32)
            at dn09.main(dn09.java:19)

I cannot for the hell of me figure out what is wrong, what im trying to do is print the Tip in the i slot of the tipi array as a string. May the way i read and create ''Prim'' be wrong? or are any of the methods used to print wrong? it wont print if i use the toString method from class Tip either

This is the entire program i have now:

    import java.util.*;
    public class dn09 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int B = sc.nextInt();
        Tip[] tipi = preberi(sc);
        int u = sc.nextInt();
        int[] ukazia = new int[u];
        int[] ukazid = new int[u];
        for (int i = 0; i < u; i++) {
            ukazia[i] = sc.nextInt();
            ukazid[i] = sc.nextInt();
        }
        for (int i = 0; i < u; i++) {
            switch(ukazia[i]) {
                case 1:
                    int vrstica = ukazid[i];
                    izpisi(vrstica-1, tipi);
                case 2:

                case 3:

            }

        }


    }

    private static void izpisi(int i, Tip[] tipi) {
        System.out.println(tipi[i]);
    }


    private static Tip[] preberi(Scanner sc) {
        int d = sc.nextInt();
        Tip[] tipi = new Tip[d];
        for (int i = 0; i < d; i++) {
            String tipPodatka = sc.next();
                switch (tipPodatka) {
                    case "prim":
                        tipi[i] = new Prim(sc.nextInt());
                        break;
                    case "arr":

                        break;
                    case "ostruct":
                        break;
                    case "pstruct":
                        break;
                }
        }
        return tipi;
    }




    private static class Prim extends Tip {
        protected int v;

        public Prim (int v) {
            this.v = v;
        }

        public String vrsta() {
            return "prim";
        }

        public String podatki() {
            return String.format("v = %d", this.v);
        }


    }

    private static abstract class Tip extends dn09 {
        public abstract String podatki();
        public abstract String vrsta();
        public String toString() {
            return String.format("%s%d", this.vrsta(), this.podatki());
        }
    }
}

Upvotes: 0

Views: 694

Answers (3)

Clashsoft
Clashsoft

Reputation: 11882

When using String.format, %d is used for integer numbers. To insert a string, you have to use %s.

private static abstract class Tip
{
    public abstract String podatki();
    public abstract String vrsta();

    public String toString()
    {
        return String.format("%s%s", this.vrsta(), this.podatki());
    }
}

Upvotes: 0

Todd
Todd

Reputation: 31690

return String.format("%s%d", this.vrsta(), this.podatki());

If I'm reading it right, podatki is a String, not an integer. So you'll need to change that format a bit:

return String.format("%s%s", this.vrsta(), this.podatki());
                         ^

Upvotes: 0

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31269

Change your code to:

return String.format("%s%s", this.vrsta(), this.podatki());

(two times %s, not %s and then %d)

%d means that you want to print a number, but this.podatki() returns a String, which is not compatible with %d.

Upvotes: 2

Related Questions