简体   繁体   中英

How to convert a string to an integer

i have an assignment saying :

  1. The character part of the course code (the first 3 characters of the course code), eg (csc), need to be changed to capital letters.

  2. You should stores the student level which is correspondence to the first digit of the course code from the right hand side, eg if the course code is CSC224, this student is at level 4.

  3. You should calculate the remaining number of terms for the student to graduate based on the current level, assuming that the last level is level 10.

i could do the first two points, but i'm stock in the third. i know that i cant do operations between a String and an Int, so i have to convert the str to an int. but that didnt work for me, i dont know why?!.

    import java.util.*;
    public class HighSpecialCourseCode
    {
    static Scanner input = new Scanner (System.in);
    public static void main (String[] args)
    {


    // declaring 
    String CourseCode;
    String Level ;

    // input
    System.out.println("Course code:");
    CourseCode=input.next().toUpperCase();
    System.out.println("\nCourse Code: "+ CourseCode);

    // output 
    Level=CourseCode.substring(CourseCode.length()-1);
    System.out.println("Student Level: "+ Level);

    int RemainingTerms = Integer.parseInt("Level");
    System.out.print("Remaining Terms: " + (10 - Level) );


    }
    } 

this is my code and when i compile it, i get :

HighSpecialCourseCode.java:25: error: bad operand types for binary operator '-'
System.out.print("Remaining Terms: "+ (10 - Level) );
                                     ^
  first type:  int
  second type: String
1 error

i already converted ( Level ) so why isn't working!!

Instead of:

int RemainingTerms = Integer.parseInt("Level");

Should be:

int RemainingTerms = Integer.parseInt(Level);

Then you need to use RemainingTerms not Level in the next line.

System.out.print("Remaining Terms: " + (10 - RemainingTerms) );

Aside

You really should follow java naming conventions and use lower camel case for variables, read more here: http://www.oracle.com/technetwork/java/codeconventions-135099.html

尝试这个:

System.out.print("Remaining Terms: " + (10 - Integer.valueOf(Level)) );
  1. Your variable Level is a String. It is always a string. You can't perform arithmetic with Strings.
  2. You created a variable named RemainingTerms that is an int. Use it in your arithmetic.

So change that line to:

System.out.print("Remaining Terms: " + (10 - RemainingTerms));

You have another error that wouldn't be reported until after you can compile and run the program:

 int RemainingTerms = Integer.parseInt("Level"); 

On this line you try to parse the literal string "Level" to an integer, but those letters do not represent a number. What you want to do is pass the string you extracted out of the course code, not the letters Lev-el.

int RemainingTerms = Integer.parseInt(Level);

尝试这个....

System.out.print("Remaining Terms: "+ (10 - Integer.parseInt(Level)) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM