Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
3.22/5 (2 votes)
See more:
Iam currently working on a mapper which takes input from a text file test.txt and matching with the content in the file named goodfile and badfile respectively. Below is my program
Java
import java.util.*;
import java.io.*;
public class getdata {
	  public static void main(String []args){
		  try{
		
			  ArrayList a=new ArrayList();
			  File testfile = new File("D:\\test.txt");
			  Scanner test=new Scanner(testfile);
			  int notcount=0;
			  int goodcount=0;
			  int badcount=0;
		  File file1 = new File("D:\\good.txt");
		  Scanner goodFile = new Scanner (file1);
		 File file2 = new File("D:\\Bad.txt");
		  Scanner badFile=new Scanner(file2);
		  test.useDelimiter(" ");
		  while(test.hasNext()){
			  String text=test.next();
			  if(text.equals("not")){
				  notcount++;
			  }
			  a.add(text);
		  }
		  badFile.useDelimiter(",");
          while(badFile.hasNext())
          {
        	  String word=badFile.next();
        	  for(int i=0;i<a.size();i++){
             
              if(word==a.get(i))
              {
            	  badcount++;
              }
          }
        	 }
		  
		  
          
          goodFile.useDelimiter(",");
          while(goodFile.hasNext())
          {
        	  String word=goodFile.next();
        	  for(int i=0;i<a.size();i++){
             
              if(word.equals(a.get(i)))
              {
            	  goodcount++;
              }
          }
        	 }
          if(notcount==1){
        	  if(goodcount!=0){
        		  goodcount=goodcount-1;
        		  badcount=badcount+1;
        	  }
        	  else
        		  if(badcount!=0){
        			  badcount=badcount-1;
        			  goodcount=goodcount+1;
        		  }
          }
          
          
          
          System.out.println("negative remarks"+badcount);
          System.out.println("positive remarks"+goodcount);
	  }
		  catch(IOException e){
			  System.out.println("File not found");
			  }
		  }
	  }		 

My question is in the above program badcount is always multiplied by 4 whereas goodcount is proper. Why is that so?
Posted
Comments
Sergey Alexandrovich Kryukov 5-Dec-13 0:57am    
Use the debugger and check up the code step by step. Did you try it? What have you tried so far?
—SA
Rakshith Kumar 5-Dec-13 1:28am    
I did try that. I found that my value is replicated four times. I just wanted to figure out why. I thought that it may be because == places the reference of a value and .equals places the exact value. Just wanted to confirm it

1 solution

Without knowing contents of test.txt, good.txt, and Bad.txt, it is very difficult to follow step by step and locate where the problem is; however, comparing the code you have for goodcount and badcount, I notice your string comparison of word and a.get(i) differs with == and equals. I bet that your problem will be resolved if you change == operator to equals() call. Equals() doesn't work entirely the same as == operator.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900