Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while loop" Watch "while loop" New topic
Author

while loop

William Coates
Greenhorn

Joined: May 20, 2003
Posts: 2
Hi,
I am trying to get the following program to contine checking for palidromes
until nothing is entered. I used a while loop but I am stuck.Could someone point me in the right direction?



Java hurts the brain.<br />SOLUTION:<br />More coffee!!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30


Just looking at this: args[0] is the first command line argument to the program. It is a String. So it can never be 0, and it won't change during the execution of the program (unless you change it, of course.)
Your while loop should, I think, be checking whether s is equals() to "".


[Jess in Action][AskingGoodQuestions]
William Coates
Greenhorn

Joined: May 20, 2003
Posts: 2
Hi Earnest,
Thanks for getting back to me. I looked at what you posted and I think
I have the right statment, now I am having difficulty in placing it in the right spot. I have this
Any more input would be greatly appreciated
[ November 11, 2003: Message edited by: William Coates ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Well, you'll want to move the call to readLine() inside the loop, of course, otherwise every iteration will operate on the same string.
You've also got your while loop running while the string is empty; you probably want it to run while the string is not empty, yes?
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
What about:
while (args[0].length != 0) {
that should run until the length of the first argument is 0 (blank input) - I'm a beginner too so there's no guarantee but it's worth a try
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Hi Dan,
Welcome to Java Ranch!

while (args[0].length != 0) {

But he's not looking at program arguments here, he's looking at data read from a stream; and as I said, "args" isn't going to change by itself while the program is running -- they're set when the program is started.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

args is the array of Strings passed in on the command line.
You are looking to process the interactive user input. That lives in "s" from the "readString()". Read the last post over again, you are almost there.


Please ignore post, I have no idea what I am talking about.
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
got it - so it probably should be:
while (s.length != 0){
System.out.print("Enter a string: ");
String s = MyInput.readString();
etc. . .;
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Right.
santoshsamala kumar
Greenhorn

Joined: Nov 02, 2003
Posts: 3
what is this MyInput type U didnt mentioned any where
Jonathan Zaleski
Greenhorn

Joined: Nov 11, 2003
Posts: 2
I am unsure exactly how adept you are in java programming, but if you have knowledge of stacks as well as queues you can create the palindrome tester very very easily. If you've not heard of these, you may wish to investigate, even if it's only for your own education. A stack's, first in last out structure and a queue's first in first structure make for a very easy way to compare the respective first and last char's of any input. Course you do ultimately need to consider for a few other things in this instance, but its an easy setup, and easy solution, just something to consider.
Regards, and good luck,
Jonathan W. Zaleski
IF you care to see the actual coding to my method, I can show you, and help you further as need be. If you have any questions, please feel free to get in touch.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Or you want to use reverse() in StringBuffer class
then compare those two strings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while loop
 
Similar Threads
More help on java modifications
palindrome not very proficient...
Palindrome example
Palindrome
palindrome program