Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: creating an array of strings?

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default creating an array of strings?

    Hi, my problem is, i know how to create an array of Strings, BUT, how can i import a file into the string array?
     TextInputFile InputCards = new TextInputFile ("sample_input.txt");
            TextOutputFile outputCards = new TextOutputFile ("Score_Output.txt");
     
            //Points
            int points, player1Pts = 0, player2Pts = 0, change;
            String card;
            char player;
            String [] deck = new String [52];

    SO, the string [] deck..i would like to import the (InputCards) into it
    any help?


  2. #2
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: creating an array of strings?

    i thought of
     while (InputCards.eof () != true)
            {
            card = InputCards.readString ();
            deck = deck[card];
     
            }
    but it keeps returning ( deck = deck[card]; ) this type of expression, "java.lang.String", is not numeric

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: creating an array of strings?

    You must keep track of the current index - you cannot access an array index using a String object
    In Pseudo-code
    int counter = 0;
    while ( ... ){
         card = InputCards.readString ();
        deck[counter] = card;
        counter++;
    }

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: creating an array of strings?

    Try using a scanner!

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: creating an array of strings?

    Quote Originally Posted by copeg View Post
    You must keep track of the current index - you cannot access an array index using a String object
    In Pseudo-code
    int counter = 0;
    while ( ... ){
         card = InputCards.readString ();
        deck[counter] = card;
        counter++;
    }
    so..what is the point of doing that?

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: creating an array of strings?

    Quote Originally Posted by Jason
    so..what is the point of doing that?
    You need a counter to keep track of where you are.

    counter++ increments itself by 1.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. NullPointerException when trying to read Strings into an array of myObject
    By sdivinyi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 24th, 2010, 09:45 AM
  2. Problems with File Reader (Strings and 2D Array Storage)
    By K0209 in forum File I/O & Other I/O Streams
    Replies: 44
    Last Post: January 12th, 2010, 10:48 AM
  3. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  4. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM