The Artima Developer Community
Sponsored Link

Java Answers Forum
Newbie needs help desperately!

2 replies on 1 page. Most recent reply: May 28, 2003 5:49 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Tara Elisabeth

Posts: 1
Nickname: tara
Registered: May, 2003

Newbie needs help desperately! Posted: May 27, 2003 11:31 AM
Reply to this message Reply
Advertisement
Hi!

I've just started studying java, and I am doing a small program for school. I've searched everywhere and read dozens of tutorials, but I don't seem to find help anywhere. So any help is really appreciated.

I'd like to do a "Random name generator", which generates random name couples (first name + last name) from names in text files. Text files are called firstnames.txt and lastnames.txt, and there are one name per line in those files. I read about something called ScriptChooser, but since it doesn't belong to the basic JDK I have, I can't use it. But I'm looking for a way to do the same thing otherwise.

I really do value all the help I can get. So if you know anything about this, please help, any links to tutorials or codes or anything are more than welcome! :)

Tara


Michael

Posts: 4
Nickname: yaruki
Registered: May, 2003

Re: Newbie needs help desperately! Posted: May 28, 2003 3:28 AM
Reply to this message Reply
Hi Tara,

I recommend checking out the File I/O section of the Java Tutorial. ( http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html ) It will tell you what you need to know to get the data from the text files. The tutorial is a great resource in general for learning Java.

Load it up into the data structure of your choice (e.g. String arrays), and you're most of the way there.

http://developer.java.sun.com/developer/JDCTechTips/2001/tt0925.html#tip1 will help you generate random numbers to build your names.

Good luck!
Michael

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Newbie needs help desperately! Posted: May 28, 2003 5:49 AM
Reply to this message Reply
import java.io.*;
import java.util.*;
 
public class RandomNameGenerator{
 
    public RandomNameGenerator(){
        generateNames();
    }
    
    public static void main(String[] args){
        new RandomNameGenerator();
    }
    
    public void generateNames(){
    
        List firstNames = getNames("firstnames.txt");
        List lastNames = getNames("lastnames.txt");
        int number = 100;
        for (int i = 0; i < 100; i++){
            Collections.shuffle(firstNames);
            Collections.shuffle(lastNames);
            System.out.println(firstNames.get(0) + " " + lastNames.get(0));
        }
    
    }
    
    private List getNames(String fileName){
        List names = new ArrayList();
        try{
            File file = new File(fileName);
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String lineRead = "";
            while ((lineRead = bufferedReader.readLine()) != null){
                names.add(lineRead);
            }            
        }catch(FileNotFoundException fnfe){
            System.err.println("FileNotFoundException: " + fnfe.getMessage());
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return names;
    }
 
}

Flat View: This topic has 2 replies on 1 page
Topic: setVisibleRowCount() Previous Topic   Next Topic Topic: Java API structure

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use