mardi 26 février 2019

How can I check if a string has +, -, or . (decimal) in the first character?

I am writing a program that will determine if a double literal is 4 characters, and print it out on the screen. I believe I did the part right where I will check to see if there are exactly 4 characters. I am stuck on how to check if a +, - or . is the first character. With my str.charAt(0) == "+" || "-" || "." I am receiving an Incompatible operand error.

public class Program4 {

    public static void main(String[] args) {



    Scanner stdIn = new Scanner(System.in);

    String str;

    System.out.println("Please enter a valid (4 character) double literal consisting of these numbers and symbols: '+', '-', '.', (decimal point), and '0' through '9'");

    str = stdIn.nextLine();
    int length = str.length();

    // the next if statement will determine if there are exactly 4 characters \\
    if ( length == 4 ) { 

        // the next if statement checks for a +, -, or . (decimal) in the first character \\
        if ( str.charAt(0) == "+" || "-" || ".") {

        }
    }


    else {  

        System.out.println ("Please restart the program and enter in a valid 4 character double literal.");

    }

    }
}

Aucun commentaire:

Enregistrer un commentaire