简体   繁体   English

我的Java代码错误?

[英]Error in my Java code?

I am trying to complete a project where a user inputs a String and then my program displays that String reversed . 我正在尝试完成一个项目,用户在该项目中输入String ,然后程序显示String 相反 I was able to do this but I need to do it with message and input boxes which is giving me trouble. 我能够做到这一点,但是我需要使用消息和输入框来做到这一点,这给我带来了麻烦。 My code has an error on line 12 . 我的代码在第12行有错误。 It says: 它说:

Error: The method showMessageDialog(java.awt.Component, java.lang.Object) in the type javax.swing.JOptionPane is not applicable for the arguments (java.lang.String)

Here's the relevant piece of code: 这是相关的代码段:

import java.lang.StringBuilder;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class reverse
{
   public static void main(String[] args)
   {
     Scanner input = new Scanner(System.in);
     String string;
     JOptionPane.showInputDialog("Please input a string");
     string = input.nextLine();
    JOptionPane.showMessageDialog(new StringBuilder(string).reverse().toString());
   }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

The error explains the problem, you are passing the wrong type of parameter to showMessageDialog. 该错误说明了问题,您将错误的参数类型传递给showMessageDialog。 A quick google search got me this page: http://alvinalexander.com/java/joptionpane-showmessagedialog-examples-1 Which indicates you need to create a 'frame' for the message dialog and pass that as the first parameter. 快速的Google搜索使我获得此页面: http : //alvinalexander.com/java/joptionpane-showmessagedialog-examples-1表示您需要为消息对话框创建一个“框架”并将其作为第一个参数传递。

What you have to do is, use the JOptionPane as the source to get input from user, so you don't need the Scanner. 您要做的是,使用JOptionPane作为源来从用户那里获取输入,因此您不需要Scanner。 Once you store the string from user then you can instantiate a StringBuilder with that. 一旦存储了用户的字符串,就可以使用该实例化StringBuilder。

When using JOptionPane.showMessageDialog you have to pass null as first arg that is for component. 使用JOptionPane.showMessageDialog时,必须将null作为组件的第一个arg传递。

String string = JOptionPane.showInputDialog("Please input a string");
StringBuilder sb = new StringBuilder(string); 
JOptionPane.showMessageDialog(null, sb.reverse().toString());

A more complete and validation proof solution would look as follow: 更加完整和可验证的解决方案如下所示:

 public static void main(String... args) {
        String string = JOptionPane.showInputDialog("Input a String to reverse it");
        if (string != null && string.length() > 0) {
            StringBuilder reversedString = new StringBuilder(string);
            JOptionPane.showMessageDialog(null, reversedString.reverse().toString());
        } else {
            //validation in case Cancel/Close button or empty input provided
            if (string == null) {
                JOptionPane.showMessageDialog(null, "No input provided.");
            } else {
                JOptionPane.showMessageDialog(null, "empty input provided.");
            }
        }
    }

To reverse a String without using StringBuilder you can make use of a for loop to do so. 要在不使用StringBuilder情况下反转String ,可以使用for loop来这样做。 You start looping on original string one character at a time but, you start from end and come to the beginning. 您开始一次在原始字符串上循环一个字符,但从末尾开始一直到头。 Inside the loop, one character at a time you create reverse of the original string. 在循环内部,您一次创建一个与原始字符串相反的字符。 See below: 见下文:

String originalString= JOptionPane.showInputDialog("Please input a string");
String reversed = "";   

//loop from end of string to the beginning
for(int i = originalString.length()-1; i >= 0; i--) {
    /*
     * Use the previous value of reversed, add to it new character
     * this addition causes a new string to be created internally since
     * String is immutable
     */
    reversed += originalString.charAt(i);
}
JOptionPane.showMessageDialog(null, reversed);

//This should work: //这应该工作:

import java.lang.StringBuilder;
import javax.swing.JOptionPane; 

public class reverse 
{
   public static void main(String[] args)
   {
     String string = JOptionPane.showInputDialog("Please input a string");

     JOptionPane.showMessageDialog(null, new StringBuilder(string).reverse().toString());

   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM