Exception in thread "main" java.util.NoSuchElementException (I'm really new to this) -
i'm having problems simple code, i've seen problem everywhere can't seem find fix work me, again started learning java 3 weeks ago.
this error after compiling , running
$java -xmx512m -xms64m roleplayinggame name adventurer. exception in thread "main" java.util.nosuchelementexception @ java.util.scanner.throwfor(scanner.java:907) @ java.util.scanner.next(scanner.java:1416) @ roleplayinggame.main(roleplayinggame.java:11) and code. isn't urgent, i'll learn in class sometime i'd still know, it's not class or anything, it's pet project of mine. here's small amount of code.
import java.util.scanner; public class roleplayinggame { public static void main (string [] args) { scanner keyboard = new scanner (system.in); int enemyhack, enemyslash, enemystab, enemyblock, hack, slash, stab, block, potion, health, enemyhealth, enemyhealth2, enemyhealth3, enemyhealth4, enemyhealth5, level; string hack2, slash2, stab2, block2, potion2, name; level = 1; health = 20+(level*5); system.out.println("name adventurer."); name = keyboard.next(); system.out.println("your name " + name + " ."); system.out.println("you're passing through murky forest."); system.out.println("you've been travelling many days. , tired."); system.out.println("there has been heavy fog past week , it"); system.out.println("has begun grate on nerves. however,"); system.out.println("you've noticed fog lessening. fog lessens"); system.out.println("you begin notice shapes, draw sword."); system.out.println("as pass through fog demon lunges @ you."); system.out.println("you let forth battlecry , begin fight."); system.out.println("the enemies health " + enemyhealth + " whereas yours " + health + " . want flee?"); } } after added level , health started getting new error while compiling. might've been site using (i using online compiler, site went down minute after started getting error).
the error lies here: name = keyboard.next();
you should use name = keyboard.nextline();
the java.util.scanner.next() method finds , returns next complete token scanner. complete token preceded , followed input matches delimiter pattern, blank space default.
you may want take @ documentation of scanner class
you should take @ java naming convention
Comments
Post a Comment