0

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());
        }
    }
}
1
  • podatki() returns String, so either change %s%d to %s%s, or change its return type to something numeric like int. Commented Jan 11, 2015 at 15:27

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Do you know how i would go about adding int v and int B into the izipisi method, so that it would print it like this: tipi[i](next line) v(next line) B(next line) Sorry im quite new to programming so im having trouble with this
You would need to pass in B into the izipsi method as an argument, because right now B only exists within the scope of the main method. And v only exists in the Prim class, but izipsi received the super class type Tip, which means that you need to cast: (Prim)tipi[i].v - which is not nice and fails when Tip[i] is not a Prim. Otherwise if you had a B and v in izipsi, then you can print System.out.format("%s\n%d\n%d\n", tipi[i], B, v);
and how do i go about getting v into the main method? i cant seem to get it i wrote a method that returns v and then called it in to main but it says it cannot find symbol
0

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());
                         ^

Comments

0

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());
    }
}

3 Comments

And btw, when asking for answers here, you should make sure you are following naming conventions, and not use cryptic languages to name your fields, methods and classes.
If I search for "vrsta" with Google I get a Bosnian result, and if I search for "podatki" I get a Polish result. Not sure where the OP is from but I'm guessing from an Eastern European country. Just because a word is not English, doesn't mean that it's cryptic.
It means kind (say ball or triangle) of an object and data on the object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.