Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to compare against a null element in an array in java?
    text
    copied!<p>I have a program where I need to store the results in an arraylist:- </p> <pre><code>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&lt;String&gt; Keyword = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; Alternate = new ArrayList&lt;String&gt;(); // 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&gt;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&lt;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&lt;alternate.length;i++){ System.out.println("The value of the alternate is :"+alternate[i]); } ArrayList&lt;String&gt; AlternateNew = new ArrayList&lt;String&gt;(); for(int i=1;i&lt;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&lt;alternatenew.length;i++){ System.out.println("the value of the alternatenew :"+alternatenew[i]); } }catch (Exception e){ System.out.println("there is an error :"+e); } } } </code></pre> <p>The following is the csv file</p> <pre><code>Keyword,Alternate ego kit,baby doll ego kit,garage park ego kit,random beats galaxy tab,venus galaxy tab,earth galaxy tab,sun </code></pre> <p>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 i.e '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'</p> <p>The following is the result of the program :</p> <pre><code>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 </code></pre> <p>The last element is not getting stored in the arraylist.</p> <p>Do not understand why? New to Java programming.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload