Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: "While" Problem

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "While" Problem

    I do noT undErstaNd why my pRogram inSists on continUing to aSk for a new Student, eveN after I have told my while Loop "no".

    Just wondeRing if Anyone can helP me.

    -Ki

     
    public class MainClass {
     
    	static private int nextStudent = 0;
    	static StudentInformation Student[] = new StudentInformation[100];
    	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	static boolean addAnotherStudent = true;
     
    	public static void main(String[] args) {		
     
    		while (addAnotherStudent == true) {
    			addStudent();
    			System.out.println("Would you like to add another Student?");
    			try {
    				String temp = br.readLine();
    				if (temp == "no") {
    					System.out.println("No more students to enter, Good bye");
    					addAnotherStudent = false;
    					}
    				else {
    						addAnotherStudent = true;
    					}
    				} 
    		catch (IOException ioExcption) {
    			System.out.println("IO exception occurred!");
    			System.exit(1);
    			}
    		}
    	}	
     
    	public static void addStudent(){
    		StudentInformation new1 = new StudentInformation();		
     
    		System.out.println("Enter your Student Information: ");
     
    		System.out.println("Enter student's full name: ");
    		 try {
    			 new1.setName(br.readLine());
    		 } 
    		 catch (IOException ioExcption) {
    			 System.out.println("IO exception occurred!");
    			 System.exit(1);
    		 }
     
    		 System.out.println("What is your address? ");
    		 try {
    			 new1.setAddress(br.readLine());
    		 } 	
    		 catch (IOException ioExcption) {
    			 System.out.println("IO exception occurred!");
    			 System.exit(1);
    		 }
     
    		 System.out.println("What is your city? ");
    		 try {
    			 new1.setCity(br.readLine());
    		 } 
    		 catch (IOException ioExcption) {
    			 System.out.println("A double was not entered!");
    			 System.exit(1);
    		 }
     
    		 System.out.println("What is your state? ");
    		 try {
    			 new1.setState(br.readLine());
    		 } 
    		 catch (IOException ioExcption) {
    			 System.out.println("IO exception occurred!");
    			 System.exit(1);
    		 }
     
    		 System.out.println("What is your zipcode? ");
    		 try {
    			 new1.setZipcode(br.readLine());
    		 } 
    		 catch (IOException ioExcption) {
    			 System.out.println("IO exception occurred!");
    			 System.exit(1);
    		 }
     
    		 System.out.println("What is your current GPA? ");
    		 try {
    			 new1.setCurrentGPA(Double.parseDouble(br.readLine()));
    		 } 
    		 catch (IOException ioExcption) {
    			 System.out.println("A double was not entered!");
    			 System.exit(1);
    		 }
     
    		System.out.println("Student's Information: "); 
    		System.out.println(new1.getName());
    		System.out.println(new1.getAddress());
    		System.out.println(new1.getCity());
    		System.out.println(new1.getState());
    		System.out.println(new1.getCurrentGPA());
     
    		Student[nextStudent] = new1;
    		++ nextStudent;
    	}
     
     
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: "While" Problem

    try using temp.equals("no") and would you please stop with the random capitals

    Thanks,
    Chris

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: "While" Problem

    Quote Originally Posted by Freaky Chris View Post
    try using temp.equals("no") and would you please stop with the random capitals

    Thanks,
    Chris
    Grrr I was just posting that myself!! You always need to do .equals("") on a String instead of ==
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Jul 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "While" Problem

    Thanks foR everythinG!! I was goinG throgh Hell!

  5. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: "While" Problem

    Quote Originally Posted by JavaPF View Post
    I think Kelly may be disabled Chris? When I asked about the caps:



    No idea?!
    He plas solely FPS games, but he dabbles with other things / different FPS's if that makes sense lol. What he said makes perfect sense in my eyes at least.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: "While" Problem

    LOL I obviousally read that totally wrong
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: "While" Problem

    @op

    The == operator only tests if the references to objects held by two variables is pointing to the same object, rather than looking at two object to see if they are equal. To compare two objects, you need to use the equals method.

    String s1 = new String("string");
    String s2 = new String("string");
     
    if (s1 == s2){
    	//This check will fail
    }

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: "While" Problem

    Thanks for sharing that for anybody who wishing to read more into it, maybe those who didn't know this here's a link, but read carefully otherwise you may think there is a === operator

    Java: ==, .equals(), compareTo(), and compare()

    Chris

  9. #9
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: "While" Problem

    However this will be true.

            String s1 = "string";
            String s2 = "string";
     
            if (s1 == s2) {
                // This is true
            }

    Because of the way strings work in Java.

    Another one to watch out for is the use of wrapper objects and ==

    This will be true.

            Integer integer1 = 127;
            Integer integer2 = 127;
     
            if (integer1 == integer2) {
                // True
            }

    But this will be false.

            Integer integer1 = 128;
            Integer integer2 = 128;
     
            if (integer1 == integer2) {
                // False
            }

    Scary stuff. Like the other posts in this thread, refrain from using == unless you know what you are doing or you are using primitives. When comparing a wrapper object with a primitive its also alright to use the == because the JVM will automagically unbox the wrapper for you and do a pure primitive comparison.

    // Json

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: "While" Problem

    ??? that last little bit made no sense!

    I thought the first would be false.

    This would also be true:
    Scanner input = new Scanner(System.in);
    Scanner in2 = input;
    if (input == in2)
    {
         // true
    }

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Replies: 2
    Last Post: March 23rd, 2010, 01:38 AM
  3. Replies: 4
    Last Post: June 18th, 2009, 09:23 AM
  4. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM