2 Replies - 118 Views - Last Post: 04 February 2012 - 09:50 PM Rate Topic: -----

Topic Sponsor:

#1 agidreos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 6
  • Joined: 12-December 11

Arraylist problem

Posted 04 February 2012 - 09:32 PM

I'm not asking you to write the code i just want at least a few hints for the below aspects("print out the ..."

myprogram needs to ask the user for a file name. The file will contain a name on each line. Put each of the names into an ArrayList.

After you have put all of the names into the ArrayList search through the list and print out the following information;

Print out the longest name
Print out the shortest name
print out the total number of characters in all of the names.
print out the average number of characters per name
print out the whole list after the previous four pieces of information.

i have this so far. help with at least one aspect?
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Scanner;

public class project1 {

    public static void main(String[] args) throws IOException
          {

          String filename = "";
          String longestString ="";
          int i;

          ArrayList<String> filestrings = new ArrayList<String>();

          Scanner keyboard = new Scanner(System.in);

          System.out.print("Enter the name of the file you would like to search:");
          filename = keyboard.nextLine();


          File file = new File(filename);

          if (!file.exists())
          {
          System.out.println("The file " +filename+ " is not found.");
          System.exit(0);
          }


          else
          {
              System.out.println(file);
          }

          File file1 = new File(filename);
          Scanner inputFile = new Scanner(file);

          while (inputFile.hasNext())
          {

          String line = inputFile.nextLine();
          filestrings.add(line);
          }
          System.out.println("There are " + filestrings.size() + " words in this document.");




       }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Arraylist problem

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Arraylist problem

Posted 04 February 2012 - 09:38 PM

Quote

Print out the longest name
Print out the shortest name

This is a simple find-max or find-min situation. Think about using a for loop and comparing the String lengths, accessible for the length() method.

Quote

print out the total number of characters in all of the names.
print out the average number of characters per name

Again, just add up the String lengths. The average is easy.

Quote

print out the whole list after the previous four pieces of information.

Quite literally pass the ArrayList to a System.out.println() call.
Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Arraylist problem

Posted 04 February 2012 - 09:50 PM

Ok, looking at your code first I see the following:
-First no need to have two files of the same:
 File file = new File(filename);
File file1 = new File(filename);

What is the reason?

- then since you are dealing with next line, check for the next line:
while (inputFile.hasNextLine())


Now after finishing the reading, what you will have to do is just looping through the list(which can also be done using the same loop before adding to the list) and compare things.
//you will have a variable to count characters initialized to 0
 //make the first name in the list to be the shortest and longest
 //add its size to the counter(number of characters)
 //enter loop starting from the second item
 //add its size to the counter
 //compare if it is longer than longest, make it longer
 //else compare if it is shortest that shorter, make it shortest
 //end loop
 //counter / size of the list to get average
// print list

EDIT: oh! ninjaed

This post has been edited by smohd: 04 February 2012 - 09:53 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1