How to get input content in java

 

There are many object types in Java, such as Object, StringBuilder, etc. Among them, Object and String are the most commonly used object types, and the StringBuilder class is a special class that can create other objects through inheritance. In our usual work, we often encounter situations where we need to obtain input content, so what methods can we obtain input content? We together look. 1. Enter a string in the input box 2. Click the send button 3. Return a string according to the user's operation 4. The above program uses the string class to obtain the input content. 5. An Object class is used here. This class is mainly used to receive and process Object objects, so you need to specify an object name when creating an object. 6. After clicking the send button, a string will be returned. Of course, you can also get the input content through JavaScript code. 8. The Object class is used here to obtain the content entered by the user, and the returned content is a string type.

  • 1. Enter a string in the input box

    We enter a string in the input box, such as "hello world", then the input box will output a string, which is also a simple method. However, this method has a disadvantage, it can only get the content of the string type. If the user enters date, time, currency and other types of information in the input box, then this method will report an error. Because the Java language does not support the output of these information types. 1. First, we need to create an Object class. When creating an Object, we need to specify an object named "Handler", and then assign it to the StringBuilder class. Because we want to get the content entered by the user, here we also need to create a Handler class. 3. Next we need to create a StringBuilder object, because here we need to get the content entered by the user, so we need to assign the StringBuilder object to the Object class. Because the method of receiving and processing string type data has been defined in the Object class, here we only need to call the Object. append (StringBuilder. append (String []). const String []) method. 4. Finally, we only need to assign the Handler object to the StringBuilder object. Of course, the method of the StringBuilder class can also be called here to obtain the content input by the user, but an error may be reported here. 5. In the above code, we just called the StringBuilder method, and did not directly call the Handler method, so the error is reported here because we have not called the Handler method. 6. Next, we only need to call the Handler method to get the content entered by the user. After parsing, if it is found that the string contains a number, then the number is the content entered by the user. If it does not contain a number, then the number is the content returned by the system. 8. After the system has parsed all strings, the content entered by the user will be returned to the user in the form of JavaScript code. JavaScript code needs access to Java objects to get content. If you want to get string type content, you can use JavaScript to get string type data. Both methods are implemented in the Object class.

  • 2. Click the send button

    Use the sendMessage class in the program. In sendMessage, we can obtain the corresponding information by selecting the object to be sent. A send button class is mainly used in the program. For the acquisition of objects, you can use JavaScript code to achieve it, and it is the same in Java, such as the following code: As you can see from the above code, an object text () is used in the program, which is used to obtain the characters input by the user string. The text () method is mainly used to obtain the string object entered by the user. As you can see from the above code, the program first creates an Object class, then creates a text () method in the class, and then creates an Object object in the class. This Object object can be used to receive the string input by the user, and the string input by the user can be obtained by calling the start () method in the object. Two methods are used in the program to obtain the string input by the user: The first method is to call the start () method in the Object class to obtain the string object entered by the user; for string type data, the String class can be used to obtain , if it is other types of data, you can use the Object class to obtain it. Of course, we can also use different types of data in the actual development process. Let's take a look at the effect achieved by using JavaScript code in the above program:

  • 3. According to the user's operation, return a string

    If the user needs to input some characters, such as English words, numbers, etc., then the string acquisition method can be used, such as the following code: cannot be obtained by this method. For example: We can use string to get a string. In the above code, because there are two characters, this string is a "." character. The above are the three methods of obtaining input content in Java, which are to use String, Object and StringBuilder classes to obtain input content.

The following are several commonly used Java codes for obtaining input content:
1. Use the Scanner class to obtain console input:
```
import java.util.Scanner;
public class InputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.print("Please enter an integer: ");int num = scanner.nextInt();System.out.println("The integer you entered is: " + num) ;}}```2. Use the BufferedReader class to get console input:```import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class InputExample { public static void main(String [] args) throws IOException {















BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter a string: ");
String str = reader.readLine();
System.out.println("Your input The string is: " + str);
}
}
```
3. Use the JOptionPane class to pop up a dialog box to get input:
```
import javax.swing.JOptionPane;
public class InputExample { public static void main(String[] args) { String str = JOptionPane.showInputDialog("Please enter a string: "); System.out.println("The string you entered is: " + str); } } `` ` The above three methods can be used to obtain The content entered by the user, which method to use depends on the specific application scenario.






Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130555296