Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while loop

 
William Coates
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 "".
 
William Coates
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right.
 
santoshsamala kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is this MyInput type U didnt mentioned any where
 
Jonathan Zaleski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you want to use reverse() in StringBuffer class
then compare those two strings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic