Hi all,
i want to ask opinion,advice or mayb solution to my problem.

i want to add data from this object to an ArrayList<string>.this is my code:

class Main
{ 
  static ArrayList<String> test_case_list=new ArrayList<String> ();
  public static void main(String[] args)
  {
    //do argument....
   TestCaseGenerator(s,count,test_suite_list);
   display_list("Final Result",test_suite_list);
  }
  
  private final static void display_list(String title,ArrayList<string> list)
  {
    int i=0;
    System.out.println(title);
    for(Iterator it= list.iterator();it.hasNext();)
    String s=(String)it.next();
    System.out.println("i="+i+"->"+s);
   i++;
  }
   private final static void TestCaseGenerator(String binary_setting,int Count,ArrayList<String> test_suite_list)
  {
   System.out.println("Interaction Setting ->"+binary_setting);
   int[][] TestCase_Store = new int[count][value.length];
   String Test_Case=new String ("");
   int repetition =1;
   for(int i=value.length-1;i>=0;i--)
     {
       if(binary_setting.charAt(i)=='1')
	   {
	     int currentTestCase=0;
	     while(currentTestCase < count)
	  	  {
	  	 for(int j=0;j<value[i];j++)
	  	    {
	  	 for(int k=0;k<repetition;k++)
	  	      {
	  	  TestCase_Store[currentTestCase][i]=j;
	  	  currentTestCase++;
      		      }
	  	    }
	  	  }
	     repetition=repetition* value[i];
	    }
           else
	    {
 	     for(int j=0;j<count;j++)
		 {
		 Random generator = new Random();
		 TestCase_Store[j][i]=generator.nextInt(value[i]);
                 }
             }
        }
       for(int i=0;i<count;i++)
	  {
	     for(int j=0;j<value.length;j++)
	  	 {
		   if(j>0)
                      Test_Case=":"+TestCase_Store[i][j];
                   else
                     Test_Case=""+TestCase_Store[i][j];
                  System.out.print(Test_Case);
                  }
	  	  
	  	 System.out.println();
	  	
	    }

         // check if the generated test case is already
        // in the tupple_store

         if(!test_suite_list.contains(Test_Case))
            test_suite_list.add(Test_Case);
	  }

  }

this is an example:
input: 3,3,3,3
value=3, since the input all 3
value.length=4
binary_setting=
1110
1101
1011
0111
count=27..

the expected result
example: i=1->0:1:2:1

the reason i want to use arraylist,because since there r a lot of data list,u can see after system.out.print(Test_Case) and possibility to get duplicate data is there,so i want to compare the list data so that there is no duplicate data at last in ArrayList.
but i cant get what i expected.

i would appreciate any advice or mayb solution u can give me.or mayb any suggestion about reading material that i could have to get the solution!!

Thanks a lot

Edited 6 Years Ago by ubi_ct83: n/a

List<String> myList = new ArrayList<String>();
String myVar = "This is My String I want To Add";
myList.add(myVar)

i think this is what ive done.same thing

So what is your question? I don't get it?

i did what u post in my code,but still cant run like i want.

mhh! Would you re-state your question?
I don't know what you want to accomplish

my 2d array TestCase_Store is in int,i convert them to string using class string, String Test_Case,after that i want to put all the result in arraylist so that i can compare among the list.

TestCase_Store will be like this: example: 0021
in Test_Case,i want to add ':' between 0,2 and 1.so i choose string,should be like this:
i=0 Test_Case=0
i=1 Test_Case=:0
i=2 Test_Case=:2
i=3 Test_Case=:1

if i print the case in row is like this: 0:0:2:1

but there is a lot of row or list like that,so i dont want any duplicate list,so i put the list in arraylist so that i can compare,but unfortunately i failed.

what should i doto get such a result?is it possible to use arraylist, or i missplace the location, or any other mistake that ive done in my code?

putting in IDE i get some errors.
What is s in line below in main?

TestCaseGenerator(s,count,test_suite_list);

And what is definition of test_suite_list in code below?

TestCaseGenerator(s, count, test_suite_list);
        display_list("Final Result", test_suite_list);

Edited 6 Years Ago by Stefano Mtangoo: n/a

I'm completely lost in your code with errors and missing definition.
Post the whole code!

ok, this is my whole code

import java.io.*;
import java.util.*;


public class Main
 {
    static int t_value;
    String binary_setting;
    static int value[] ;
    static ArrayList<String> binary_cmd_list = new ArrayList<String>();
    static ArrayList<String> test_suite_list = new ArrayList<String>();


    public static void main (String[] args)
    {

     // reads the command line options
     for (int i=0;i<args.length;i++)
       {
         if (args[i].equals ("-i"))
           {
             String value_str = new String();
             // specify data values
             if (i+1<args.length)
              {
                i++;
                System.out.println ("Parameter =>"+args[i]);
                value_str = args[i];
              }
              // assign data values automatically to data by commas
              StringTokenizer s = new StringTokenizer (value_str,",");
              // count parameter
              int p = value_str.replaceAll("[^,]","").length();
              p++; // as an array size
              //System.out.println ("Parameter =>"+no_of_parameter);
              value = new int [p];
              int k =0;
              while (s.hasMoreTokens())
               {
				 value[k]=Integer.parseInt(s.nextToken());
                 k++;
               }
           }
         else if (args[i].equals ("-t"))
           {
			   String t_str=new String();
			   if (i+1<args.length)
			    {
				  i++;
			      t_str=args[i];
			      System.out.println("t="+i);
			      System.out.println("im here");
			    }
                  t_value=Integer.parseInt(t_str);

	       }

       }

     generate_binary_input_combinations (t_value,binary_cmd_list);
     generate_test_set (binary_cmd_list,test_suite_list);
     display_list ("Final Pairwise Test Suite List",test_suite_list);

  }

  //*****************************************************************
  //           DISPLAY ANY STRING ARRAY LIST
  //*****************************************************************
   private final static void display_list (String title, ArrayList<String> list)
    {
     int i=0;
     System.out.println (title);
     for (Iterator it = list.iterator(); it.hasNext(); )
     {
       String s = (String)it.next();  // Downcasting is required pre Java 5.
       System.out.println ("i = "+i + "->"+s);
       i++;
     }
    }


   //*****************************************************************
   //           GENERATE POSSIBLE BINARY INPUT COMBINATIONS
   //           BASED ON THE SELECTION OF T VALUE
   //*****************************************************************
   private final static void generate_binary_input_combinations (int t_value,
                                                                 ArrayList<String> binary_cmd_list)
    {

      int limit =(int) Math.pow (2,value.length);

      // generate binary number in comb
      for (int i=0;i<limit;i++)
       {
         String comb = new String();
         comb = Integer.toBinaryString (i);
         while (comb.length()<value.length)
           comb ="0"+comb;

         // count the occurences of 1 in comb
         int no_of_one=0;
         for (int j=0;j<comb.length();j++)
          if (comb.charAt(j)=='1')
               no_of_one++;

         if (no_of_one==t_value)
          {
             binary_cmd_list.add(comb);
          }

        }

    }

   //*****************************************************************
   //           GENERATE INTERACTION AND TWAY SET
   //*****************************************************************
   private final static void  generate_test_set (ArrayList<String> binary_cmd_list,
                                                    ArrayList<String> test_suite_list)
    {
        for (Iterator it = binary_cmd_list.iterator(); it.hasNext(); )
        {
          String s = (String)it.next();  // Downcasting is required pre Java 5.

          //count no of tupple base on binary_setting
		   int count=1;
		   for(int i=0;i<value.length;i++)

                if (s.charAt(i)=='1')
                   count = count*value[i]; //add no of value in int value

          TestCase_Generator (s,count,test_suite_list);
        }
    }


   //*****************************************************************
   //           TEST CASE GENERATOR
   //*****************************************************************

    private final static void TestCase_Generator (String binary_setting,int count,
                                                  ArrayList<String> test_suite_list)
    {
      System.out.println("Interaction Setting ->"+binary_setting);
      int[][] TestCase_Store = new int[count][value.length];
      String Test_Case=new String ("");
	  int repetition =1;
	  for(int i=value.length-1;i>=0;i--)
	  	{

	  		if(binary_setting.charAt(i)=='1')
	  		  {
	  			 int currentTestCase=0;
	  			 while(currentTestCase < count)
	  				  {
	  					  for(int j=0;j<value[i];j++)
	  						  {
	  							  for(int k=0;k<repetition;k++)
	  							     {
	  								    TestCase_Store[currentTestCase][i]=j;
	  								    currentTestCase++;
      							     }
	  						   }
	  				 }
	  					   repetition=repetition* value[i];
	  		  }
           else
		      {

				  for(int j=0;j<count;j++)
		             {
		                  Random generator = new Random();
		                  TestCase_Store[j][i]=generator.nextInt(value[i]);

					 }
              }


	      }



	  		for(int i=0;i<count;i++)
	  		 {
	  			for(int j=0;j<value.length;j++)
	  			   {
					   if(j>0)
                          Test_Case=":"+TestCase_Store[i][j];
                       else
                         Test_Case=""+TestCase_Store[i][j];

	  				   System.out.print(Test_Case);
                       //test_suite_list.add(Test_Case);
	  			   }
	  			 //test_suite_list.add(Test_Case);
	  			System.out.println();
	  			
                test_suite_list.add(Test_Case);
	  		 }

         // check if the generated test case is already
        // in the tupple_store


         //if(!test_suite_list.contains(Test_Case))
            //test_suite_list.add(Test_Case);
	  }

 }

