Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 子字符串可以';我找不到它应该找到的东西_Java_String_Substring - Fatal编程技术网

Java 子字符串可以';我找不到它应该找到的东西

Java 子字符串可以';我找不到它应该找到的东西,java,string,substring,Java,String,Substring,我有一个字符串(replacedLigning),其中包含y=3-2+4x-6。我希望xVærdi(一个数组列表)包含x值。在示例y=3-2+4x-6中,我希望xVærdi存储“4”,因为这就是有多少个x'e。xVærdi的初始值为none,问题是子字符串无法找到“x”,因此xVærdi将不包含任何元素。我想大概是这样 i被初始化为1 while(isNumber(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replac

我有一个字符串(replacedLigning),其中包含y=3-2+4x-6。我希望xVærdi(一个数组列表)包含x值。在示例y=3-2+4x-6中,我希望xVærdi存储“4”,因为这就是有多少个x'e。xVærdi的初始值为none,问题是子字符串无法找到“x”,因此xVærdi将不包含任何元素。我想大概是这样

i被初始化为1

while(isNumber(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1))) == true){
    xVærdi.add(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1)));
    i++;
}

isNumber方法:

public static boolean isNumber(String str)  {
        if((str.equals("-") && fortegn == false)){ 
            fortegn = true; //fortegn is a variable that allows the number to have it's minus with it
            return true;
        }
            else if (str.equals("+") || str.equals("*") || str.equals("/") || str.equals("=")){ 
                    return false;
        } else if((str.equals(".") && !fortegn) || (str.equals("0") && !fortegn) || (str.equals("1") && !fortegn) || (str.equals("2") && !fortegn) || (str.equals("3") && !fortegn) || (str.equals("4") && !fortegn) ||
                (str.equals("5") && !fortegn) || (str.equals("6") && !fortegn) || (str.equals("7") && !fortegn) || (str.equals("8") && !fortegn) || (str.equals("9") && !fortegn)){
            return true;
        } else 
            return false;
    }
编辑: 我已经做了一个小程序,你可以在下面看到。然而,这个错误在这个例子中没有发生,所以我不知道为什么会发生。所以我上传了我程序的大部分内容。我的输出是4,这是预期的,并且发生了

import java.util.*;
import java.lang.*;

public class Eksempel {

private static boolean fortegn = false;
private static int  i = 1;
static ArrayList xVærdi = new ArrayList();
private static String ligning = "y=3-2+4x-6";

    public static boolean erTal(String str)  {
        if((str.equals("-") && fortegn == false)){ //Variablen 'fortegn' sørger for at inkludere fortegnet af et tal, selvom det ikke er et tal. Dette sker kun en gang. 
            fortegn = true;
            return true;
        }
            else if (str.equals("+") || str.equals("*") || str.equals("/") || str.equals("=")){ 
                    return false;
        } else if((str.equals(".") && !fortegn) || (str.equals("0") && !fortegn) || (str.equals("1") && !fortegn) || (str.equals("2") && !fortegn) || (str.equals("3") && !fortegn) || (str.equals("4") && !fortegn) ||
                (str.equals("5") && !fortegn) || (str.equals("6") && !fortegn) || (str.equals("7") && !fortegn) || (str.equals("8") && !fortegn) || (str.equals("9") && !fortegn)){
            return true;
        } else //Kommer kun så langt hvis klienten har indtastet et bogstav andet end x. 
            return false;
    }
public static void main(String[] args) {

    while(erTal(ligning.substring((ligning.indexOf("x")-i),(ligning.indexOf("x")-i+1))) == true){
                        xVærdi.add(ligning.substring((ligning.indexOf("x")-i),(ligning.indexOf("x")-i+1)));
                        i++;
                    }

    Collections.reverse(xVærdi);
    double xResult = Double.parseDouble(String.join("", xVærdi));
    System.out.println(xResult); //returns 4.0 which it should
}}
无法工作的较长代码:

import java.util.*;
import java.lang.*;

public class Server{
private static String ligning = y=3x^3-2x^2+4x-6;
private static String replacedLigning;
private static int i = 1;
static ArrayList xVærdi = new ArrayList();
static ArrayList x2Værdi = new ArrayList();
static ArrayList x3Værdi = new ArrayList();

while(erTal(ligning.substring((ligning.indexOf("x^3")-i),
(ligning.indexOf("x^3")-i+1))) == true){

x3Værdi.add(ligning.substring((ligning.indexOf("x^3")-i),
(ligning.indexOf("x^3")-i+1)));
                        i++;
                    }

