简体   繁体   中英

How to compare against a null element in an array in java?

I have a program where I need to store the results in an arraylist:-

public class ReseedingDBRandomElements {

    public static void main(String[] args){

        try {

            // getting the field Keyword from the csv
            String csvfile="/Users/dray/Downloads/ReseedingDBRandomKeywords.csv";

            BufferedReader br =new BufferedReader(new FileReader(csvfile));
            StringTokenizer st = null;
            String line="";
            int linenumber=0;
            int columnnumber;

            // initializing the parameter for each column
            int free = 0;
            int free1 = 0;



            // create the ArrayList

            ArrayList<String> Keyword = new ArrayList<String>();
            ArrayList<String> Alternate = new ArrayList<String>();


            // reading through the csv file
            while((line=br.readLine())!=null){
                linenumber++;
                columnnumber = 0;

                st = new StringTokenizer(line,",");
                while(st.hasMoreTokens()){
                    columnnumber++;
                    String token = st.nextToken();
                    if("Keyword".equals(token)){
                        free=columnnumber;
                        System.out.println("The value of free :"+free);
                    }else if ("Alternate".equals(token)){
                        free1=columnnumber;
                        System.out.println("The value of free1 :"+free1);
                    }

                    if(linenumber>1){

                        if (columnnumber==free)
                        {
                            Keyword.add(token);
                        }else if (columnnumber==free1){
                            Alternate.add(token);
                        }
                    }
                }
            }

            // converting the keyword ArrayList to an array
            String[] keyword = Keyword.toArray(new String[Keyword.size()]);
            for(int i=0;i<keyword.length;i++){
                System.out.println(" The value of the keyword is :"+keyword[i]);
            }

            // converting the alternate ArrayList to an array
            String[] alternate = Alternate.toArray(new String[Alternate.size()]);
            for(int i=0;i<alternate.length;i++){
                System.out.println("The value of the alternate is :"+alternate[i]);
            }

            ArrayList<String> AlternateNew = new ArrayList<String>();


             for(int i=1;i<keyword.length;i++){
            if(keyword[i].equals(keyword[i-1])){
                AlternateNew.add(alternate[i-1]);
            }else if(!(keyword[i]==(keyword[i-1]))){
                AlternateNew.add(alternate[i]);
            }
        }


            String[] alternatenew = AlternateNew.toArray(new String[AlternateNew.size()]);
            System.out.println("The length of the array is :"+alternatenew.length);
            for(int i=0;i<alternatenew.length;i++){
                System.out.println("the value of the alternatenew :"+alternatenew[i]);
            }




        }catch (Exception e){
            System.out.println("there is an error :"+e);
        }

    }
}

The following is the csv file

Keyword,Alternate
ego kit,baby doll
ego kit,garage park
ego kit,random beats
galaxy tab,venus
galaxy tab,earth
galaxy tab,sun

What I am trying to do is compare elements and store it in an arraylist and display the results, but when last element is getting compared ie 'galaxy tab' is getting compared to an empty field after last 'galaxy tab', it is not storing the previous result in the arraylist which is 'sun'

The following is the result of the program :

The value of the alternate is :baby doll
The value of the alternate is :garage park
The value of the alternate is :random beats
The value of the alternate is :venus
The value of the alternate is :earth

The last element is not getting stored in the arraylist.

Do not understand why? New to Java programming.

This section has a few problems also present throughout

        AlternateNew.add(alternate[0]);
        for(int i=1;i<keyword.length;i++){
            if(keyword[i]==(keyword[i-1])){
                AlternateNew.add(alternate[i]);
            }else if(!(keyword[i]==(keyword[i-1]))){
                AlternateNew.add(alternate[i]);
            }
        }
  1. The naming convention in Java is to start with a lowercase letter for a variable name (unless it is a constant), which is why object AlternateNew is highlighted as if it were a class name.

  2. The else if block tests the opposite of the same condition as its if . You could comment out if(!(keyword[i]==(keyword[i-1])) , delete, or replace it with a more readable reminder comment, and the result would be the same.

  3. AlternateNew.add(alternate[i]); happens regardless of this condition, in either branch of the if , so either remove the if statement entirely or fix some typo.

As for your actual [edit: original] question, I can't find anything wrong. Are you sure you didn't forget to save the csv file? I ran it using a text file and got output contrary to your post!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM