-1
/**
 * 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

    }
}

**Hello. I just started learning how to code Java this week in my high school computer science class. I'm trying to convert a string to a double and an integer. The two variables I'm trying to convert are wholeNumberFriends (integer) and numberMiles (double). I have created a new variable for each of them so they can easily convert to a double and an integer. The error I keep getting for this conversion is, incompatible types: double cannot be converted to java.lang.String . Thank you. **

catpat4
  • 19
  • 1
  • 6

2 Answers2

1

String to Integer

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

String to Double

double num = Double.parseDouble("1.2");
faris
  • 692
  • 4
  • 18
0

You can use Integer.valueOf(java.lang.String)

       String userInput = "2";

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

The same valueOf method is available in Double, Float, Long etc..

The valueOf method will return you an Object instead of primitive.

Jobin
  • 5,610
  • 5
  • 38
  • 53