简体   繁体   中英

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 . 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 . 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. 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.

What you have to do is, use the JOptionPane as the source to get input from user, so you don't need the Scanner. Once you store the string from user then you can instantiate a StringBuilder with that.

When using JOptionPane.showMessageDialog you have to pass null as first arg that is for component.

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. 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());

   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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