Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: My code has error when its run....I dont understand what's wrong of it.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My code has error when its run....I dont understand what's wrong of it.

    import java.util.* ;
    /**
       Complete the given class InputTester, which 
     
       1) reads an integer into variable i
       2) prints the value read
       3) reads a double into variable x 
       4) prints the value read
       5) reads a word into variable word
       6) prints the word read
       7) reads the rest of the words into variable line
       8) prints the string read
       9) prints the result of (i/i) + (x/x)
     */
    public class InputTester
    {
        public static void main(String[] args)
        {
    	Scanner scanner = new Scanner("5 3.1415 Hello World 19 more words") ;
    	//----------------------Start here. Do not remove this comment.
    	// todo:  1) reads an integer into variable i
    	// approximate lines of code required: 1
    	int i = scanner.nextInt();
    	//----------------------End here. Do not remove this comment.
    	System.out.println("Integer read: " + i) ;
    	//----------------------Start here. Do not remove this comment.
    	// todo:  3) reads a double into variable x 
    	// approximate lines of code required: 1
    	double x = scanner.nextDouble();
    	//----------------------End here. Do not remove this comment.
    	System.out.println("Double read: " + x) ;
    	//----------------------Start here. Do not remove this comment.
    	// todo:  5) reads a word into variable word
    	// approximate lines of code required: 1
    	String word = scanner.nextLine();
    	//----------------------End here. Do not remove this comment.
    	System.out.println("Word read: " + word) ;
    	//----------------------Start here. Do not remove this comment.
    	// todo:  7) reads the rest of the words into variable line
    	// approximate lines of code required: 1
    	String line = scanner.next();    <-----------System said this line error
    	//----------------------End here. Do not remove this comment.
    	System.out.println("Line read: " + line) ;
     
    	System.out.println("i/i + x/x = " + (i/i + x/x)) ;
        }
    }




    When it's run the windows will shows line 73 error- -"
    Please help...
    Last edited by KevinWorkman; October 11th, 2011 at 07:14 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: My code has error when its run....I dont understand what's wrong of it.

    I've added highlight tags for you, please remember them in the future.

    That being said, you still haven't provided enough information. What is the error you're getting? Is it a compile-time error or a run time error? Please copy and paste the full stack trace or error message here.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code has error when its run....I dont understand what's wrong of it.

    There we go~'

    java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at InputTester.main(InputTester.java:41)

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: My code has error when its run....I dont understand what's wrong of it.

    Your scanner only has one line of text. After you call nextLine(), you don't have anything else to read. Then you call the next() method, and you get an Exception because there is no next to read.

    Check out the Scanner API for more information.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 4
    Last Post: July 25th, 2011, 06:12 PM
  2. Can't seem to understand whats wrong with my code! Help!!
    By aquir in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2011, 12:06 PM
  3. I dont understand why this happens....
    By ashenwolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 09:31 PM
  4. My code have a simple error but i dont know how to solve it !!!
    By Blackhawk1993 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 17th, 2011, 01:12 PM
  5. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 08:31 PM