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.


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

Results 1 to 2 of 2

Thread: Funny Behaviour

  1. #1
    Join Date
    Jan 2013
    Central Texas
    Thanked 0 Times in 0 Posts

    Default Funny Behaviour

    Here is a little program I wrote to help me memorize my student ID #. I will * out the actual number but here is my code.
    import java.util.Scanner;
    class baylorID {
    	public static void main(String[] args){
    		//clear the console
    		//store correct baylor id in a string
    		String correctID = "*********";
    		String inputID = new String();
    		String quit = "quit";
    		int numCorrect = 0;
    		int numIncorrect = 0;
    		int netCorrect = 0;
    		//ask user for student id
    		while (netCorrect < 10) {
    			Scanner scan = new Scanner(System.in);
    			System.out.println("What is your Student ID? \t Type 'quit' to exit.");		
    			inputID = scan.next();
    		//compare the two
    			if (inputID.equals(quit)) {
    				numCorrect = 12;
    				numIncorrect = 1;
    			if (inputID.equals(correctID)) {
    				System.out.println("Correct! You remembered it!");
    				numCorrect += 1;
    			} else {
    				System.out.println("Incorrect. Try Again.");
    				numIncorrect += 1;
    			netCorrect = numCorrect - numIncorrect;

    It compiles and runs successfully. However, when I type quit, it does not do what I want.

    What I Want:
    The program to stop running

    What Happens:
    What is your Student ID? 	 Type 'quit' to exit.
    Incorrect. Try Again.
    Then it stops running

  2. #2
    Join Date
    Sep 2012
    Thanked 14 Times in 14 Posts

    Default Re: Funny Behaviour

    I'm not sure why you need to "clear the console". I believe you may be confusing the scanner with the buffered writer class.

    As far as i know you just need to close it after use with scan.close()

    Try and declare the scanner at the top, so it has scope beyond the while loop.

    What does System.out.print("\033[H\033[2J"); do?

    One you have looked at the above, I think you need to review this part of the code:

    //compare the two
    			if (inputID.equals(quit)) {

Similar Threads

  1. Simple CashRegister program behaviour issue [Basic]
    By Moa in forum Java Theory & Questions
    Replies: 6
    Last Post: June 19th, 2012, 03:49 PM
  2. Funny output
    By Leiferson in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 16th, 2011, 07:21 PM
  3. Quicksort algorithm displaying strange behaviour
    By Mathew.Williams in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 19th, 2011, 12:56 PM
  4. Funny NullPointerException
    By Marcus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2009, 10:14 AM
  5. Swing incorrect behaviour from JRE5 to JRE6
    By singhkanhaiya in forum AWT / Java Swing
    Replies: 1
    Last Post: August 25th, 2009, 01:23 AM