Results 1 to 11 of 11
  1. #1
    Frankybb is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default ObjectInputStream (FileInputStream? kinda lost)

    Hello, I am trying to fill two textboxes with information contained in a .txt file like such

    "java","slang for coffee"
    "hangman","the name of the game"
    (on a line below would be another combination of word\hint)

    and separate the two words into separate text boxes and be able to navigate through each combination in order to choose one of them to start a new 'Hangman' game. The idea is that player 1 chooses the word Player 2 will have to guess, which comes with a hint, then Player 2 does the same. Idealy, i want to be able to wrap around when I reach the end of the list to go back to the start of it.

    I know I should use some kind of Stream but every Class and method I tried haven't given me any solid results. May anyone give me some guidelines please?

    Thank you.

  2. #2
    Frankybb is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    I read a little on the subject deeper in the forums and thought maybe storing each word in an array and each hint in another might make it work if they are stored in the same order. I understand the logic but, really the methods and classes to use elude me!

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Don't store things in parallel arrays or lists, make a new class:
    Java Code:
    public class Riddle {
      String word, hint;
      //constructors and methods go here
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  4. #4
    Frankybb is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    How do I separate the word from the hint though?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You can split the line using comma as the delimiter, but this will only work if your text has no commas within the quoted statements themselves. If they contain commas within, then you have to use fancier techniques such as use of a CSV (comma-separated values) reader/parser.

  6. #6
    Frankybb is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Splitting it means I have to store in an Array doesn't it?

  7. #7
    Frankybb is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    My problem is that I have to be able to scroll through all those combinations and I want the user to be able to choose the one they want. I also need them to be able to Wrap around to the beginning if the reach the end of the list.

  8. #8
    Frankybb is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Here's what I'm trying to accomplish... This code doesn't compile right i'm getting an Incompatible types on Line : Splits[compteur] = myArray[compteur].split(",");

    FileInputStream entry = new FileInputStream(Words1);
    BufferedReader br = new BufferedReader(new InputStreamReader(entry));

    String strLine;
    String[] myArray;
    myArray = new String[10];
    String Splits[] = new String[10];



    for (int counter = 0; counter < myArray.length; counterr++)
    {
    myArray[counter] = br.readLine();
    Splits[counter] = myArray[counter].split(",");
    }
    entry.close();

    I know it's probably messed up but I can't make it work even though I know it should be simple, I'm probably thinking too much... can anyone help? Thanks in advance!

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    This code doesn't compile right
    The compiler thinks that the type of the object on the left of the = is not compatible with what's on the right.
    Look at the types of both and see if they are. One is a String and the other is what is returned by the String.split() method.

  10. #10
    Frankybb is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    if my Array is declared as String[10], doesnt it make it also a String?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    No. An array of Strings in NOT the same as a String.
    An element of the array is a String.
    Splits[counter] = myArray[counter].split(",");
    On the left is a String on the right is an array of Strings

Similar Threads

  1. initiating objects question(kinda)
    By helpisontheway in forum New To Java
    Replies: 4
    Last Post: 01-11-2010, 03:07 AM
  2. ObjectInputStream does not initialize
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 06-03-2009, 08:11 AM
  3. Kinda stuck of learning Java
    By jurka in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 04:00 PM
  4. need help, weird question kinda.
    By carlos123 in forum New To Java
    Replies: 6
    Last Post: 01-22-2008, 03:19 AM
  5. k this is my ultimate project. kinda
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 04:47 AM

Posting Permissions

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