Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java help

  1. Nov 6, 2006 #1
    Hello. I am writing a program, but I can't figure out how to do something. I want to do something like this:

    Sample input:

    abcdeft
    01234567
    asfg


    Sample output:

    The length of line 1 is: 6
    The length of line 2 is: 7
    The length of line 3 is: 3
     
    Last edited: Nov 6, 2006
  2. jcsd
  3. Nov 6, 2006 #2
    So I want to input any number of lines and then I want to give the length of each one of those lines. I want to write it with a while loop and use a sentinel value so it knows when I want to quite entering lines of text. Once I enter the sentinel value the program then outputs the length of each line.
     
  4. Nov 6, 2006 #3
    Should I do it like this:

    final String SENTINEL = stop;
    Scanner scan = new Scanner(System.in);
    String line = "";
    int length = 0;
    int n = 0;

    while( !line.equals(SENTINEL) )
    {
    line = scan.nextLine();
    length = line.length();i
    n = n +1;
    }

    System.out.println("The length of line " + n + " is: " + length);
     
    Last edited: Nov 6, 2006
  5. Nov 6, 2006 #4
    So my question is how do I get each n and length to show up (not just for the final line I enter)?
     
    Last edited: Nov 6, 2006
  6. Nov 6, 2006 #5

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Looks reasonable, except you'd need to put the System.out.println statement INSIDE the while loop. You want to print the length of every line, yes? Not just the last one?

    - Warren
     
  7. Nov 6, 2006 #6
    Yes, exactly right. But I don't want to print the data until after I am done entering the lines of text.
     
  8. Nov 6, 2006 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Then you need to store the information somewhere, e.g. in an array or Vector.

    - Warren
     
  9. Nov 6, 2006 #8
    We have not yet covered arrays. Is there another way to do it?
     
  10. Nov 6, 2006 #9

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Then you do not yet have the resources to write this program.

    - Warren
     
  11. Nov 6, 2006 #10
    Could you help me write that with an array then?
     
  12. Nov 6, 2006 #11

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Do you know the maximum number of lines the user might enter? Arrays are of fixed length.

    - Warrens
     
  13. Nov 6, 2006 #12
    No. One of the things I want to do is write as many lines as I want ( not keep the number of lines a fixed number ).

    What about a vector?
     
  14. Nov 6, 2006 #13
    Oh, MAXIMUM number. I could just choose a large number that I would never use, right?
    I will never use more than 10 lines.
     
  15. Nov 6, 2006 #14

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    If you intend to never read more than 10 lines, then you can make an array of ten integers, like this:

    int lengths[10];

    You'd then modify your for loop to store the line length in lengths[n].

    After you're done reading input, you'd need another for loop (from 0 to n-1) to print out the information stored in that array.

    - Warren
     
  16. Nov 6, 2006 #15
    Ok, hold on. Like I said, I am not familiar with arrays. So at the top where I am declaring all of the variables I would just write:

    int lengths[10];

    Then in the while loop I would do:

    length[n] = line.length();

    Do I have to change the while loop to a for loop?
     
    Last edited: Nov 6, 2006
  17. Nov 10, 2006 #16
    I would suggest an ArrayList, It is similar to an Array but can grow or shrink to fit the needed size. ArrayList lengths = new ArrayList();
    lengths.add(line.length); // adds the line length to the end of the list lengths
    lengths.get( enter a line number here); // returns the length of the line at the specified index of the list, for example if you place 3 in the parantheses then it returns the length of the thrid line, provided there is a third entry in your arraylist
     
    Last edited: Nov 10, 2006
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Java help
  1. Java help (Replies: 7)

  2. Java Help (Replies: 7)

  3. Need help with Java (Replies: 53)

  4. Help! Cant get Java! (Replies: 3)

Loading...