7 Replies - 4336 Views - Last Post: 18 January 2014 - 11:55 PM Rate Topic: -----

#1 keaton177  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 04-November 12

How can I restart or call to method main again?

Posted 04 November 2012 - 10:31 AM

Hello I'm trying to write a program use if statements and a while loop where you input a number (0-13), and it'll give you the month for the number you entered (I.E 1 = January). If you enter something below 0 or above 13, it tells you and error and should re-prompt you for input <--- that's the part I can't seem to get.

I tried adding in calling the main method again in my while loop to see if that would work, but I get this error :

Quote

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Months.main(Months.java:8)
at Months.main(Months.java:14)


Here is my code (with the call to main method that causes error in it)

import java.util.Scanner;
public class Months {

	public static void main(String[] args) {
	int num = 0;
	Scanner input = new Scanner(System.in);
	System.out.println("Enter the numeric number you want to print (13 to quit): " );
	num = input.nextInt();
	input.close();
	
		while (num < 0 || num > 13)
		{
			System.out.println("You entered an invalid choice, please enter again.");
			Months.main(args);
		
			}
		if (num == 0){
			System.out.println("You have entered 0, the first month starts at 1!");
		}
		else if (num == 1){
			System.out.println("You entered January");

		}
		else if (num == 2){
			System.out.println("You entered February");
		}
		else if (num == 3){
			System.out.println("You entered March");
		}
		else if (num == 4){
			System.out.println("You entered April");
		}
		else if (num == 5){
			System.out.println("You entered May");
		}
		else if (num == 6){
			System.out.println("You entered June");
		}
		else if (num == 7){
			System.out.println("You entered July");
		}
		else if (num == 8){
			System.out.println("You entered August");
		}
		else if (num == 9){
			System.out.println("You entered September");
		}
		else if (num == 10){
			System.out.println("You entered October");
		}
		else if (num == 11){
			System.out.println("You entered November");
		}
		else if (num == 12){
			System.out.println("You entered December");
		}
	}
}
	







Is This A Good Question/Topic? 1
  • +

Replies To: How can I restart or call to method main again?

#2 aejames23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-September 12

Re: How can I restart or call to method main again?

Posted 04 November 2012 - 11:36 AM

 System.out.println("Enter the numeric number you want to print (13 to quit): " );
	    num = input.nextInt();
	    
	     
	    if (num < 0 || num > 13)
		{
			do
			{
				System.out.println("Number must be > 0 and less than 13,please enter again: ");
				num = input.nextInt();
			}
		while ((num < 0 || num > 13));
		}


Using something like that

This post has been edited by aejames23: 04 November 2012 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How can I restart or call to method main again?

Posted 04 November 2012 - 11:52 AM

You just can't do that:

Months.main(args);

when you'll start main() again the int num in the new run is not the same int num then your calling program so:



while (num < 0 || num > 13)

will always be true as you don't change the actual num
Was This Post Helpful? 0
  • +
  • -

#4 gautamw  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 3
  • Joined: 18-January 14

Re: How can I restart or call to method main again?

Posted 18 January 2014 - 06:59 PM

View Postpbl, on 04 November 2012 - 11:52 AM, said:

You just can't do that:

Months.main(args);

when you'll start main() again the int num in the new run is not the same int num then your calling program so:



while (num < 0 || num > 13)

will always be true as you don't change the actual num

He can definitely do that. The problem with his code is he closing the scanner before recalling the main function. This if he moves the input.scanner to end of the main function (Just before closing the main function), his code will work perfectly, as desired :)
Was This Post Helpful? 1
  • +
  • -

#5 gautamw  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 3
  • Joined: 18-January 14

Re: How can I restart or call to method main again?

Posted 18 January 2014 - 07:12 PM

I know its like an year you posted this, however for someone new to java like me, will surely come across.

The problem with your code is closing the scanner before recalling the main function. Also you have to use the if statement instead of while. Thus if you move the input.close() to end of the main function (Just before closing the main function) and use if instead of while, your code will work perfectly, as desired :)/>/>

Below is the updated code.

import java.util.Scanner;

public class Months {

 public static void main(String[] args) {

    int num = 0;

    Scanner input = new Scanner(System.in);

    System.out.println("Enter the numeric number you want to print (13 to quit): " );

    num = input.nextInt();

    if (num < 0 || num > 13)
    {
       System.out.println("You entered an invalid choice, please enter again.");
       Months.main(args);
       }
     
   else{

        if (num == 0){

            System.out.println("You have entered 0, the first month starts at 1!");

        }

        else if (num == 1){

            System.out.println("You entered January");

        }

        else if (num == 2){

            System.out.println("You entered February");

        }

        else if (num == 3){
        	
            System.out.println("You entered March");

        }

        else if (num == 4){

            System.out.println("You entered April");

        }

        else if (num == 5){

            System.out.println("You entered May");

        }

        else if (num == 6){

            System.out.println("You entered June");

        }

        else if (num == 7){

            System.out.println("You entered July");

        }

        else if (num == 8){

            System.out.println("You entered August");

        }

        else if (num == 9){

            System.out.println("You entered September");

        }

        else if (num == 10){

            System.out.println("You entered October");
        }

        else if (num == 11){

            System.out.println("You entered November");

        }

        else if (num == 12){

            System.out.println("You entered December");

        }
     }
input.close();
    }

}


This post has been edited by macosxnerd101: 18 January 2014 - 11:41 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 1
  • +
  • -

#6 yashwanth.c.b  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: How can I restart or call to method main again?

Posted 18 January 2014 - 08:58 PM

and post your code in code tags.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,117
  • Joined: 27-December 08

Re: How can I restart or call to method main again?

Posted 18 January 2014 - 11:42 PM

Might as well put the months in an array:
String[] months = {"January", "February", ..., "December"};
int monthNumber = scanner.nextInt(); //a number 1-12
System.out.println(months[monthNumber - 1]);


Was This Post Helpful? 1
  • +
  • -

#8 gautamw  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 3
  • Joined: 18-January 14

Re: How can I restart or call to method main again?

Posted 18 January 2014 - 11:55 PM

View Postmacosxnerd101, on 18 January 2014 - 11:42 PM, said:

Might as well put the months in an array:
String[] months = {"January", "February", ..., "December"};
int monthNumber = scanner.nextInt(); //a number 1-12
System.out.println(months[monthNumber - 1]);


Great thanks!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1