                    replacedLigning = ligning.replace("x^3","");
                    while(erTal(replacedLigning.substring((replacedLigning.indexOf("x^2")-i),(replacedLigning.indexOf("x^2")-i+1))) == true){
                        x2Værdi.add(replacedLigning.substring((replacedLigning.indexOf("x^2")-i),(replacedLigning.indexOf("x^2")-i+1)));
                        i++;
                    }

                   System.out.println(replacedLigning); //gives: y=3-2x^2+4x-6
                    replacedLigning = replacedLigning.replace("x^2","");

                    System.out.println(replacedLigning); //gives y=3-2+4x-6

                    i=1;
                    while(erTal(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1))) == true){
                        xVærdi.add(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1)));
                        i++;
                    }

                    Collections.reverse(xVærdi);
                    Collections.reverse(x2Værdi);
                    Collections.reverse(x3Værdi);

                    System.out.println(xVærdi); **//Contains nothing: [] should contain [4]**
                    System.out.println(x2Værdi); **//Contains nothing: [] should contain [-2]**
                    System.out.println(x3Værdi); **//Contains [4, ., 6, -] should contain [3]**

                    double xResult = Double.parseDouble(String.join("", xVærdi)); //gets an error, because the arraylist is empty. 
                    double x2Result = Double.parseDouble(String.join("", x2Værdi));
                    double x3Result = Double.parseDouble(String.join("", x3Værdi));

这里没有算命的人。您希望代码做什么?预期的结果是什么?i的初始值是多少?子字符串找不到它应该找到的内容是什么?很抱歉忘记通知您。我希望xVærdi包含x值。在示例
y=3-2+4x-6
中,我希望xVærdi存储“4”,因为这就是有多少个x'e。xVærdi的初始值是none,问题是,子字符串无法找到“x”,因此xVærdi将不包含任何元素。isNumber方法并没有真正做到您认为它所做的事情。您不检查字符串的单个字符,而是检查整个字符串。因此,与其检查字符串是否以
-
开头,不如检查字符串是否是
-
str.equals(“-”
)开头。因此,对于
isNumber(“-”)
您将得到true,而对于
isNumber(“-6”)
您将得到false,现在可能是阅读正则表达式的好时机,
str.matches(“[0-9.]”)
相当于您在上一个if语句中执行的所有相等检查。较长的代码仍然是一个不完整的代码示例,但唯一的问题似乎是您没有重置
fortegn
i
import java.util.*;
import java.lang.*;

public class Server{
private static String ligning = y=3x^3-2x^2+4x-6;
private static String replacedLigning;
private static int i = 1;
static ArrayList xVærdi = new ArrayList();
static ArrayList x2Værdi = new ArrayList();
static ArrayList x3Værdi = new ArrayList();

while(erTal(ligning.substring((ligning.indexOf("x^3")-i),
(ligning.indexOf("x^3")-i+1))) == true){

x3Værdi.add(ligning.substring((ligning.indexOf("x^3")-i),
(ligning.indexOf("x^3")-i+1)));
                        i++;
                    }

                    replacedLigning = ligning.replace("x^3","");
                    while(erTal(replacedLigning.substring((replacedLigning.indexOf("x^2")-i),(replacedLigning.indexOf("x^2")-i+1))) == true){
                        x2Værdi.add(replacedLigning.substring((replacedLigning.indexOf("x^2")-i),(replacedLigning.indexOf("x^2")-i+1)));
                        i++;
                    }

                   System.out.println(replacedLigning); //gives: y=3-2x^2+4x-6
                    replacedLigning = replacedLigning.replace("x^2","");

                    System.out.println(replacedLigning); //gives y=3-2+4x-6

                    i=1;
                    while(erTal(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1))) == true){
                        xVærdi.add(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1)));
                        i++;
                    }

                    Collections.reverse(xVærdi);
                    Collections.reverse(x2Værdi);
                    Collections.reverse(x3Værdi);

                    System.out.println(xVærdi); **//Contains nothing: [] should contain [4]**
                    System.out.println(x2Værdi); **//Contains nothing: [] should contain [-2]**
                    System.out.println(x3Værdi); **//Contains [4, ., 6, -] should contain [3]**

                    double xResult = Double.parseDouble(String.join("", xVærdi)); //gets an error, because the arraylist is empty. 
                    double x2Result = Double.parseDouble(String.join("", x2Værdi));
                    double x3Result = Double.parseDouble(String.join("", x3Værdi));