Rethink your logic...
IF ever this condition would be true if (args[i].equals("-i")) you will get nothing because you never actually store anything in the array list.
So you get null pointer exception when you call generate_binary_input_combinations(t_value, binary_cmd_list);

A couple of suggestions:
1)Add a sample input to the code that allows for easy of testing:
Add this first thing in main() method

if(args.length == 0) {
        args = new String[] {"-i", "3", "-t", "3", "-1", "3","-t", "3"};
      }

2) add a final else clause to print out error message when unexpected args are found

}else {
          System.out.println("unknown arg: " + args[i]);
        }

Edited 6 Years Ago by NormR1: n/a

Rethink your logic...
IF ever this condition would be true if (args[i].equals("-i")) you will get nothing because you never actually store anything in the array list.
So you get null pointer exception when you call generate_binary_input_combinations(t_value, binary_cmd_list);

im sorry, i dont get what u mean.

i got the result,but the problem is i cant manipulate the result to be such my expected result

A couple of suggestions:
1)Add a sample input to the code that allows for easy of testing:
Add this first thing in main() method

if(args.length == 0) {
        args = new String[] {"-i", "3", "-t", "3", "-1", "3","-t", "3"};
      }

2) add a final else clause to print out error message when unexpected args are found

}else {
          System.out.println("unknown arg: " + args[i]);
        }

im so sorry...

please try this input:

-i 3,3,3,3 -t 3

hope this will clear u

im sorry, i dont get what u mean.

i got the result,but the problem is i cant manipulate the result to be such my expected result

Based on the previously provided "attempts" of input I did not see anywhere that you used "-i" as a parameter. Meaning that above mentioned condition never be true.

Secondly as I already said you never place anything into your ArrayList variables and passing around only initial object that will give you exceptions

im sorry, i dont get what u mean.

i got the result,but the problem is i cant manipulate the result to be such my expected result

It is strange that it runs!
Me it gives

Exception in thread "main" java.lang.NullPointerException
at MainClass.generate_binary_input_combinations(MainClass.java:89)
at MainClass.main(MainClass.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)

Process finished with exit code 1

what do you pass as arguments when running it?

It is strange that it runs!
Me it gives


what do you pass as arguments when running it?

Yes you will get these, because previously we haven't been provided with correct input type. Above mentioned does work. Dunno what is actually purpose of this messy application

It is strange that it runs!
Me it gives


what do you pass as arguments when running it?

ive just posted the example input.

u can put -i 3,3,3,3 -t 3

Yes you will get these, because previously we haven't been provided with correct input type. Above mentioned does work. Dunno what is actually purpose of this messy application

i think, its not important what is th epurpose of my code, because i know why i did it.

like what i am posted before, i want the result like this. 1 example: 0:2:1:0 and make sure this data only 1 in arraylist

would you write simple version of what you want to accomplish.
Here is my advice:
1. Make a separate class
2. Make Methods each to do one particular *single* task
3. Make another method (May be a constructor) to make full task utilizing the methods
3. Instantiate the class in main method of public class

In that way it will be easy to help.
currently your code is too complex and functions does too much works

Change your program to have it set default args for testing

if(args.length == 0) {
        args = new String[] {"-i", "3", "-t", "3", "-1", "3","-t", "3"};
      }

Add else at end of if and else if to show invalid input:

}else {
          System.out.println("unknown arg: " + args[i]);
        }
This article has been dead for over six months. Start a new discussion instead.