Thread Closed

Java help

 
Share Thread Thread Tools
Nov6-06, 08:36 PM   #1
 

Java help


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
 
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> 'Whodunnit' of Irish potato famine solved
>> The mammoth's lament: Study shows how cosmic impact sparked devastating climate change
>> Curiosity Mars rover drills second rock target
Nov6-06, 08:38 PM   #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.
 
Nov6-06, 08:43 PM   #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);
 
Nov6-06, 08:49 PM   #4
 

Java help


So my question is how do I get each n and length to show up (not just for the final line I enter)?
 
Nov6-06, 08:50 PM   #5
 
Recognitions:
Gold Membership Gold Member
Science Advisor Science Advisor
Retired Staff Staff Emeritus
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
 
Nov6-06, 08:54 PM   #6
 
Quote by chroot
You want to print the length of every line, yes? Not just the last one?
Yes, exactly right. But I don't want to print the data until after I am done entering the lines of text.
 
Nov6-06, 08:55 PM   #7
 
Recognitions:
Gold Membership Gold Member
Science Advisor Science Advisor
Retired Staff Staff Emeritus
Then you need to store the information somewhere, e.g. in an array or Vector.

- Warren
 
Nov6-06, 08:56 PM   #8
 
We have not yet covered arrays. Is there another way to do it?
 
Nov6-06, 08:58 PM   #9
 
Recognitions:
Gold Membership Gold Member
Science Advisor Science Advisor
Retired Staff Staff Emeritus
Then you do not yet have the resources to write this program.

- Warren
 
Nov6-06, 09:00 PM   #10
 
Could you help me write that with an array then?
 
Nov6-06, 09:01 PM   #11
 
Recognitions:
Gold Membership Gold Member
Science Advisor Science Advisor
Retired Staff Staff Emeritus
Do you know the maximum number of lines the user might enter? Arrays are of fixed length.

- Warrens
 
Nov6-06, 09:02 PM   #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?
 
Nov6-06, 09:03 PM   #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.
 
Nov6-06, 09:13 PM   #14
 
Recognitions:
Gold Membership Gold Member
Science Advisor Science Advisor
Retired Staff Staff Emeritus
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
 
Nov6-06, 09:13 PM   #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?
 
Nov10-06, 12:50 AM   #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
 
Thread Closed
Thread Tools


Similar Threads for: Java help
Thread Forum Replies
java q Computing & Technology 3
Help! Cant get Java! Computing & Technology 3
new to java Programming & Comp Sci 7
Help in Java 3D Computing & Technology 0
Java Introductory Physics Homework 13