Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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_File_Token - Fatal编程技术网

Java 从字符串中获取值

Java 从字符串中获取值,java,string,file,token,Java,String,File,Token,我正在制作一个简单的画图程序,我一直在努力获取字符串的某个部分。 问题是,当我保存9面板图像时,它将每个面板的RBG值存储到一个.txt文件中。例如: java.awt.Color[r=0,g=0,b=0] java.awt.Color[r=255,g=255,b=255] java.awt.Color[r=255,g=0,b=0] java.awt.Color[r=0,g=0,b=255] java.awt.Color[r=0,g=0,b=0] java.awt.Color[r=255,g=2

我正在制作一个简单的画图程序,我一直在努力获取字符串的某个部分。 问题是,当我保存9面板图像时,它将每个面板的RBG值存储到一个.txt文件中。例如:

  • java.awt.Color[r=0,g=0,b=0]
  • java.awt.Color[r=255,g=255,b=255]
  • java.awt.Color[r=255,g=0,b=0]
  • java.awt.Color[r=0,g=0,b=255]
  • java.awt.Color[r=0,g=0,b=0]
  • java.awt.Color[r=255,g=255,b=0]
  • java.awt.Color[r=255,g=255,b=0]
  • java.awt.Color[r=255,g=0,b=0]
  • java.awt.Color[r=0,g=0,b=255]
  • 从这里,我打电话给扫描器读取我们文件的行。我只需要找到将[]中的值提取为字符串的最佳方法。我尝试过使用标记器,但没有效果,仍然被多余的字符串卡住了。我试过操纵角色,但还是失败了。从括号中提取数据的最佳方法是什么?将单个r=xxx、b=xxx、g=xxx值存储到字符串[]会更容易吗?谢谢,以下是我目前掌握的信息来源:

    import java.awt.Color;
    import java.io.*;
    import java.lang.*;
    import java.util.*;
    //when finished, organize imports (narrow down what imports were used)
    
    public class SaveLoad {
    
    private boolean tryPassed, tryPassed2;
    
    private Formatter x;
    //final String[] rawData; will be where the rgb raws are stored
    
    private Scanner xReader;
    
    public void save(Color[] c, String s) {
        //s is the filename
        int counter = c.length;
    
        //Tries to create a file and, if it does, adds the data to it.
        try{
            x = new Formatter(s+".txt");
            tryPassed = true;
            while(counter>0) {
                x.format("%s. %s\n", (c.length-(counter-1)), c[counter-1]);
                counter--;
            }
            x.close();
        }catch (Exception e){
            e.printStackTrace();
            tryPassed = false;
        }
    }
    
    //load will take paramaters of a filename(string); NOTE:::: make the file loaded specify an appendix (ex] .pixmap)
    //MAYBE add a load interface with a jDropdownmenu for the filetype? add parameter String filetype.
    public void load(String s, String filetype) {
        //loads the file and, if successful, attempts to read it.
        try{
            xReader = new Scanner(new File(s+filetype));
            tryPassed2 = true;
        }catch(Exception e){
            e.printStackTrace();
            tryPassed2 = false;
            System.out.println(s+filetype+" is not a valid file");
        }
        while(xReader.hasNext()&&tryPassed2==true) {
            String inBrackets = xReader.next().substring(17);
            System.out.println(inBrackets);
        }
    }
    }
    

    另外,请忽略我混乱的符号。

    最好的方法是更改存储格式。至少有两种选择:

    • 逗号分隔值。在每行存储
      r、g、b
      。例如
      21522213
      。然后您可以使用
      line.split(“,”
      )来获得值的
      字符串[]
    • 使用
      ObjectOutputStream

      • 我建议更改格式。但如果您坚持使用一次正则表达式:

            String st = "java.awt.Color[r=0,g=0,b=0]";
            Pattern p = Pattern.compile("java.awt.Color\\[r=(.*),g=(.*),b=(.*)\\]");
        
            Matcher m = p.matcher(st);
        
            if (m.matches()) {
                System.out.println("r=" + m.group(1));
                System.out.println("g=" + m.group(2));
                System.out.println("b=" + m.group(3));
            }
        

        仅供参考,你漏掉了最重要的标签:。不好意思,在你发布之前一分钟左右修复了它:S谢谢,实际上,你的修复与我的意思相反。不要在标题中添加“(Java)”,只需在问题底部添加标记即可。我已经把它们都修好了。你考虑过用Java来保存状态吗<代码>颜色是可序列化的@Jeffrey Hm,我来试一试。谢谢