Results 1 to 3 of 3
  1. #1
    Exultant is offline Member
    Join Date
    Apr 2017
    Posts
    1
    Rep Power
    0

    Default I have no clue how this isn't working.

    pls help
    Java Code:
    package Chapter7;
    
    import java.util.Scanner;
    
    public class TimeCoverter {
    	public static void main(String args[]){ 
    		int choice;
    	
    		Scanner input = new Scanner(System.in);
    		System.out.println("Pick a conversion ( 1 - 4 )");
    		System.out.println("1 ) Hours to Minutes ");
    		System.out.println("2 ) Days to Hours");
    		System.out.println("3 ) Minutes to Hours");
    		System.out.println("4 ) Hours to Days");
    		System.out.println("Enter : ");
    		choice = input.nextInt();
    		input.close();
    		
    		switch (choice){
    		case 1: HtM();
    			break;
    		case 2: DtH();
    			break;
    		case 3: MtH();
    			break;
    		case 4: HtD();
    			break;
    		}
    		
    		
    	}
    	public static void HtM(){ 
    		double hours, minutes;
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter amount of hours : ");
    		hours = input.nextDouble();
    		minutes = hours * 60;
    		System.out.println(hours + " hours in " + minutes + " minutes.");	
    		input.close();
    	}
    	public static void DtH(){ 
    		double days, hours;
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter amount of days : ");
    		days = input.nextDouble();
    		hours = days * 24;
    		System.out.println(days + " days in " + hours + " hours.");	
    		input.close();
    	}
    	public static void MtH(){ 
    		double minutes, hours;
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter amount of minutes : ");
    		minutes = input.nextDouble();
    		hours = minutes / 60;
    		System.out.println(minutes + " minutes in " + hours + " hours.");	
    		input.close();
    	}
    	public static void HtD(){ 
    		double hours, days;
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter amount of hours : ");
    		hours = input.nextDouble();
    		days = hours / 24;
    		System.out.println(hours + " hours in " + days + " days.");	
    		input.close();
    	}
    }
    I get this error when doing stuff.

    Pick a conversion ( 1 - 4 )
    1 ) Hours to Minutes
    2 ) Days to Hours
    3 ) Minutes to Hours
    4 ) Hours to Days
    Enter :
    2
    Enter amount of days :
    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.nextDouble(Unknown Source)
    at Chapter7.fghdfg.DtH(fghdfg.java:45)
    at Chapter7.fghdfg.main(fghdfg.java:22)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: I have no clue how this isn't working.

    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.nextDouble(Unknown Source)
    at Chapter7.fghdfg.DtH(fghdfg.java:45)
    at Chapter7.fghdfg.main(fghdfg.java:22)
    The code on line 45 called nextDouble without any input available to the Scanner.

    Never close the Scanner when it is using System.in. That will turn off console input for the rest of the program.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    raghusomineni is offline Member
    Join Date
    Apr 2017
    Posts
    2
    Rep Power
    0

    Default Re: I have no clue how this isn't working.

    Hi,

    Remove input.close(); form main() method. Your program works.

    If you close input stream once, it closes input to the remaining part of the program.

    If you still have doubt, let me know.

Similar Threads

  1. Code wont work have no clue why
    By CreatingDrake in forum New To Java
    Replies: 12
    Last Post: 11-25-2013, 08:52 AM
  2. Replies: 5
    Last Post: 10-12-2012, 10:12 AM
  3. No clue how to do this.
    By jjth39347 in forum New To Java
    Replies: 17
    Last Post: 03-02-2011, 05:11 AM
  4. I have no clue what this is... Please Help
    By 4rch in forum New To Java
    Replies: 3
    Last Post: 12-29-2010, 08:34 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •