How to convert from string to char in C++?

How to convert a string to a double?

  • I need to convert the value of String-avg to the double-numberGrade. Thanks a lot for your help. import javax.swing.JOptionPane; public class April3 { public static void main(String [] args) { String avg; double numberGrade; char letterGrade; while ( ! avg.equals("-1") ) { avg = JOptionPane.showInputDialog("Please enter student average");; if ( (numberGrade >= 0.0) && (numberGrade <= 100.0) ) { letterGrade = getLetter(numberGrade); JOptionPane.showMessageDialog(null… letterGrade); } } } private static char getLetter(double n) { if (n >= 80) { return ('A'); } if (n >= 60) { return ('B'); } if (n >= 40) { return ('C'); } if (n >= 20) { return ('D'); } if (n >= 10) { return ('F'); } return ('Z'); } }

  • Answer:

    You could just have getLetter return a String instead of a char, and put "A", "B" and so on instead of how they are now. Another choice is using Character.toString(), you need to import java.lang.Character for this. chars are just integer codes if I remember correctly.

Wayne at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.