9 Replies - 8214 Views - Last Post: 19 January 2009 - 09:15 PM Rate Topic: -----

#1 binarimon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 08-January 09

Making an If statement work in a While loop

Post icon  Posted 19 January 2009 - 05:05 PM

//Prof suggests I add an If statement to my existing While loop, but he didn't elaborate. would
//I replace the IF statement I have, or add another? The goal is to just get the response to say Goodbye
//when I type Exit or exit. Right now it responds the same as when I type in any string.
//I read the input, do the output, but maybe only then test for "exit"?
//Would I be better off with a While loop vs a Do While Loop?
//TOTALLY Confused.


/**
	 Homework assignment that prompts for user input
	 */
	import java.io.BufferedReader;
	import java.io.IOException;
	import java.io.InputStreamReader;

public class Homework1{

	public static void main(String[] args){
		BufferedReader dataIn = new BufferedReader(new
		InputStreamReader(System.in));

		String name ="";
		do
		{
			System.out.print("Hi, what's your name?");
			try{
				 name = dataIn.readLine();
				 if(!(name.compareToIgnoreCase("quit")==0));
				 System.out.println(name + " it's so nice to meet you");
				 				 
					 
				 }
				catch(IOException e)
				 { 
					  System.out.println("error");
				 }
		}while(!(name.compareToIgnoreCase("exit")==0));
		System.out.println("Goodbye!");
	}
}





This post has been edited by binarimon: 19 January 2009 - 05:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Making an If statement work in a While loop

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Making an If statement work in a While loop

Posted 19 January 2009 - 05:22 PM

Yeah replace the if you have there, it isn't doing anything anyways because you terminated it with a semicolon. This In its place you want to put an if statement like ...

if (!name.compareToIgnoreCase("exit")) { 
    System.out.println(name + " it's so nice to meet you");
}



This will allow the user to type in "exit" which will be stored in "name" and then with an if statement you check if it is equal to "exit" and if it is, you bypass the print. If it is not equal to exit, then it will say "____ its so nice to meet you"

:)

This post has been edited by Martyr2: 19 January 2009 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 binarimon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 08-January 09

Re: Making an If statement work in a While loop

Posted 19 January 2009 - 05:36 PM

Hi,

thanks for this- makes sense but I'm getting errors typing it in like this:

if (!name.compareToIgnoreCase("exit")) { 
    System.out.println(name + " it's so nice to meet you");
}



Says operator ! is undefined.

??

View PostMartyr2, on 19 Jan, 2009 - 04:22 PM, said:

Yeah replace the if you have there, it isn't doing anything anyways because you terminated it with a semicolon. This In its place you want to put an if statement like ...

if (!name.compareToIgnoreCase("exit")) { 
    System.out.println(name + " it's so nice to meet you");
}



This will allow the user to type in "exit" which will be stored in "name" and then with an if statement you check if it is equal to "exit" and if it is, you bypass the print. If it is not equal to exit, then it will say "____ its so nice to meet you"

:)

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Making an If statement work in a While loop

Posted 19 January 2009 - 05:48 PM

compareToIgnoreCase() returns an int
so
if (name.compareToIgnoreCase("exit") != 0) { 
	System.out.println(name + " it's so nice to meet you");
}


This post has been edited by mostyfriedman: 19 January 2009 - 05:48 PM

Was This Post Helpful? 1
  • +
  • -

#5 binarimon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 08-January 09

Re: Making an If statement work in a While loop

Posted 19 January 2009 - 05:57 PM

ok, the operator error went away but when I run and input "Exit". I still get "Exit, it's so nice to meet you!" instead of just goodbye!


/bin

View Postmostyfriedman, on 19 Jan, 2009 - 04:48 PM, said:

compareToIgnoreCase() returns an int
so
if (name.compareToIgnoreCase("exit") != 0) { 
	System.out.println(name + " it's so nice to meet you");
}


Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Making an If statement work in a While loop

Posted 19 January 2009 - 06:07 PM

import java.io.*;
public class HomeWork{
public static void main(String[] args){
		BufferedReader dataIn = new BufferedReader(new
		InputStreamReader(System.in));

		String name ="";
		do
		{
			System.out.print("Hi, what's your name?");
			try{
				 name = dataIn.readLine();
				 if(!(name.compareToIgnoreCase("exit")==0))
				 System.out.println(name + " it's so nice to meet you");
				
								 
					 
				 }
				catch(IOException e)
				 {
					  System.out.println("error");
				 }
		}while(!(name.compareToIgnoreCase("exit")==0));
		System.out.println("Goodbye!");
	}
}


ok i tested it for you, you just had to remove the ; after the if statement and change the condition in the while loop..now when the user enters exit, the loop will terminate and goodbye will be printed
Was This Post Helpful? 0
  • +
  • -

#7 binarimon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 08-January 09

Re: Making an If statement work in a While loop

Posted 19 January 2009 - 08:02 PM

Hi,

sorry, what do you mean "change the "condition" in the While Loop?

Just taking the ; out of causes an error.

/bin



View Postmostyfriedman, on 19 Jan, 2009 - 05:07 PM, said:

import java.io.*;
public class HomeWork{
public static void main(String[] args){
		BufferedReader dataIn = new BufferedReader(new
		InputStreamReader(System.in));

		String name ="";
		do
		{
			System.out.print("Hi, what's your name?");
			try{
				 name = dataIn.readLine();
				 if(!(name.compareToIgnoreCase("exit")==0))
				 System.out.println(name + " it's so nice to meet you");
				
								 
					 
				 }
				catch(IOException e)
				 {
					  System.out.println("error");
				 }
		}while(!(name.compareToIgnoreCase("exit")==0));
		System.out.println("Goodbye!");
	}
}


ok i tested it for you, you just had to remove the ; after the if statement and change the condition in the while loop..now when the user enters exit, the loop will terminate and goodbye will be printed

Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Making an If statement work in a While loop

Posted 19 January 2009 - 08:13 PM

sorry i meant that i changed the condition in the if statement and removed the ; that was after the if statement
if(!(name.compareToIgnoreCase("quit")==0));


changed it to
if(!(name.compareToIgnoreCase("exit")==0))


i did that to make things work

you shouldnt type a semicolon after if statements.
Was This Post Helpful? 0
  • +
  • -

#9 binarimon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 08-January 09

Re: Making an If statement work in a While loop

Posted 19 January 2009 - 09:00 PM

Okay cool. But what else did you change to get rid of the response:
"exit, it's nice to meet you"
"Goodbye!"

When you type in the word "exit" on the command line?

When I type Exit or exit, it should just come back cleanly with "goodbye!"



View Postmostyfriedman, on 19 Jan, 2009 - 07:13 PM, said:

sorry i meant that i changed the condition in the if statement and removed the ; that was after the if statement
if(!(name.compareToIgnoreCase("quit")==0));


changed it to
if(!(name.compareToIgnoreCase("exit")==0))


i did that to make things work

you shouldnt type a semicolon after if statements.

Was This Post Helpful? 0
  • +
  • -

#10 binarimon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 08-January 09

Re: Making an If statement work in a While loop

Posted 19 January 2009 - 09:15 PM

GOT IT!!! Needed to use name.equalsIgnoreCase not compareto.


First homework assignment that prompts for user input
	 */
	import java.io.BufferedReader;
	import java.io.IOException;
	import java.io.InputStreamReader;

public class Homework1{

	public static void main(String[] args){
		BufferedReader dataIn = new BufferedReader(new
		InputStreamReader(System.in));

		String name ="";
		do
		{
			System.out.print("Hi, what's your name?");
			try{
				 name = dataIn.readLine();
				 if(!(name.equalsIgnoreCase("exit")))
				 System.out.println(name + " it's so nice to meet you");
				 				 
					 
				 }
				catch(IOException e)
				 { 
					  System.out.println("error");
				 }
		}while(!(name.equalsIgnoreCase("exit")));
		System.out.println("Goodbye!");
	}
}





//Thanks for all your help here. I still don't get it all, but this was all very encouraging.

*Edited to fic the [ code] [ /code] tags

This post has been edited by pbl: 19 January 2009 - 09:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1