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

Thread: How to read multiple integers without assigning them to seperate variables?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Seattle, Washington
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to read multiple integers without assigning them to seperate variables?

    I have this program that i need to be able to take multiple integers(in this case years) from the user. So far the program works with just one integer. I need the program to be able to take in multiple years seperated by a space in one line of input.
    thanks in advance!
    import java.util.Scanner;
     
    public class GregorianCalendar {
    			public static void main (String[] args) {
    		    	Scanner scan  = new Scanner(System.in);
    			int year;
    			System.out.print("Enter the year: ");
    			year = scan.nextInt();
    			    if (year < 1582){
    			    	System.out.println("Error; year invalid");
    			    	System.exit(0);
    			    }
    			if (year % 4 == 0) {
    				if (year % 100 != 0) {
    				System.out.println(year + " is a leap year");
    			    }
    			    else if (year % 400 == 0) {
    				System.out.println(year + " is a leap year");
    			    }
    			    else {
    				System.out.println(year + " is not a leap year");
    			    }
    			}
    			else {
    			    System.out.println(year + " is not a leap year");
    			}
    			}
        }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to read multiple integers without assigning them to seperate variables?

    Read the entire line of input as a String. Then use a String method to break that single String into an array containing the years as individual Strings.
    Improving the world one idiot at a time!

Similar Threads

  1. referencing variables from multiple instances of a class?
    By nickdesigner in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 23rd, 2013, 08:24 PM
  2. problem accessing variables in multiple classes
    By javaiscool in forum Java Theory & Questions
    Replies: 4
    Last Post: March 20th, 2013, 06:15 PM
  3. Replies: 1
    Last Post: May 16th, 2012, 05:15 PM
  4. "Separate Numbers"-Problem:Assigning Variables to User Inputs.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2012, 06:58 PM
  5. Multiple Identical Scripts - Variables and Load Handlers
    By cas22 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 22nd, 2011, 11:37 AM