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 6 of 6

Thread: Why won't this code loop?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Location
    Stockholm, Sweden
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why won't this code loop?

    Hello everyone, I'm brand new to java and also to this forum. Hoping to get some help and maybe help others when I get better at this.

    Ok, so I tried to make a loop, if you enter "y" the code is supposed to loop, otherwise the program should terminate, but for some reason it won't loop, and I can't figure out what I'm doing wrong...

    import java.util.Scanner;
     
    public class scanner {
    	public static void main (String args[]){
     
    		Scanner scn = new Scanner(System.in);
    		Scanner go = new Scanner(System.in);
     
    		int fnum, snum, answer;
    		String yn = "y";
     
    		while(yn == "y"){	// this is there my loop starts
     
    			System.out.print("enter first number: ");
    			fnum = scn.nextInt();
    			System.out.print("enter second number: ");
    			snum = scn.nextInt();
    			answer = fnum - snum;
    			System.out.println(fnum + " - " + snum + " = " + answer);
     
    			System.out.print("go again (y/n)? ");
    			yn = go.nextLine();
    			System.out.print(yn);	// just so make sure yn="y"
    		}	
    	}
    }
    Last edited by Joniverse; March 29th, 2012 at 03:53 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why won't this code loop?

    Use the equals method to compare Strings, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Location
    Stockholm, Sweden
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why won't this code loop?

    If i do that, i get a "Type mismatch: cannot convert from Strings to boolean"...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why won't this code loop?

    Please post the code that created the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Location
    Stockholm, Sweden
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why won't this code loop?

    It is this one, here without the ==.

    The error message reads:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Type mismatch: cannot convert from String to boolean

    at scanner.main(scanner.java:12)

    import java.util.Scanner;
     
    public class scanner {
    	public static void main (String args[]){
     
    		Scanner scn = new Scanner(System.in);
    		Scanner go = new Scanner(System.in);
     
    		int fnum, snum, answer;
    		String yn = "y";
     
    		while(yn = "y"){	// this is there my loop starts
     
    			System.out.print("enter first number: ");
    			fnum = scn.nextInt();
    			System.out.print("enter second number: ");
    			snum = scn.nextInt();
    			answer = fnum - snum;
    			System.out.println(fnum + " - " + snum + " = " + answer);
     
    			System.out.print("go again (y/n)? ");
    			yn = go.nextLine();
    			System.out.print(yn);	// just so make sure yn="y"
    		}	
    	}
    }
    Last edited by Joniverse; March 29th, 2012 at 10:10 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why won't this code loop?

    while(yn = "y"){	// this is there my loop starts
    The while statement requires a boolean value inside of the ().
    Your code has an assignment statement inside of the ()s that evaluates to a String.
    cannot convert from String to boolean
    The compiler is complaining that there is no way to convert the String to the required boolean

    Where are you using the equals() method that I recommended?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  2. Need help playing sound in backround of a program in a loop.(Please Fix my code!)
    By DusteroftheCentury in forum What's Wrong With My Code?
    Replies: 30
    Last Post: January 21st, 2012, 02:14 AM
  3. Do Loop, Reversing a Number Example, Incorrect Code in Book?
    By djl1990 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 24th, 2011, 07:01 AM
  4. a problem with a code... (for loop and an "if" nested..)
    By kobi1988 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 13th, 2011, 12:59 PM
  5. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM