Results 1 to 4 of 4

Thread: Some java help...a little stuck

  1. #1
    Senior Member
    Join Date
    Oct 2001
    Posts
    186

    Some java help...a little stuck

    I was wondering if someone here could try and help me with something. I have a programming project that requires me to input a text file, determine what kind of record it is ( such as add or ship ) then store the data types in an object. I have a couple problems that i run into.
    If the line starts with A 7 different data types need to be constructed in an add class
    If the line starts with S 4 integers need to be sent to the Ship class constructor

    a typical line for add in the input file would be as follows: A 9999 C 2.3 2 0 1 3
    and for ship : S 1 0 2 4
    in the main i initiate a loop so i can read in using bufferedreadline

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

    BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    String lastline = "";
    char addA = "a";
    boolean done = false;

    while(!done)
    {
    String input = console.readline();
    char ch = Character.toLowerCase(s.CharAt(0));
    //checks first letter

    if ( input == null)
    done = true;

    //i am not sure of the syntax to fine out what the record is
    else if ( input.indexOf(ch) == addA )

    //also i dont understand how to break up the full string into parts using substring
    //i assume i should pass all as strings and parse them in a method of add
    Add Adding = new Add( int , char , double , int , int , int , int);

    else
    Ship Shipping = new Ship(int , int , int , int );
    //ends snip of code

    what i dont understand isexactly how to compare the first charactar of the string and how to break it up into multiple strings using a space a the divider.

    The examples in my book are not clear on this and have maybe devoted a half of a page to the subject. I've done a couple of searches and have come up empty. Any help would be appreciated.
    Ben Franklin said it best. \"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.\"

  2. #2
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    You need to look into this website - http://java.sun.com/j2se/1.4.2/docs/api/

    Look up the java.lang package (top left corner) and then in bottom left corner look up the String class.

    You will most likely want to look into the charat() and split() methods.

    I highly recommend that you get acquainted with that website. The documentation there is invaluable.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  3. #3
    Since you said this is a project, I assume you mean for school, so I won't come right out and tell you the answer but I do have a couple of suggestions.

    For starters, if you're going to be using a text file as your input, than you are going to have to construct your BufferedReader differently. Right now it is using the Standard in for its input. A BufferedReader takes a Reader Object as its parameter for its basic constructor. Maybe A FileReader Object would be better suited.

    The major problem with the program so far seems to be the way that you are trying to determine the first character from the incoming text. As Juridian suggests, you should carefully read the charAt() function in the String class documentation. Is it really neccessary to convert the first character to lower case?

    As far as parsing the actual input String itself. I think you will find the StringTokenizer class most helpful, it's one of my favorites. StringTokenizer is a member of java.util. Good luck!!

  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    186
    Thanks for the help everyone. Your suggestions gave me the push in the right dirrection and i see how this should come together now.
    Ben Franklin said it best. \"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.\"

Posting Permissions

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