Results 1 to 5 of 5
  1. #1
    Ryanas is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Creating an array from data in a text file

    Solved. Need to keep my code hidden against being copied
    Last edited by Ryanas; 02-06-2011 at 11:41 PM. Reason: To prevent trouble.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    declare a string array for example

    String[] response = new String[10];

    and inside your while-loop where you call the hasNextLine() you can do

    response[i] = scan.nextLine();
    i++;

    please instantiate and int i outside the try/catch-block with 0.
    Last edited by j2me64; 02-06-2011 at 02:40 PM.

  3. #3
    Ryanas is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you so much, it's worked.

    I hate to be a pain but using that same file, when a user inputs a word which contains "network" for example in another class that is linked to the responder,

    So when the user enters a word that is in the data text file, print out a specific line in the file which relates to it.

    So if I entered network for example, it would look at the text file and return "Please make sure you have typed in the correct Network Security Key." because it has the word network in it?

    Thank you

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    the class String has a method called contains(CharSequence s) that returns true if the charsequence given as s is in the String the method is called, example

    Java Code:
    		String s = "My name is Bond, James Bond";
    		System.out.println(s.toUpperCase().contains("BOND"));

    note, that chained methods are evaluated from left to right, so the string s is set to uppercases so that the search is not case sensitif (if you want that). for your code loop through the arrays of string a make a call to contains() to look for your word.
    Last edited by j2me64; 02-06-2011 at 03:31 PM.

  5. #5
    Ryanas is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Mmmm, but wouldn't that require me to write out all the different sentences?

    I want to simply scan a text file for a specific word that the user has input and then system.out the sentence that contains the word.

    while(!finished) {
    String input = reader.getInput();

    if (input.contains("stupid")) {
    swearing = true;

    System.out.println("I'm sorry, but that is an offence word. Please improve your language.");
    }

    else if(input.startsWith("bye")) {
    finished = true;
    printGoodbye();

    }

    else {
    String response = responder.generateTextResponse();
    System.out.println(response);

    I use this class to generate the response, so if the user inputs a certain word, how can it scan the text file and system.out the sentence containing said word.

    Sorry, I'm very Java illiterate lol

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 06:31 AM
  2. creating a graph from a text file
    By chinolee in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 09:36 AM
  3. Creating&Saving as a different Text file
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 08:17 AM
  4. Replies: 11
    Last Post: 04-01-2009, 04:43 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
  •