DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2002
    Posts
    15

    String Variables In while loops

    Hello all, i am having trouble making a while loop that has the user press Y or N , either Y to re-run the program, or N to quit ,,, for some reason it will only end the program when Y or N is entered.. here is the source...

    /*
    import java.io.*;

    public class Assignment4
    {
    public static void main(String args[]) throws IOException
    {
    BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
    String choice="y";
    while(choice=="y")
    {
    System.out.print("How many numbers do you have? > ");
    int n = Integer.parseInt(input.readLine());

    int x=0;
    int largest=0;
    int second=0;
    int third=0;

    while(x<n)
    {
    System.out.print("Input number "+ (x+1) + "> ");
    int i = Integer.parseInt(input.readLine());

    if(i>largest)
    {
    third = second;
    second = largest;
    largest = i;
    }
    else if(i > second)
    {
    third = second;
    second = i;
    }
    else
    {
    third = i;
    }
    x+=1;
    }
    System.out.println("The three largest numbers are: " + largest + ", " + second + ", " + third);
    System.out.println();
    System.out.println("Compute again? <y/n>");
    choice=input.readLine();

    }
    System.out.println("Normal termination!");

    }
    }
    */

  2. #2
    Join Date
    Sep 2002
    Posts
    16
    Why don't you try something like this.

    String response = 'Y'


    While (response.equalsIgnoreCase("Y"))
    {
    //Do your stuff...
    }
    Jayceepoo

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links