-1

How can a vector pass to another class? I want to compare vector 1, which is in User.java and vector 2, in Case.java by using cosine similarity.

User.java

    JButton btnNewButton = new JButton("Process");
        btnNewButton.setBounds(360, 296, 89, 23);
        contentPane.add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        String a=Integer.toString(comboBox.getSelectedIndex()+1);
        String b=Integer.toString(comboBox_1.getSelectedIndex()+1);
        String day=Integer.toString(comboBox_2.getSelectedIndex()+1);                         
        Vector<String> myVector=new Vector <String> (3,2);
        myVector.add(a);
        myVector.add(b);
        myVector.add(day);
        myVector.add(valuesOfCheckBox);
        String holder;
        holder=myVector.toString();

        Case ca= new Case();
        try {
            ca.addPlace(holder);
            LoginGUI um= new LoginGUI();
            um.setVisible(true);

        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

Case.java

    public void addPlace( int[] h) { 

        int vec1[] =  h;
        int vec2[] = [2,1,3,2]; 

        double cos_sim = cosine_similarity(vec1,vec2); 
    }
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • 3
    I don't think `java.util.Vector` means what you think it means. It's just a collection of values - in your case, a collection of strings. Does it make sense to take the cosine similarity between strings? It's not a topic I'm familiar with, but it doesn't *sound* appropriate... – Jon Skeet Jul 29 '15 at 06:29
  • Make a compare method with parameter as vector in the class where you want your vector to be compared... Then make an object of the that class in the class from where you want to send the data for comparison... Now call the method using that object and pass the vector... – CoderNeji Jul 29 '15 at 06:29
  • @JonSkeet I have edited the post.It's a collection of int value – John Joe Jul 29 '15 at 06:40
  • 1
    @user5156075 No it is not. `addPlace` expects an array of int but in User.java you give it a `Vector` which is a collection of `String`s. Even if those Strings happen to be numbers, it will not make them an array of int automatically. – Fildor Jul 29 '15 at 06:42
  • @CoderNeji How? Any link that regarding on this topic you can share to me? Thanks – John Joe Jul 29 '15 at 06:42
  • By the way: this shouldn't even compile. Didn't you investigate on the messages the compiler gave you? – Fildor Jul 29 '15 at 06:46
  • @Fildor What should I change so that the vector in User.java can pass to Case.java? Sorry, i know it sounds a stupid question but i really don't know how to do – John Joe Jul 29 '15 at 06:46
  • These are the messages given by compiler java.lang.NullPointerException at api.Case.dot_product(Case.java:95) at api.Case.cosine_similarity(Case.java:78) at api.Case.addPlace(Case.java:74) – John Joe Jul 29 '15 at 06:49
  • Well, first of all. As is, you seem to convert Integers to Strings and add them to a String Vector. Don't do that. A better choice (from my point of view) would be an `ArrayList` to begin with. From here I am sure you will be able to find out how to come to an int[] from ArrayList. And what is that `holder` for? It makes no sense at all. – Fildor Jul 29 '15 at 06:49
  • But when I write like ArrayList myVector=new ArrayList(); I cannot use myVector.add(a); The holder is used to collect all the value like [1 1 1 1]. – John Joe Jul 29 '15 at 06:59
  • 1
    I suggest you read https://docs.oracle.com/javase/tutorial/collections/ to start with. And also, learn to differentiate between *exceptions* (thrown at run time) and *compiler errors* which you should see when the code compiles. NullPointerException isn't something the *compiler* would report. It doesn't help that you've just provided snippets - a short but complete program (no UI needed) demonstrating the problem would really help. – Jon Skeet Jul 29 '15 at 07:22

1 Answers1

0

I solved it by using this way:-

User.java

 ArrayList<Integer> myVector=new ArrayList<Integer>(); 
                        myVector.add(a);
                        myVector.add(b);
                        myVector.add(day);
                        if(chckbxLei.isSelected())
                        {
                            myVector.add(lei);
                        }
                        if(chckbxAdv.isSelected())
                        {
                            myVector.add(adv);
                        }

                        Case ca= new Case();
                        try {
                            ca.addPlace(myVector);
                            LoginGUI um= new LoginGUI();
                            um.setVisible(true);

                        } catch (Exception e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

Case.java

 public void addPlace( ArrayList<Integer> h) throws Exception { 
            int[] vec1 =  h.stream().mapToInt(t -> t).toArray();
            int vec2[] = [2,1,3,2];
            double cos_sim = cosine_similarity(vec1,vec2); 
    }
John Joe
  • 12,412
  • 16
  • 70
  • 135