Scanner Java Class

Asked by Jorge Elias

Hi,
I have a Java JDK 1.6.0.13 on Windows XP and on Ubuntu 9.04, but a program that use a Scanner Java Class run correct on Windows XP and do not run correct on Ubuntu.

Above i wrote the program and the error. Can you help me ?

Thanks,

Jorge Elias

My program:
import java.util.Scanner;

public class InvoiceApp
{
 public static void main (String[] args)
 {
  Scanner sc = new Scanner (System.in); // create a new instance of Scanner class
  double subtotal, discountPercent, discountAmount, invoiceTotal;
  String message;

  // display a welcome message
  System.out.println ("Welcome to the Invoice Total Calculator");
  System.out.println ();

  // get the input from the user
  System.out.println ("Enter subtotal: ");
  subtotal = sc.nextDouble ();

  // calculate the discount amont and total
  discountPercent = .2;
  discountAmount = subtotal * discountPercent;
  invoiceTotal = subtotal - discountAmount;

  // format and display the result
  message = "Discount percent: " + discountPercent + "\n" +
          "Discount amount : " + discountAmount + "\n" +
     "Invoice total : " + invoiceTotal + "\n";
  System.out.println (message);
 }
}

The error:
>javac InvoiceApp.java
>Exit code: 0
>java InvoiceApp
Welcome to the Invoice Total Calculator

Enter subtotal:
Exception in thread "main" java.util.NoSuchElementException
 at java.util.Scanner.throwFor(Scanner.java:838)
 at java.util.Scanner.next(Scanner.java:1461)
 at java.util.Scanner.nextDouble(Scanner.java:2387)
 at InvoiceApp.main(InvoiceApp.java:17)
>Exit code: 1

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Julian Dax (julian-dax) said :
#1

Just to be sure: Have you actually typed in a double value? If you just hit enter you will get this exception as the scanner wont find a value.

Revision history for this message
Jorge Elias (jea-web) said :
#2

Julian,

Before i enter a value the error ocurred. So i think is another problem. Maybe this class in Ubuntu is different that the same in WIndows.
I think that the nextDouble method does not exist in this class in Ubuntu. I don´t know.
Is there another version of the Java Classes 1.6.0.13 release for Ubuntu ?

Revision history for this message
Sergey Fukanchik (fuxx) said :
#3

Problem may originate not only from hitting enter, but also from
pressing Ctrl-D if this is not the case - your problem is in System.in.
Have you redirected output, or launch your program from another?

Your program works for me (Ubuntu 9.04).

Can you help with this problem?

Provide an answer of your own, or ask Jorge Elias for more information if necessary.

To post a message you must log in.