Using VectorEnumerator Class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • japuentem
    New Member
    • Jun 2007
    • 31

    Using VectorEnumerator Class

    I found this example on a web page

    Enumeration enum=v.elements ();
    while(enum.hasM oreElements())
    {
    String elemento=(Strin g)enum.nextElem ent();
    if(elemento.equ als("tres"))
    {
    System.out.prin tln("Encontrado tres");
    break;
    }
    }

    but when i paste into my code and execute it throws this exeption

    java.lang.Class CastException: java.util.Vecto r
    at Layout2.documen tLines(Layout2. java:282)
    at Layout2.obtiene Cadena(Layout2. java:200)
    at Layout2.generaD ocumento(Layout 2.java:78)
    at LanzaLayout.mai n(LanzaLayout.j ava:241)
    Exception in thread "main"

    Please help
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by japuentem
    I found this example on a web page

    Enumeration enum=v.elements ();
    while(enum.hasM oreElements())
    {
    String elemento=(Strin g)enum.nextElem ent();
    if(elemento.equ als("tres"))
    {
    System.out.prin tln("Encontrado tres");
    break;
    }
    }

    but when i paste into my code and execute it throws this exeption

    java.lang.Class CastException: java.util.Vecto r
    at Layout2.documen tLines(Layout2. java:282)
    at Layout2.obtiene Cadena(Layout2. java:200)
    at Layout2.generaD ocumento(Layout 2.java:78)
    at LanzaLayout.mai n(LanzaLayout.j ava:241)
    Exception in thread "main"

    Please help
    What type of elements do you have in the vector?

    Comment

    • japuentem
      New Member
      • Jun 2007
      • 31

      #3
      Originally posted by r035198x
      What type of elements do you have in the vector?
      I have vectors inside the main vector

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by japuentem
        I have vectors inside the main vector
        Well then doesn't

        [CODE=java] String elemento=(Strin g)enum.nextElem ent();[/CODE]
        look suspicious to you?

        Comment

        • japuentem
          New Member
          • Jun 2007
          • 31

          #5
          Originally posted by r035198x
          Well then doesn't

          [CODE=java] String elemento=(Strin g)enum.nextElem ent();[/CODE]
          look suspicious to you?
          i think it's trying to convert object to string, that is what i see, may be i'm wrong

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by japuentem
            i think it's trying to convert object to string, that is what i see, may be i'm wrong
            That's what it's trying to do. But you said the Objects in the Vector are actually Vectors. So a class cast exception will occur because you cannot cast a Vector to a String.

            Comment

            • japuentem
              New Member
              • Jun 2007
              • 31

              #7
              Originally posted by r035198x
              That's what it's trying to do. But you said the Objects in the Vector are actually Vectors. So a class cast exception will occur because you cannot cast a Vector to a String.
              So, i need to cast the object to vector then get each value of the inside vectors and that value cast to string?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by japuentem
                So, i need to cast the object to vector then get each value of the inside vectors and that value cast to string?
                Yes, depending on what you want the program to do of course.

                Comment

                • japuentem
                  New Member
                  • Jun 2007
                  • 31

                  #9
                  Originally posted by r035198x
                  Yes, depending on what you want the program to do of course.
                  Thanks for your help

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by japuentem
                    Thanks for your help
                    Aren't you using Java 1.5 or later? Those versions have generics and you don't
                    have to cast anything then. This is a vector of vectors of String e.g.

                    [code=java]
                    Vector<Vector<S tring>> vvs= new Vector<Vector<S tring>>();
                    ...
                    // get i,j-th element:
                    String element= vvs.get(i).get( j);
                    [/code]

                    kind regards,

                    Jos

                    Comment

                    • japuentem
                      New Member
                      • Jun 2007
                      • 31

                      #11
                      Originally posted by JosAH
                      Aren't you using Java 1.5 or later? Those versions have generics and you don't
                      have to cast anything then. This is a vector of vectors of String e.g.

                      [code=java]
                      Vector<Vector<S tring>> vvs= new Vector<Vector<S tring>>();
                      ...
                      // get i,j-th element:
                      String element= vvs.get(i).get( j);
                      [/code]

                      kind regards,

                      Jos
                      How can i know what version of java i'm using?

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by japuentem
                        How can i know what version of java i'm using?
                        Open a command prompt/shell and type
                        Code:
                        java -showversion

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by r035198x
                          Open a command prompt/shell and type
                          Code:
                          java -showversion
                          Make that:
                          Code:
                          java -version
                          kind regards,

                          Jos ;-)

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by JosAH
                            Make that:
                            Code:
                            java -version
                            kind regards,

                            Jos ;-)
                            That's definitely cleaner.
                            See why you should always be around?

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by r035198x
                              That's definitely cleaner.
                              See why you should always be around?
                              Is there a cleaner one for the javac as well? I'd think the JDK version might be more important to know than the JRE in (some cases).

                              Comment

                              Working...