Results 1 to 6 of 6
  1. #1
    redasu is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default help me think this through????

    My assignment is about read a regular expression and defining the gramar the first part of the assignment is done, the second part is about isolating all the "identifiers" and counting them if they repeat themselves, so I am using a two dimensional array so that I save a string in the first dimension and the number of appearance in the second. For some reason I am just not getting what I need and I am getting frustrated with this, here is my method:
    public static void output2(String name) throws IOException
    {

    String[][] identifier;
    identifier= new String[100][3];
    for(int j=0;j<identifier.length;j++)
    {
    identifier[j][1]=" ";
    identifier[j][2]="0";

    }



    FileReader fr = new FileReader(name + "_tok.txt");
    Scanner scan = new Scanner(fr);

    FileWriter fw2 = new FileWriter(name + "_sym.txt", true);
    BufferedWriter out2 = new BufferedWriter(fw2);


    while (scan.hasNextLine())
    {
    String line=scan.nextLine();
    int count=0;
    int length=0;

    StringTokenizer st = new StringTokenizer(line,"\t");// check white space symbol
    while(st.hasMoreTokens())
    {
    String token= st.nextToken();

    if(token.equals("identifier"))
    {
    String value= st.nextToken();
    identifier[count][1]=value;
    count++;
    System.out.println(value);
    }
    }




    for (int j=0; j<identifier.length;j++)
    {
    for(int k=j+1;k<identifier.length;k++)
    {

    if(!identifier[j][1].equals(identifier[k][1]) && identifier[j][2].equals("0"))
    {

    int number=1;
    identifier[j][2]=Integer.toString(number);
    System.out.println(identifier[j][2]+identifier[j][1]+"**");

    }
    else if((identifier[j][1].equals(identifier[k][1])) && !identifier[j][2].equals("0") && !identifier[j][1].equals(" "))
    {
    System.out.println(identifier[j][2]+identifier[j][1]+"gg");
    int number=(Integer.parseInt(identifier[j][2]))+1;
    identifier[j][2]=Integer.toString(number);

    System.out.println(identifier[j][2]+identifier[j][1]);

    }


    }

    }

    }



    for (int j=0; j<identifier.length;j++)
    {
    if(identifier[j][2].equals("0"))
    {
    identifier[j][1]="";
    identifier[j][2]="";

    }
    }
    for(int k=0;k<identifier.length;k++)
    {
    out2.write(identifier[k][1]+" "+identifier[k][2]);
    out2.newLine();
    out2.flush();
    }



    }

    To make the problem easier to understannd how do u go through a string array and count those elements and the number of time they appear, I know it might be easy but I am just not getting their.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    how do u go through a string array and count those elements and the number of time they appear

    Just addressing this ... (I find the code a little hard to follow since it not formatted or divided into small methods with clear or documented intent)

    A Map provides a nice data structure for counting. Not just String instances, but anything. Create a Map<String,Integer> where the key is the string and the value is the number of times it appears. To count a String instance you have identified you do the following:

    * If the string does not appear in the map's key set add a new map entry using the string as key and a value of 0.
    * Obtain the value associated with the string key (which must exist because of the first step)
    * Put an entry into the map with the string as key and a value obtained by incrementing the value obtained in the second step.

  3. #3
    redasu is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default Little bug

    I tried a hashmap(not very familiar with them) I am still having some errors(a expected .class error)

    public static void output(String name2) throws IOException
    {
    FileReader fr = new FileReader(name2 + "_tok.txt");
    Scanner scan = new Scanner(fr);

    FileWriter fw2 = new FileWriter(name2 + "_sym.txt", true);
    BufferedWriter out2 = new BufferedWriter(fw2);


    while (scan.hasNextLine())
    {
    String line=scan.nextLine();


    HashMap names = new HashMap();
    String name[] = null;

    name = line.split("\t");
    int count = 0;




    // convert array into hashmap (using
    // keys to remove duplicates)
    for (int x = 0; x < name.length ; x++) {
    if(names.put(name[], "")!= null)
    count++;
    }

    // print count
    System.out.println(names.size()+"\t");
    System.out.println(count+"\n");
    out2.write(names.size()+"\t"+count+"\n");
    out2.newLine();
    out2.flush();
    }

    }

    How should I fix the error and would this work???

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Please edit your code above and add code tags so that it is readable. Please see my signature immediately below my post for details.

    2) Please indicate by an obvious comment, which line is causing the error to occur.

    3) Please print the stack trace of the error rather than paraphrasing it.

  5. #5
    redasu is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default HashMAP NOOB

    When it comes to the previous error I actually fixed it but I am still not getting the desired output, I didn't know about the tags I am new to the forum thx for the link.
    Java Code:
    <script type: java>
    <
    public static void output(String name2) throws IOException
     {
          FileReader fr = new FileReader(name2 + "_sym2.txt");
                    Scanner scan = new Scanner(fr);
    
                    FileWriter fw2 = new FileWriter(name2 + "_sym.txt", true);
                    BufferedWriter out2 = new BufferedWriter(fw2);
    
    
                    while (scan.hasNextLine())
                     {
                         String line=scan.nextLine();
    
    
                             HashMap names = new HashMap();
    		String name[] = null;
    
    		name = line.split("\t");
    		int count = 0;
    
    
    
    
    		// convert array into hashmap (using
    		// keys to remove duplicates)
    	    for (int x = 0; x < name.length ; x++) {
    	    	if(names.put(name, "")!= null)
    	    			count++;
    	    }
    
    	    // print count
    	    
                out2.write(names+"\t"+count+"\n");
                out2.newLine();
                out2.flush();
    	}
    
     }
    //-->
    </script>

    //output
    <script type="text/javascript">
    <!--{[Ljava.lang.String;@61de33=} 0

    {[Ljava.lang.String;@14318bb=} 0

    {[Ljava.lang.String;@ca0b6=} 0

    {[Ljava.lang.String;@10b30a7=} 0


    //-->
    </script>

    //output desired
    <script type="text/javascript">
    <!--
    set1 2
    begin 1
    //-->
    </script>

    //input
    <script type="text/javascript">
    <!--
    set1
    set1
    begin
    //-->
    </script>

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your post is a bit fersmuttered.

    Anywho, you should use a generic hash map, for example HashMap<String, Integer>, and the count should be what is obtained via the get method and put into the hashmap's value field via the put method.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •