Ask Experts Questions for FREE Help !
Ask
    vjsmith's Avatar
    vjsmith Posts: 1, Reputation: 1
    New Member
     
    #1

    Nov 3, 2013, 03:30 PM
    Declaring string and char variables
    Very new to Java. Using netbeans for this class.

    Homework is to use JOptionPane to prompt a user for their first name, middle initial, and last name Code is below.

    The problem is on line 16. "incompatible types: String cannot be converted into char." I understand that String cannot be converted into char. I thought by declaring "middle" as char it would be stored as one. Need to be pointed in the right direction... everything works if "middle" is declared as a string but the instructor wants the middle initial to be a char.

    Import javax.swing.JOptionPane;

    Public class VSmithp1 { //project 1 to gather user information

    Public static void main(String[] args) {

    //declare variables
    String firstname,lastname;
    Char middle;
    Int age, lucky1, lucky2, lucky3, average;

    Firstname = JOptionPane.showInputDialog(null, "Please enter your first name:");

    Middle = JOptionPane.showInputDialog(null, "Please enter your middle initial");

    Lastname = JOptionPane.showInputDialog (null, "Please enter your last name:");

    JOptionPane.showMessageDialog(null,"Welcome " + firstname + " " + middle + " "+lastname);
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #2

    Nov 30, 2013, 06:19 AM
    Love them instructors making hoops to jump where none need exist. Middle is indeed a char as declared. The problem is you are trying to stuff a string into it. If middle must be a single char vs. an array of char use charAt() method.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Char input problems [ 1 Answers ]

Why when I try to compile , it says "variable ch is already defined in method main(java.lang.String)"? I have probkems getting char inputs. import java.util.Scanner; public class Tolak{ public static void main(String args) { int correct=0; char ch;

Char[] array insert [ 0 Answers ]

Hey I was just wondering how I would solve this, I have tried many options but none of them seem to work, I have commented in what I actually need done, would someone please be able to help me? Public class StringImproved { char content; public boolean insert(java.lang.String...

Using C++ (if, char or Boolean) [ 0 Answers ]

Using C++ (if, char or Boolean) Write a program that asks the user for a two digit number and then prints the English word for that number. Sample run: Enter a two-digit number: 45 You entered the number forty-five. PLAN: Read the number entered as an integer and then break the number up...


View more questions Search