Results 1 to 15 of 15
  1. #1
    FnL7676 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Quick simple question.

    Im new to java and in one of my projects it says:
    read the next full line into the variable line

    Im not sure how to word this in java, I can figure what the variable is based on what I got. ( I know I need to say what it is in this line)
    Any help appreciated.. just use ~variable~ where it would go :)

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm not going to give you the exact code, rather the description of what should be done.

    First, declare a string to hold what you want to read. The next thing you need is some sort of io chain. Check out the tutorials on io. If you are not trying to read the line from a file please clarify your question for me.

  3. #3
    FnL7676 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    The code before where I am stuck, is:

    package csc212project02a;

    import static java.lang.System.out;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Scanner;
    /**
    *
    * @author J
    */
    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws IOException
    {
    out.println("Welcome to Law of Averages ...");
    out.println("\ta program to find and display the average number");
    out.println("\tof words per sentence,");
    out.println("\tas well as the average length of a word");
    out.println("\tin a document.\n");

    Scanner reader = null;
    reader = new Scanner(new FileInputStream("limerick.txt"));

    int sentenceCount = 0;
    int wordCount = 0;
    int totalCharacters = 0;

    out.println("The contents of the document");
    out.println("----------------------------");
    while ( reader.hasNext() ) {
    String line = null;

    *** // read the next full line into the variable line





    Where the *** is, is where I need to input the line I cannot figure out.
    Last edited by FnL7676; 03-08-2011 at 12:15 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can just set scanner to take System.in, then look up scanner methods in the API, see if you can find one for the next line.

  5. #5
    FnL7676 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Scanner scanner = system.in(nextLine);
    ( it is trying to read a file, as you now know)
    Might that work perhaps?
    I am in a rush because this is due tonight, is all.
    Last edited by FnL7676; 03-08-2011 at 12:27 AM.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It takes System.in as it's argument to the constructor. NextLine is the correct method.

  7. #7
    FnL7676 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    But that wouldn't go where I am stuck would it? But above where the other scanner is, right? or am I confused even more.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you are wanting to read a file you shouldn't really use a scanner, there are better methods. Decorate an input stream with a buffered input stream, then look up what the buffered decorator adds as far as methods go, and use that to read in your line.

  9. #9
    FnL7676 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Alright, I figured as much.
    I will try that, if I can figure it out.
    (Netbeans isn't recognizing Bufferedreader). Dont know why.
    Last edited by FnL7676; 03-08-2011 at 12:48 AM.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Google some examples of using buffered stream and see if that helps you.

  11. #11
    FnL7676 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Ok.
    I think we are supposed to be using something that avoids messing with the code besides where we are supposed to input though. Is there an alternate way to do this that wouldn't require that?

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You would only be adding one line, changing one line then do the input getting line

  13. #13
    FnL7676 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Yeah, I'm not sure how/what to do/change.
    I really appreciate you helping me, but this is hard to do without you seeing what Im trying to do I guess. I know what I have to do, just not how to do it. I know you said you wouldn't but could you tell me what the line would be, so I can know for next time considering I cant figure it out?

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Google reading a file with java. It will show you working code for solving this. The part you are interested in is the buffered input stream and the input stream and how they are connected. I would show you a snippet but writing code on a phone is a pain.

  15. #15
    FnL7676 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Yeah all the code I can find is sort of what I want to do.
    But because none of what is online is identical, the line that goes somewhere else is hard for me to pinpoint even if I find the corresponding line that would go where I am stuck. ( Assuming i need to use something to the effect of BufferedReader input = new BufferedReader(new FileReader(aFile)); )

Similar Threads

  1. Quick Question...
    By FatalSylence in forum New To Java
    Replies: 4
    Last Post: 10-15-2010, 03:38 PM
  2. Quick question about this simple code..
    By shroomiin in forum New To Java
    Replies: 2
    Last Post: 11-10-2009, 06:58 PM
  3. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 09:01 PM
  4. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 03:26 PM
  5. Simple version of quick sort
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:40 PM

Posting Permissions

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