7 Replies - 588 Views - Last Post: 24 September 2012 - 06:30 PM Rate Topic: -----

#1 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 142
  • Joined: 05-September 12

repeat user entry inside loop

Posted 23 September 2012 - 07:22 PM

Ok, so I pretty much perfected the below code for what I want to do, with one tiny exception: how do I get the command line to reactivate so the user can keep entering words into the console?


import java.util.Scanner;

public class Palindrome {
	
	public static void main(String[] args){
		
		Scanner phrase = new Scanner(System.in);
				
		System.out.println("Enter a phrase to determine if it is a palindrome");
		
		String input = phrase.nextLine();
		input = input.toLowerCase();
		boolean isPalindrome  = true;
		int left = 0;
		int right = input.length() -1;
		
		
		while( left < right && left != right){
			
			if( input.charAt(left) != input.charAt(right)){
				isPalindrome = false;
				break;
					
			}
			
			left++;
			right--;
		}
		
		if (isPalindrome == true){
			
			System.out.println(input + " " + "is a palindrome.");
		}
		
		else{
			System.out.println(input + " " + "is not a palindrome.");
		}
		
		
	}
}






do{

while( input.length > 0);{

   input = phrase.nextLine();

}




maybe something like this?

Is This A Good Question/Topic? 0
  • +

Replies To: repeat user entry inside loop

#2 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 142
  • Joined: 05-September 12

Re: repeat user entry inside loop

Posted 23 September 2012 - 07:35 PM

Sould be


do{

input.phrase.nextLine();
} while(input.length() > 0);


Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: repeat user entry inside loop

Posted 23 September 2012 - 07:46 PM

Will be complicated for nothing you have to exit AFTER having read
better to use an infinite loop an to break out of it
while(true) {
    String line = scan.nextLine().trim();
    if(line.length() == 0)
       break;
    ... proceed
}


Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5423
  • View blog
  • Posts: 8,721
  • Joined: 19-March 11

Re: repeat user entry inside loop

Posted 23 September 2012 - 07:47 PM

You've got the right idea, a while or a do-while is about what you're looking for there.

Now, to get what you want, you need to set up your loop so that you ask for input until the user says they're done, and you only process input if you have input to process.
There are a few standard ways to get after this. I'll give examples in pseudo-code.

One is like this.

declare a variable to hold input
get the input  => inputVariable
while inputVariable is not "stopValue"
{
  process inputVariable
  get input => inputVariable
}



The trick here is you "prime the pump" with one input outside the loop, and from then on, you ask for input at the end of the loop. This way, you can exit cleanly when the user is finished.


Another way, also fine, would be

do 
{
  get input => inputVariable
  if inputVariable is not stopValue 
 
   process inputVariable 
} while inputVariable is not "stopValue"




I don't like that as much, but it's a perfectly legitimate way to do this.


The last one I'll mention drives some people up the wall, but it's also fine:


do 
{
  get input => inputVariable
  if inputVariable is  stopValue 
  break   ---->  takes us out of the loop immediately
  process inputVariable 
} while "true" (ie, until we break out of the loop from inside)




Each of these, correctly implemented, does the same thing, so you have to pick one that you like and be ready to work with other people who have different preferences. When you get into working in teams, stylistic compromises will be important, for now you should try to understand how these are all the same.

This post has been edited by jon.kiparsky: 23 September 2012 - 07:50 PM

Was This Post Helpful? 1
  • +
  • -

#5 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 142
  • Joined: 05-September 12

Re: repeat user entry inside loop

Posted 23 September 2012 - 07:57 PM

Now see, I focus on one thing and can't let go of it. I can make that work pbl. I will implement it tomorrow, when I get up. As usual, thanks.

I'll try your suggestions as well. I actually prefer help pseudo code. I want to learn this stuff, and so far everyone on here has been good at making sure us newbees find our own solutions. I'll post my results tomorrow. Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 142
  • Joined: 05-September 12

Re: repeat user entry inside loop

Posted 24 September 2012 - 06:05 PM

So here is something kind of funny. I have managed to get the program to allow for continous user entries; however, the palindrome only works some of the time. Example: Otto is a palindrome but Bob is not. Yet, aldkfjsldkfjsdlj will come up as "not a palindrome," which is correct. Here is the code.


import java.util.Scanner;

public class Palindrome {
	
	public static void main(String[] args){
		
		Scanner phrase = new Scanner(System.in);
				
		System.out.println("Enter a phrase to determine if it is a palindrome");
		
		String input = phrase.nextLine();
		input = input.toLowerCase();
		boolean isPalindrome  = true;
		int left = 0;
		int right = input.length() -1;
		
		while(true) {
			
			input = phrase.nextLine().trim();
			input = input.toLowerCase();
			if(input.charAt(left) != input.charAt(right)){
				isPalindrome = false;}
			
			else
				isPalindrome = true;
	
		while( left < right && left != right){
			
			if( input.charAt(left) != input.charAt(right)){
				isPalindrome = false;
				break;
					
			}
			
			left++;
			right--;
		}
		
		if (isPalindrome == true){
			
			System.out.println(input + " " + "is a palindrome.");
		}
		
		else{
			System.out.println(input + " " + "is not a palindrome.");
		}
		
		}
        }
}





Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5423
  • View blog
  • Posts: 8,721
  • Joined: 19-March 11

Re: repeat user entry inside loop

Posted 24 September 2012 - 06:14 PM

You might want to start by trying some test cases to see if they guide your thinking.

What do you get for the following?

""
"i"
"oio"
"oiio"
"oiuio"
"oiuuio"
"oiuyuio"

"iy"
"oioy"
"oiioy"
"oiuioy"
"oiuuioy"
"oiuyuioy"


You'll have wrong answers and some right ones. Do you see any patterns? Are there more false positives, or false negatives? Do the wrong answers distribute in any interesting ways?
Was This Post Helpful? 0
  • +
  • -

#8 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 142
  • Joined: 05-September 12

Re: repeat user entry inside loop

Posted 24 September 2012 - 06:30 PM

View Postjon.kiparsky, on 24 September 2012 - 06:14 PM, said:

You might want to start by trying some test cases to see if they guide your thinking.

What do you get for the following?

""
"i"
"oio"
"oiio"
"oiuio"
"oiuuio"
"oiuyuio"

"iy"
"oioy"
"oiioy"
"oiuioy"
"oiuuioy"
"oiuyuioy"


You'll have wrong answers and some right ones. Do you see any patterns? Are there more false positives, or false negatives? Do the wrong answers distribute in any interesting ways?


I see. I need to pull of the ends and compare each character one at a time. So Bob will compare B with b, but when it comes to "o" it will compare it with what? White space? Is this why it's best to use StringBuffer or StringBuilder? This is why Otto works but Oto doesn't.

This post has been edited by iburres: 24 September 2012 - 06:36 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1