We have homework due tomorrow, and it's a three person group. I don't understand the slightest bit about arrays, but I've been trying - now the code just looks messy. Just with the things that say ------------------Tierra------------------, can you all help me? The book is useless and I'm just frustrated now, I don't know what to do.

//*************************************************************************
//Purpose:  Processing text files and ArrayLists
//
//Author:   Chloe Kimble
//Date:     10/30/2015
//Course:   CS1301
//
//*************************************************************************
import java.util.*;
import java.io.*;

public class MyTokens9 {

    ArrayList <String> allLines = new ArrayList<>();
       ArrayList <String> allTokens = new ArrayList<>();
       ArrayList <String> alphA = new ArrayList<>();
       ArrayList <String> others = new ArrayList<>();
       ArrayList <Integer> numeric = new ArrayList<>();
       String longestToken;
       int numberOfLines; //done
       int numberOfTokens; //done
      int numberOfCharacters;
       int numberOfLetters;
       int numberOfDigits;
       int numberOfAlphaTokens;
       int numberOfNumericValues;
       int numberOfOtherTokens;
       double averageOfNumericValue;

      //Reads all data from input file and stores each line in an
      //entry of the allLines ArrayList.
      //**********************Nicholas***************************
      public MyTokens9 (scan Scanner){
         numberOfLines = 0;
         while(scan.hasNext()){
            allLines.add(scan.nextLine());
            numberOfLines++;
         }   
         System.out.print(allLines);
      }

      //Scans each entry (a line of text –token-) of allLines ArrayList 
      //and stores alphabetic in alpha ArrayList, Integers in numeric ArrayList, 
      //and all others in others Arraylist. Computes number of lines, 
      //number of tokens, number of alphabetic tokens, numeric values, 
      //and others. Also, find the longest token in the file.
      //*********************Nicholas***************************
      public void processAllTokens(){
        String alphaPattern = "[A-Za-z]*";
        String numericPattern = "[0-9]*";

        String line;
        String token;
        for (int i =0; i < allLines.size(); i++){
            line = allLines.get(i);
            Scanner scanLine = new Scanner (line);
            scanLine.useDelimiter ("[ .,:;!?]+");
            while (scanLine.hasNext()){
                token = scanLine.next();
               allTokens.add(token);
               numberOfTokens++; //????????????????????????????????????????
                if (token.matches(alphaPattern))
                    alpha.add(token);
                else if (token.matches (numericPattern))
                    numeric.add(Integer.parseInt(token));
                else
                    others.add (token);
            }//end of while
        }//end of for
      }//end of method


      //Processes allTokens ArrayList and count numbers of characters, 
      //letters, digits, alpha tokens, and numeric values.
      //*********************Nicholas***************************
      //*************not done***********************************
      public void processCharacters(){
         String token;
         for(int i = 0; i < allTokens.size; i++){
           String alphaPattern = "[A-Za-z]*$";
           String numericPattern = "[0-9]*$";

               if (token.matches (alphaPattern)){
                  numberOfLetters ++;
               }
               else if (token.matches (numericPattern)){
                  numberOfDigits ++;
               }
               else if (token.

                  numberOfAlphaTokens ++;
            }//end of inside for
         }//end of outside for
      }//end of method

      //Displays the contents of a String ArrayList
      //-----------------Tierra--------------------
      public void displayStringArrayList(ArrayList<String> allLines, allTokens, alphA, others ){
       System.out.println ("\n\tString ArrayList");
       for (int i = 0; i < list.size(); i++){
         System.out.println (list.get(i));

      }

   }   

      //Displays the contents of an Integer ArrayList
      //-----------------Tierra--------------------
      public void displayIntegerArrayList(ArrayList<Integer> numeric){
      System.out.println ("\n\tInteger ArrayList");
      for (int i = 0; i < list.size(); i++){
         System.out.println (list.get(i));

      }

   } 

      //Computes the numeric average
      //-----------------Tierra--------------------
      public void average(ArrayList<Integer>){
      public double average (int [] numeric){
        return (double) sum (numeric) / list.length;
    }


      //Invokes displayStringArrayList to print AllLines, allTokens, 
      //alpha, and others ArrayLists.
      //Also, it invokes displayIntegerArrayList to print the numeric ArrayList.
      //-----------------Tierra--------------------
      public void displayAllLists(){
         public void displayStringArrayList(ArrayList<String> allLines, allTokens, alphA, others ){
        System.out.println ("\n\tString ArrayList");
         for (int i = 0; i < list.size(); i++){
         System.out.println (list.get(i));        
         }   
       } 
         public void displayIntegerArrayList(ArrayList<Integer> numeric){ 
         System.out.println ("\n\tInteger ArrayList");
            for (int i = 0; i < list.size(); i++){
         System.out.println (list.get(i));

            }
         }
      } 
     //Returns a string with the values of the instance variables
      //with the exception of the ArrayLists.
      public String toString(){


      }

}

Recommended Answers

All 4 Replies

Maybe the code isn't of the highest professional standard, but honestly it's not particularly messy either. Good enough for an early-stage learner. So what EXACTLY do you need help with?

To James, just the parts with "Tierra" above them. The average, displaying the integer and string arraylist, and especially: (the rest is for my team to finish up, so when they finish I can study their notes).

 //Invokes displayStringArrayList to print AllLines, allTokens, 
  //alpha, and others ArrayLists.
  //Also, it invokes displayIntegerArrayList to print the numeric ArrayList."

What do you not understand?

At least line 120 & 121 has method declaration at the same time. What do you really want to do here? If you need to average values in an ArrayList, then there must be a return type (not void). Besides, in your current code, you implement methods inside another method. You just need to call methods you want to run inside another method, not reimplement the whole code again.

Do you understand int[]? If you do, then it is easier to understand ArrayList.

Anyway, an array is a collection of data that have the same data type. Think of it as a group of people that line up in a single-person line (this is for 2 dimensional array). Each person knows what their position number is (as in array is an index). The first person in the line has position 0. The next person is 1, and so on. Thus, the size of the line (total number of people) is n and the last position number is n-1 (remember that the first person takes position 0?).

When you want to call out a person and you know the person's position number (index), it is easy to just get to the person by position number (index).

int[] and ArrayList<Integer> both are functioning in a very similar way -- collection of the same data type. An obvious difference is that int is a collection of primitive data type ONLY and ArrayList is a collection of objects ONLY. Another obvious difference is that int[] needs to know how many collection it can holds before initiate a variable and the size cannot be changed. On the other hand, ArrayList can be added/removed elements at any time, so the size is dynamic.

Now, the way to use and access both types are a bit different, but they still contains similar concept. To initiate a new variable of type int[] would be int[] arr1 = new int[5]; (create a new array size of 5. In ArrayList, primitive data type CANNOT be used, but luckily Java has a wrapper class which is Integer. So you can initiate as ArrayList<Integer> arr2 = new ArrayList<Integer>();. This initiation creats an empty ArrayList.

To assign a value of 5 to array at index 2, int[] would be arr1[2] = 5;; whereas in ArrayList, it would be arr2.add(new Integer(5));. Notice that in ArrayList, you need to convert int to Integer before you can add it to the array.

To access each array, both types require you to specify an index. In order to access value at index 3, for int[] would be arr1[3] and for ArrayList would be arr2.get(3). The accessed value will be the type of whatever you have specified (for arr1[3] is int and for arr2.get(3) will be Integer.

In your displayStringArrayList() method, you iterate through a given ArrayList (from method argument) and retrieve each object in the collection. The thing that might confuse you is System.out.println(). This command automatically calls the object's toString() method. As a result, it can be used to display what you want in String.

Overall, I believe you do not completely understand data type and object yet. As a result, you are disconnected with array. It is not easy to explain a short period of time, so I would suggest you to read more on "data type" and "object" in order to understand how to use/access variables you initiated in your program.

Furthermore, you do NOT understand methods and how they work. You need to read more on the topic as well...

PS: Java is NOT JavaScript. Please do NOT tag your post in the same category!

To Tay, I've been working on this code ever since then. The stuff is due tomorrow, and you just taught me more about this than my teacher has. This is really difficult for me, but I'm still working on it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.