0

I am creating a Java console program that reads an input value consisting of letters and numbers and that result has to be carried over to the next input line.

However, whenever I enter numeral and alphabetical the program just returns an error code. Below is an outline of the code I've written. Where the int plate code is, I feel like I need to input something different as I would like the code to register letters and numbers similar to AAA111 and return them to the next line of code.

Any help that can be provided on the problem and the coding layout itself (I'm still very new to all this) will be massively appreciated;

public static void main(String []args)
{
   {
      System.out.printf("Welcome to the input System\n\n");

      Scanner inputObject = new Scanner(System.in);

      int plate;
      int hours;

      System.out.printf("Enter the licence plate number of car 1 ==>");
      plate = inputObject.nextInt();
      System.out.printf("Enter the hours car: " +plate+ " was parked==>");
      hours = inputObject.nextInt();
   }
}
Zong
  • 6,160
  • 5
  • 32
  • 46
J. Bay
  • 19
  • 1
  • 8

2 Answers2

0

If you use next() instead of nextInt(), you'll get a String. So change your plate variable type for a String and you'll be able to use plate = inputObject.next()

geffrak
  • 38
  • 6
0

First, you want to store a alphanumeric value in the plate variable. Therefore it should be a String, not an int.

Then, to read a string(complete line of input in the console) you need to call scanner.next().

scanner.nextInt() will only read integers, while you are entering an alpha numeric value.

public static void main(String []args)
    {
    System.out.printf("Welcome to the input System\n\n");
    Scanner inputObject = new Scanner(System.in);

    String plate;
    int hours;
    System.out.printf("Enter the licence plate number of car 1 ==>");
    plate = inputObject.next();
    System.out.printf("Enter the hours car: " +plate+ " was parked==>");
    hours = inputObject.nextInt();
}
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
  • Thank you so much Imesha!! that worked perfectly. – J. Bay Apr 21 '16 at 03:16
  • Another quick question regarding the same code. If i wanted to loop this sequence another 2 times but changing the values (example car 1 to car 2 ect.) where would i have to place the loop function for this to work? – J. Bay Apr 21 '16 at 03:42
  • just cover the code containing reading input in the loop. that is, last 4 lines – Imesha Sudasingha Apr 21 '16 at 04:09