-1

My project is very similar to the famous ATM problem. I have to create a hotel check in/check out system. I have to get the user input for the last name and confirm it. However when I try to return the string, it tells me it cant convert string to int.

import java.util.Scanner;

public class Keyboard {
   private Scanner input;
   String lastName;

   public Keyboard() {
       input = new Scanner( System.in);
   }

   public int getInput() {
       lastName = input.nextLine();
       return lastName;
   }

}
Héctor
  • 24,444
  • 35
  • 132
  • 243
hey miki
  • 13
  • 2
  • 4
    The return type on your method is an int – John Kane May 07 '18 at 17:14
  • Possible duplicate of [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – MFisherKDX May 07 '18 at 17:14
  • 1
    @MFisherKDX The name should be a `String`, no need to convert to `int` – xingbin May 07 '18 at 17:16
  • Welcome to StackOverflow. In this case, since your issue is so simple, it's OK. But for next time, please read how to post a [Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve). Your example here isn't complete since there is no main to call the getInput() method. Just something to keep in mind for next time. – Jeff Learman May 07 '18 at 17:31

2 Answers2

2

In your code, you declare getInput method will return an int by:

public int getInput()

Since the name should be a String, not an int, you need to adjust your method signature:

public String getInput() {   
    lastName = input.nextLine();    
    return lastName;    
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
2
public int getInput() {
   lastName = input.nextLine();
   return lastName;
}

Here you said, that getInput will return int, change it into

public String getInput() {
   lastName = input.nextLine();
   return lastName;
}

For more information, take a look here

namokarm
  • 668
  • 1
  • 7
  • 20