My code has error when its run....I dont understand what's wrong of it.
Code java:
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...
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.
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)
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.