How to Run Main Again Java

#ane

  • New D.I.C Head

Reputation: 1

  • View blog
  • Posts: 1
  • Joined: 04-Nov 12

How can I restart or telephone call to method main again?

Posted 04 Nov 2012 - 10:31 AM

Hello I'thou trying to write a program use if statements and a while loop where you input a number (0-13), and it'll requite you the calendar month for the number you entered (I.Due east 1 = January). If you enter something below 0 or to a higher place 13, it tells you and error and should re-prompt y'all for input <--- that's the function I can't seem to get.

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

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:eight)
at Months.main(Months.java:14)

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

import coffee.util.Scanner; public grade Months {  	public static void main(String[] args) { 	int num = 0; 	Scanner input = new Scanner(Arrangement.in); 	System.out.println("Enter the numeric number you want to print (13 to quit): " ); 	num = input.nextInt(); 	input.shut(); 	 		while (num < 0 || num > thirteen) 		{ 			System.out.println("You entered an invalid pick, please enter over again."); 			Months.main(args); 		 			} 		if (num == 0){ 			System.out.println("Yous have entered 0, the first calendar month starts at 1!"); 		} 		else if (num == 1){ 			System.out.println("You entered January");  		} 		else if (num == two){ 			Arrangement.out.println("Y'all entered February"); 		} 		else if (num == 3){ 			System.out.println("You entered March"); 		} 		else if (num == four){ 			Arrangement.out.println("You lot entered April"); 		} 		else if (num == 5){ 			System.out.println("You lot entered May"); 		} 		else if (num == half dozen){ 			System.out.println("You entered June"); 		} 		else if (num == 7){ 			System.out.println("Yous entered July"); 		} 		else if (num == eight){ 			System.out.println("Yous entered August"); 		} 		else if (num == 9){ 			Organisation.out.println("You lot entered September"); 		} 		else if (num == x){ 			Arrangement.out.println("You lot entered October"); 		} 		else if (num == 11){ 			System.out.println("You entered November"); 		} 		else if (num == 12){ 			System.out.println("You lot entered December"); 		} 	} }            


Is This A Skillful Question/Topic? one

  • +

#2 aejames23 User is offline

  • New D.I.C Head

Reputation: 0

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

Re: How can I restart or phone call to method master once more?

Posted 04 November 2012 - 11:36 AM

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

Using something like that

This mail service has been edited by aejames23: 04 November 2012 - xi:40 AM

#3 pbl User is offline

Reputation: 8381

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

Re: How can I restart or telephone call to method chief once again?

Posted 04 Nov 2012 - 11:52 AM

You just can't do that:

Months.main(args);

when you'll commencement main() again the int num in the new run is not the aforementioned int num and so your calling program so:

while (num < 0 || num > 13)

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

#4 gautamw User is offline

  • New D.I.C Caput

Reputation: 2

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

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

Posted 18 Jan 2014 - 06:59 PM

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

You just can't exercise that:

Months.primary(args);

when you'll kickoff 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 alter the actual num

He tin definitely practise that. The problem with his code is he closing the scanner before recalling the main role. This if he moves the input.scanner to end of the main role (Just before closing the main office), his code volition work perfectly, as desired :)

#5 gautamw User is offline

  • New D.I.C Head

Reputation: 2

  • View blog
  • Posts: 3
  • Joined: xviii-Jan 14

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

Posted eighteen Jan 2014 - 07:12 PM

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

The trouble with your lawmaking is closing the scanner earlier recalling the principal function. Also you have to utilise the if argument instead of while. Thus if you move the input.close() to end of the master function (Just before endmost the main office) and use if instead of while, your code will piece of work perfectly, every bit desired :)/>/>

Below is the updated lawmaking.

import coffee.util.Scanner;  public class Months {   public static void chief(String[] args) {      int num = 0;      Scanner input = new Scanner(System.in);      Arrangement.out.println("Enter the numeric number y'all want to print (xiii to quit): " );      num = input.nextInt();      if (num < 0 || num > thirteen)     {        System.out.println("Y'all entered an invalid choice, please enter again.");        Months.main(args);        }          else{          if (num == 0){              Organisation.out.println("You have entered 0, the first month starts at 1!");          }          else if (num == i){              Arrangement.out.println("You entered January");          }          else if (num == ii){              Arrangement.out.println("Y'all entered February");          }          else if (num == 3){         	             System.out.println("You lot entered March");          }          else if (num == 4){              System.out.println("Y'all entered April");          }          else if (num == 5){              System.out.println("You entered May");          }          else if (num == 6){              System.out.println("You lot entered June");          }          else if (num == 7){              Organisation.out.println("You lot entered July");          }          else if (num == 8){              System.out.println("You entered August");          }          else if (num == nine){              System.out.println("You entered September");          }          else if (num == 10){              Arrangement.out.println("You entered October");         }          else if (num == 11){              Arrangement.out.println("You entered November");          }          else if (num == 12){              System.out.println("You entered December");          }      } input.close();     }  }            

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

#half-dozen yashwanth.c.b User is offline

  • D.I.C Caput

Reputation: 31

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

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

Posted 18 January 2014 - 08:58 PM

and postal service your code in code tags.

#vii macosxnerd101 User is offline

Reputation: 12804

  • View blog
  • Posts: 45,994
  • Joined: 27-December 08

Re: How can I restart or phone call to method principal once more?

Posted xviii January 2014 - xi:42 PM

Might too put the months in an array:

String[] months = {"January", "February", ..., "Dec"}; int monthNumber = scanner.nextInt(); //a number i-12 System.out.println(months[monthNumber - 1]);            

#8 gautamw User is offline

  • New D.I.C Head

Reputation: 2

  • View blog
  • Posts: three
  • 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 - xi:42 PM, said:

Might besides put the months in an array:

Cord[] months = {"January", "February", ..., "Dec"}; int monthNumber = scanner.nextInt(); //a number one-12 Organization.out.println(months[monthNumber - i]);              

Great thanks!!

halltrubmisoace.blogspot.com

Source: https://www.dreamincode.net/forums/topic/298622-how-can-i-restart-or-call-to-method-main-again/

0 Response to "How to Run Main Again Java"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel