3 Replies - 314 Views - Last Post: 15 May 2020 - 12:11 PM Rate Topic: -----

#1 ashlymargret   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-May 20

Exception in thread "main" java.util.NoSuchElementException

Posted 15 May 2020 - 08:46 AM

package com.example.test;
import java.util.*;
public class MyClass {
	    public static void main (String[] args) {
	    	Patient patient = new Patient("brad",45,
					new Eye("Left eye", "short sighted", "brown", true),new Eye("right eye","short sighted","brown",true),
					new Heart("heart","normal",80), new Stomach("stomach","acidic",true),
					new Skin("skin","burned","wheatish", 80)
					);
			System.out.println("Name : " + patient.getName());
			System.out.println("Age : "+ patient.getAge());
			boolean shouldQuit = false;
			while(!shouldQuit){
				System.out.println("Choose and organ : " +
						"\n\t1. Left eye" +
						"\n\t2. Right eye" +
						"\n\t3. Heart" +
						"\n\t4. Stomach" +
						"\n\t5. Skin" +
						"\n\t6. Quit");
				Scanner scanner = new Scanner(System.in);
				int choice = scanner.nextInt();
				switch (choice){
					case 1:
						patient.getLefteye().getDetail();
						if(patient.getLefteye().isOpen()){
							System.out.println("\t\t1. Close");
							if(scanner.nextInt()==1){
								patient.getLefteye().close();
							}
						}
						else{
							System.out.println("\t\t1. Open");
							if(scanner.nextInt()==1){
								patient.getLefteye().open();
							}
						}
						break;
					case 2:
						patient.getRighteye().getDetail();
						if(patient.getRighteye().isOpen()){
							System.out.println("\t\t1. Close");
							if(scanner.nextInt()==1){
								patient.getRighteye().close();
							}
						}
						else{
							System.out.println("\t\t1. Open");
							if(scanner.nextInt()==1){
								patient.getRighteye().open();
							}
						}
						break;
					case 3:
						patient.getHeart().getDetail();
						System.out.println("\t\t1. change rate :");
						if(scanner.nextInt()==1) {
							patient.getHeart().changeRate(scanner.nextInt());
						}
						break;
					case 4:
						patient.getStomach().getDetail();
						System.out.println("\t\t1. Digest");
						if (scanner.nextInt()==1) {
							patient.getStomach().digest();
						}
						break;
					case 5:
						patient.getSkin().getDetail();
						break;
					case 6:
						shouldQuit=true;
						break;
					default:
						shouldQuit=true;
						break;
				}
			}

	    }
    }

error


20:59:13: Executing task 'MyClass.main()'...

Executing tasks: [MyClass.main()] in project C:\project

> Task :test:compileJava UP-TO-DATE
> Task :test:processResources NO-SOURCE
> Task :test:classes UP-TO-DATE

> Task :test:MyClass.main() FAILED
Name : brad
Age : 45
Choose and organ :
1. Left eye
2. Right eye
3. Heart
4. Stomach
5. Skin
6. Quit
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at com.example.test.MyClass.main(MyClass.java:22)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test:MyClass.main()'.
> Process 'command 'C:/Program Files/Android/Android Studio/jre/bin/java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle....d_line_warnings

BUILD FAILED in 388ms
2 actionable tasks: 1 executed, 1 up-to-date
20:59:13: Task execution finished 'MyClass.main()'.

Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.util.NoSuchElementException

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Exception in thread "main" java.util.NoSuchElementException

Posted 15 May 2020 - 08:55 AM

Try moving the declaration of scanner outside of the loop. The code looks like it creates a new Scanner object every time the loop iterates. You only need to create one and then use it as needed.

This post has been edited by NormR: 15 May 2020 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 703
  • View blog
  • Posts: 4,503
  • Joined: 12-January 10

Re: Exception in thread "main" java.util.NoSuchElementException

Posted 15 May 2020 - 08:56 AM

what line number does it break on -- you tossed a lot of code up there
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: Exception in thread "main" java.util.NoSuchElementException

Posted 15 May 2020 - 12:11 PM

You're apparently running tests on that class, so you need to simulate human input if you're doing that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1