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: Can the same scanner input handle more than one datatype?

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Can the same scanner input handle more than one datatype?

    I have a string array that I want the user to associate with the number of a month:
    package samsExperiments;
     
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Random;
     
    import static samsExperiments.StaticMethodExample.*;
     
    import java.lang.StringBuilder;
    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.InputMismatchException;
     
    import customExceptions.IntegerOutOfRangeException;
     
    public class SamsExperimentsMain {
     
    	public static void main(String[] args){
     
    		Scanner input = new Scanner(System.in);
    		System.out.println("Welcome to the Month selector\n");
     
    		String[] monthName = {"January", "February", "March", "April", "May",
    				"June", "July", "August", "September", "November", "December"};
     
    		String choice = "y";
    		while(choice.equalsIgnoreCase("y")) {
    			System.out.println("Enter a month number, between 1 and 12.");
    			int monthNumber = input.nextInt();
     
    			//validate input
    			if(monthNumber < 1 || monthNumber > monthName.length) {
    				System.out.println("Invalid month number. Try again.");
    				continue;
    			}
    			int monthIndex = monthNumber - 1;
    			System.out.println("You selected " + monthName[monthIndex]);
    			System.out.print("Do you want to continue? (y/n): ");
    			choice = input.nextLine();
    			System.out.println();
    		}
    	}
    }

    However, without any error messages, the program got stuck here:

    Welcome to the Month selector

    Enter a month number, between 1 and 12.
    2
    You selected February
    Do you want to continue? (y/n):


    That wouldn't have anything to do with taking user input as a string instead of an integer, would it?

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

    Default Re: Can the same scanner input handle more than one datatype?

    There is a problem with using different Scanner class's methods:
    https://christprogramming.wordpress....on-mistakes-1/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I need help on creating a method to handle minimum and maximum input values
    By Manzanita in forum Object Oriented Programming
    Replies: 1
    Last Post: April 2nd, 2014, 02:22 AM
  2. [SOLVED] Parsing input from a file using Scanner
    By Helplessdrowningpuppy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2014, 04:15 PM
  3. Scanner multipule input [Solved]
    By Dragon-RD in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 17th, 2013, 09:03 AM
  4. File input using Scanner help.
    By AaronHarris in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 4th, 2013, 07:15 AM
  5. Scanner skipping input
    By hblakek in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 21st, 2012, 01:44 PM