如何将字符串转换为类型整数并从用户输入中键入double? [重复]

问题描述 投票:-1回答:2

这个问题在这里已有答案:

/**
 * MadLib.java  
 *
 * @author: Jackie Hirsch
 * Assignment: Madlib
 * 
 * Brief Program Description: This program has will read a madlib with 
the inputs that the user gives the computer. 
 * 
 *
 */
import javax.swing.JOptionPane; //import of JOptionPane
public class MadLib
{
    public static void main (String[] args)
    {
        String cheeseType; //Cheese Character
        String interjection; //interjection
        String treeType; //tree type
        String wholeNumberFriends; // number for number of friends on 
        //line 27
        String numberMiles; //number of miles
        int wholeNumberFriendsConverted; // number for number of 
        //friends on line 27 converted
        double numberMilesConverted; //number of miles

        //ask user for variable string cheese type
        cheeseType = JOptionPane.showInputDialog ("Enter a type of 
        cheese");
        //ask user for varaible string interjection
        interjection = JOptionPane.showInputDialog ("Enter an 
        interjection");
        //ask user for variable string tree type
        treeType = JOptionPane.showInputDialog ("Enter a type of 
        tree");
        //ask user for variable integer number for number of friends
        wholeNumberFriends = JOptionPane.showInputDialog ("Enter a 
        whole number");
        //ask user for variable double number for number of miles
        numberMiles = JOptionPane.showInputDialog ("Enter a decimal or 
        whole number");

        //string converted
        wholeNumberFriends = Integer.parseInt 
        (wholeNumberFriendsConverted);
        numberMiles = Integer.parseInt (numberMilesConverted);




        //Madlib reading printed
        JOptionPane.showMessageDialog (null, "There once was a " + 
        cheeseType + "and this " + cheeseType  + "was super exciting. " 
        +
        "Because " + cheeseType + "was so exciting he would shout, " + 
        interjection + ". " +
        "His " + wholeNumberFriendsConverted + "friends, the " + 
        treeType + ", would sing home and the whole"  +
        "neighborhood hated them. One neighboor walked outside and 
        said, " +
        "''You annoying hooligans are crazy!!!''. They were so confused 
        that" +
        "they ran away to Neverland which was " + numberMilesConverted 
        + "miles so they never" +
        "had to grow up. Then they ran into captain hook and then Peter 
        Pan saved them.");
        System.exit (0); //ends the program

    }
}

**你好。我刚开始学习如何在我的高中计算机科学课上编写Java代码。我正在尝试将字符串转换为double和整数。我想要转换的两个变量是wholeNumberFriends(整数)和numberMiles(double)。我为每个变量创建了一个新变量,因此可以轻松转换为double和整数。我一直在为这次转换得到的错误是,不兼容的类型:double无法转换为java.lang.String。谢谢。 **

java bluej
2个回答
1
投票

字符串到整数

int num = Integer.parseInt("1");

字符串为Double

double num = Double.parseDouble("1.2");

0
投票

你可以使用Integer.valueOf(java.lang.String)

       String userInput = "2";

        try {
            Integer.valueOf(userInput);
        } catch (NumberFormatException e) {
            // Not a Number
        }

valueOfDoubleFloat等中可以使用相同的Long方法。

valueOf方法将返回一个Object而不是primitive。

© www.soinside.com 2019 - 2024. All rights reserved.