Tag: 字符串

从字符串中提取日期

我有一个文件名列表大致如下:Gadget1-010912000000-020912235959.csv,即它们包含两个日期,表示其数据的时间跨度。 用户输入日期格式和文件格式: 在这种情况下的文件格式:* GADGET * – * DATE_FROM * – * DATE_TO * .csv 在这种情况下的日期格式:ddMMyyHHmmss 我想要做的是使用给定的文件和日期格式从文件名中提取三个值。 我的问题是:由于日期格式可能差别很大(小时,分钟和秒可以用冒号分隔,日期用点,…)我不太清楚如何创建一个合适的正则表达式。

Java – 如何使非String对象的JComboBox显示字符串名称?

我想让JComboBox组件显示String名称,而不是引用。 但是,我不知道是怎么做到的。 下面显示了我的代码: public class Properties extends JPanel implements ItemListener { private static final long serialVersionUID = -8555733808183623384L; private static final Dimension SIZE = new Dimension(130, 80); private JComboBox tileCategory; public Properties() { tileCategory = new JComboBox(); tileCategory.setPreferredSize(SIZE); tileCategory.addItemListener(this); this.setLayout(new GridLayout(16, 1)); loadCategory(); } private void loadCategory() { //Obtains a HashMap of Strings from somewhere […]

Java,文本中的n个单词数

此类的目的是计算用户输入的文本中每个n字符单词的出现次数。 我认为在methodsNumber方法中存在一些错误,因为我为每个n字符单词获得228。 错误在哪里? import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class TempTest { public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter the integer n”); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char[] array = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, […]

如何在Java中打印字符串数组的偶数和奇数位置字符?

题 给定长度为N的字符串S,其从0到N-1索引,在单行上打印它的偶数索引和奇数索引字符作为2个空格分隔的字符串。 假设输入从索引位置0开始(被认为是偶数) 输入 第一行包含一个整数T(测试用例的数量)。 T个后续行的每一行i包含一个String,S。 产量 对于每个字符串S,打印它的偶数索引字符,后跟空格,后跟奇数索引字符。 样本输入 2 黑客 秩 样本输出 Hce akr Rn ak 我写的守则 public static void main(String[] args) { Scanner scan = new Scanner(System.in); int T = scan.nextInt(); scan.nextLine(); for(int i=0 ; i<T ; i++) { String myString = scan.nextLine(); int evn = 0, odd = 0, len = myString.length(); […]

ANDROID:在一个textView中的多对字符串之间导航

闪存卡应用程序 我有多个静态字符串存储在strings.xml上,我想使用prev_Clicked / next_Clicked按钮在它们之间导航。 我还希望能够在应用程序运行时存储问题和答案的字符串,并通过prev / next按钮访问这些字符串 感谢您的时间,请关注我的代码: 的.java public class MainActivity extends splashActivity { Button closeButton, addButton, prevButton, nextButton; TextView QAText, answerText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flashcard); QAText = (TextView) findViewById(R.id.fcfront_textView); answerText = (TextView) findViewById(R.id.fcfront_textView); //not used anywhere atm QAText.setOnClickListener(new View.OnClickListener() { //function to toggle between Q/A public void onClick(View v) […]

连接字符串数组+分隔符

java noob here … 这让我疯了,因为我知道这很简单,但我已经在这30分钟了… 这来自于codefights: 对于arguments = [“Code”,“Fight”,“On”,“!”]和separator =“/”,输出应该是myConcat(arguments,separator)=“Code / Fight / On /!/”。 我的代码: String myConcat(String[] arguments, String separator) { for(int i = 0; i <= arguments.length; i++){ String output = arguments[0] + separator; } return output; } 错误:第5行的file.java:错误:找不到符号返回输出; ^符号:变量输出位置:类_runfniek 1错误 任何提示将非常感谢…

我在if else语句中分配字符串时遇到问题

现在我有一个编程分配来计算bmi(体重指数)并将其分配给一个分类。 根据bmi,它分配如下。 我在获取字符串的分类方面遇到了麻烦。 当我尝试将分类放在消息框中时,它告诉我我没有初始化它。 public static void main(String[] args) { //Variables double bmi; // Body Mass Index String weight; // Weight in kilograms String height; // height in meters String classification; weight = JOptionPane.showInputDialog(“Welcome to the BMI and BSA Calculator to begin enter weight in kilograms.”); double weight2 = Double.parseDouble(weight); height = JOptionPane.showInputDialog(“Enter height in […]

Java中是否存在基于8位字节数组的字符串类型?

Java(标准或其他)中的字符串类型/类是否基于8位字节类型的数组,而不是16位字符类型? 我非常喜欢C ++中的std :: string类型,因为它可以很好地解析base256(二进制)数据…… 我继续写了一个小课程,提供了我需要的大部分function(下图),但是更好的一个将不胜感激! public class stdstring extends Object { private byte [] bytedata; public stdstring() { bytedata = new byte[0]; } public stdstring(byte[] bytes) { bytedata = bytes; } public stdstring(byte[] bytes, int offset, int length) { bytedata = new byte[length]; System.arraycopy(bytes,offset,bytedata,0,length); } public stdstring(String string) throws UnsupportedEncodingException { bytedata = new […]

如何序列化ArrayList 2x并且不要过度编写已经存在的ArrayList JAVA

我不得不为我的程序创建一个管理面板来序列化更多对象,它的作用是什么,它将对象(String,String)保存到arraylist中,并且arraylist通过序列化保存到文件中。 当试图将另一个对象添加到arraylist时,程序首先将文件内容反序列化为arraylist,因为如果不这样做,我将如何访问文件中已存在的对象。 但是在添加对象3时会发生类似这样的事情 (安慰) 索引0 [[Word:word1 – 提示:提示1],Word:word2 – 提示:hint2] 怎么解决这个? 我想我应该删除旧的arraylist每次创建一个新的对象,但我不知道该怎么做,有什么我可以做的编辑文件中的旧arraylist而不是删除它。 提前致谢 enter code here class Admin{ ArrayList al = new ArrayList(); public void ifwordsarepresent(){ try { FileInputStream fis = new FileInputStream(“D:/serial.txt”); ObjectInputStream ois = new ObjectInputStream(fis); if(fis.available()>0){ try { try { al.add((ArrayList) ois.readObject()); addingwords(); ois.close(); } catch public void addingwords(){ try { try […]

检查字符串数组输入的Anagram

这是我下面的代码,用于检查给定字符串数组的字谜。 即使在最简单的情况下,只有一个输入,它总是给我假。 我不明白我没有正确地将字符串数组转换为字符串,或者我的算法是完全错误的。 public class anagram { static boolean isAnagram(String[] s1, String[] s2) { String str = s1.toString(); String str2 = s2.toString(); if (str.length() != str2.length()) return false; for (int i =0; i<str.length();i++) { for (int j = 0;j<str2.length();j++) { if (s1[i] == s2[j]) { return true; } return false; } } return true; } […]