←  Code Block

Fallout Studios Forums

»

[Java] Having two types of data on input

Waris's Photo Waris 03 Apr 2008

The exact objective of this program is to determine whether the year, input by user, is a leap year. In order to end the program the user needs to key in 'exit' or one of its variant ('Exit', 'EXIT' etc).

As you can see, the input data types are integer (if the user enters a year) and string (if the user enters 'exit') respectively.

 import java.util.Scanner;
 
 public class LeapYear
 {
	 public static void main(String[] args)
	 {
		 Scanner input = new Scanner(System.in);
		 System.out.println("This program can determine whether a year is a leap year or not.");
		 System.out.println("You may repeat this program as long as you want, otherwise enter 'exit'.");
		 int y, rem4, rem100, rem400;
		 String cont = "yadda";
		 
		 while(cont != "exit" || cont != "Exit" || cont != "EXIT")
		 {
		 System.out.print("Enter a year: ");
		 y = input.nextInt();
		 cont = input.nextLine();
		 rem4 = y % 4;
		 rem100 = y % 100;
		 rem400 = y % 400;
		 
		 if (rem4 == 0 || rem400 == 0 && rem100 != 0)
			 System.out.println("This year is a leap year.");
		 else
			 System.out.println("This year is not a leap year.");
		 }
	 }
 }


The code works flawless... except that when I want to exit the program, it does so in an Exception :P

What have I done wrong? Pls help *cute puppy face*
Quote

UnderFlow's Photo UnderFlow 03 Apr 2008

The problem is here:
System.out.print("Enter a year: ");
y = input.nextInt();
cont = input.nextLine();


If the user does not enter a string that can be interpreted as a number, the corresponding method will throw an exception.
This happens for example if the user enters 'exit'.



Edit: My solution

import java.util.Scanner;

public class LeapYear {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out
				.println("This program can determine whether a year is a leap year or not.");
		System.out
				.println("You may repeat this program as long as you want, otherwise enter 'exit'.");
		int y, rem4, rem100, rem400;
		String cont = "yadda";

		while (true) {// infinite loop
			System.out.print("Enter a year: ");
			cont = input.nextLine();// read line
			if (cont.compareToIgnoreCase("exit") == 0) // is the input equal to
										// "exit"?
				return;// return from main == terminate app

			try {// the try-catch-block catches exceptions
				y = Integer.valueOf(cont);// convert string to int

				rem4 = y % 4;
				rem100 = y % 100;
				rem400 = y % 400;

				if (rem4 == 0 || rem400 == 0 && rem100 != 0)
					System.out.println("This year is a leap year.");
				else
					System.out.println("This year is not a leap year.");
			} catch (Exception e) {// if the user enters an invalid string
				// he will land here
				System.out.println("Enter a number, idiot!");
			}
		}
	}
}



Do not compare strings with the == operator; use specific methods (compareTo);
Some people might say that the infinite loop/break (return in this case) construct is bad style.
If you have any further questions, do not hesitate to ask.^^

PS: I didn't change the algorithm, it still says that 1900 was a leap year.
Edited by UnderFlow, 03 April 2008 - 14:34.
Quote

CodeCat's Photo CodeCat 03 Apr 2008

To elaborate on the use of == on strings:

When used with built-in types, == returns true if the values are equal. However, String is not a built-in type but an object type. And as you might know, in Java objects are always used by pointer/reference and created with 'new'. Therefore, when you use == on two Strings, what you're actually doing is comparing the references of the strings. Hence, they are only equal if both references point to the same object. That does of course mean that they are equal, but it does not guarantee that they are NOT equal.

Try this to prove my point:
String s1 = "Hi!";
String s2 = "Hi!";

if (s1 == s1)
	System.out.writeln("s1 and s2 are equal according to ==.");

if (s1.equals(s2))
	System.out.writeln("s1 and s2 are equal according to equals().");
Quote

Waris's Photo Waris 04 Apr 2008

Thanks guys for the explanation :) UnderFlow I'll look into your solution, in fact I just learned how to handle exceptions in lecture today :D
Quote