English 中文(简体)
不可兼容类型 - 找到 java. lang. String 但预期在 java 中
原标题:Incompatible types - found java.lang.String but expected int, in java

显示从今天起的100天、从你出生之日起的100天和从你出生之日起的10 000天。我已经做了所有这些,但是我想再进一步一点,我希望用户能够输入从今天起他/她希望知道的日期(如果这有意义的话...)的天数。这里的代码im工作部分看起来像:

 public class calendarProjectTest
    {
    public static void main(String[] args)
    { GregorianCalendar cal = new GregorianCalendar(); //declare today s date
    GregorianCalendar myBirthday = new
    GregorianCalendar(1990, GregorianCalendar.JUNE, 9); //declare my birthday
    System.out.println("Please enter a number greater than 0:");
    Scanner keyboard = new Scanner(System.in);
    String number = keyboard.next();
    int value = number;
    cal.add(GregorianCalendar.DAY_OF_MONTH, number);

I m using Bluej and it says, "incompatible types - found java.lang.String but expected int" I m at a loss. i don t have any idea what to do. any advise would be great. and yes, i know there are no end brackets.

最佳回答

更改此更改 :

cal.add(GregorianCalendar.DAY_OF_MONTH, Integer.parseInt(number));

docs 中可以看到, add() 方法需要两个参数作为 int

问题回答

使用此选项 :

int value = Integer.parseInt(number);

问题就在这里

String number = keyboard.next();
int value = number;

代之以使用:

int value = keyboard.nextInt(); // you will get parsed int value
cal.add(GregorianCalendar.DAY_OF_MONTH, value);

Keyboard.next(); returns a string, not an integer.

sot value = number 正在试图为 int 类型指定字符串值。 尝试将此值替换为 : < number >int value = Integer. parseInt( number ) ;

http://www.vogella.com/articles/JavaDateTimeAPI/article.html http://www.vogella. com/articles/JavaDateTimeAPI/article.html rel=“nofollow” >http://www.vogella.com/articles/JavaDateTimeAPI/article.html/a>http://www.vogella.com/articles/JavaDateTimeAPI/article.html/a>





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签