【问题标题】:How to add in if else statement to validate for string如何添加 if else 语句以验证字符串
【发布时间】:2015-05-01 20:40:18
【问题描述】:

我正在使用 BlueJ 来完成我的这项任务,我在做这部分问题时遇到了问题,即放置决策结构以确保没有设置我已经尝试过的无效数据。我在其中放置了 if else中的声明 setName 部分不起作用。如果我将 1 放入 setName 的 GraphicIllustrators 主 void 部分,代码将显示错误。那么我需要在下面的代码中将 if else 语句放在哪里。顺便说一句,我正在使用继承来执行此操作。所以请指教。谢谢!

主类的编码:

public class Publishing_Inc
    {
        private int ID=0;
        private String name="-Name Needed-";
        private int level=0;
        private String jobtitle="-Title Needed-";
        private String edit="-Edit Skill-";

public void calculateID(){
    int uniqueID;
    uniqueID =((int)( Math.random()*10000)+1);
    ID = uniqueID;
}
public int getID(){
    return ID;
}



 public void setName(String d) {
name = d;
}
public String getName() {
return name;
}
public void setTitle(String b){
    jobtitle=b;
}
public String getTitle() {
return jobtitle;
}

public void calculatelevel(){
    int uniquelevel;
    uniquelevel =((int)( Math.random()*3)+1);
    level = uniquelevel;
}
public int getlevel() {
return level;
}

public void setEdit(String z){
    edit=z;
}
public String getEdit() {
return edit;
}

}

子类:

public class GraphicIllustrators extends Publishing_Inc
{
    public void displayGraphInformation() {
System.out.println("ID: " + getID());
System.out.println("Name:" + getName());
System.out.println("Job Title: " + getTitle());
System.out.println("Level: " + getlevel());
System.out.println();
}

 public static void main (String args[]) {
 GraphicIllustrators graphic = new GraphicIllustrators ( );
graphic.calculateID ( );
graphic.setName (" Tim Cook" );
graphic.calculatelevel ();
graphic.setTitle ("Graphic Illustrators" );
graphic.displayGraphInformation( );
}

}

【问题讨论】:

  • 为什么用javascript标记?
  • @aaronk6 解决了这个问题。
  • 你的 if else 在哪里? “我在其中将 if else 语句放在 setName 部分不起作用。”
  • 请贴出相关代码。 graphic.setName (" Tim Cook" );没有问题。
  • graphic.setName(1); 将引发编译时错误,if 语句无法帮助您。编译器检查传递的变量/文字的类型并且它们与所需的类型不匹配,然后编译器将引发相应的错误。您可以尝试将变量强制转换为所需的类型,但在这里我看不出这样做的意义。

标签: java if-statement bluej


【解决方案1】:
 public static void main (String args[]) {
      GraphicIllustrators graphic = new GraphicIllustrators ( );
      graphic.calculateID ( );

      if (input instanceof String) {
           graphic.setName ( input ); //where input comes from an input box or query or other source.
      }
      else
      {
         //alert the user.
      }
      graphic.calculatelevel ();
      graphic.setTitle ("Graphic Illustrators" );
      graphic.displayGraphInformation( );
 }

当您向函数setName 传递一个不是String 的参数时,Java 将抛出异常。您需要将发送的对象转换为字符串。如果你想防止这种情况,你应该在发送函数setName之前检查输入。这将检查输入是否是 Object String 的实例,如果不提醒用户。

【讨论】:

  • 我不知道你从哪里得到这个 main(从 TO 不是),但你的 instanceof 是错误的。什么是输入?
  • @pL4Gu33,我不确切知道 OP 想要检查什么,但似乎他想检查发送到 setName 的输入是否是字符串,但是他使用键入的字符串作为输入,所以我组成了输入变量来模拟用户输入。 (还删除了= 是一个错字)。
  • @Tom,你是对的。试图制作一个使用用户输入的示例,删除了令人困惑的输入变量并在调用setName 的行中添加了注释
  • 唯一可行的方法是,input 的类型为 String。如果是别的东西,比如Object,那么你仍然需要cast它。你已经检查过它是否包含一个字符串,所以这样做不会造成任何伤害。
猜你喜欢
  • 1970-01-01
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
  • 2018-07-10
  • 2013-02-26
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
相关资源
最近更新 更多