我有一个字符串,我在我的应用程序中加载,它从数字变为字母等.我有一个简单的if声明,看它是否包含字母或数字,但有些东西不能正常工作.这是一个片段.
String text = "abc";
String number;
if (text.contains("[a-zA-Z]+") == false && text.length() > 2) {
number = text;
}
Run Code Online (Sandbox Code Playgroud)
虽然text变量确实包含字母,但条件返回为true.的和&&应作为EVAL两个条件不必是true为了处理number = text;
==============================
解:
我能够通过使用此问题的评论提供的以下代码来解决这个问题.所有其他帖子也有效!
我使用的工作来自第一条评论.虽然提供的所有示例代码似乎也是有效的!
String text = "abc";
String number;
if (Pattern.matches("[a-zA-Z]+", text) == false && text.length() > 2) {
number = text;
}
Run Code Online (Sandbox Code Playgroud) 我知道我们应该在我们的问题中添加一些代码,但我真的很傻眼,不能包裹我的头脑或找到任何可以遵循的例子.
基本上我想打开文件C:\ A.txt,其中已有内容,并在结尾处写一个字符串.基本上是这样的.
文件A.txt包含:
John
Bob
Larry
Run Code Online (Sandbox Code Playgroud)
我想打开它并在结尾写Sue所以文件现在包含:
John
Bob
Larry
Sue
Run Code Online (Sandbox Code Playgroud)
很抱歉没有代码示例,今天早上我的大脑已经死了......
这是一个代码片段,但基本上我想要做的是从名为'listings.txt'的文件中读取并写入名为'overview.txt'的文件.我想从'listings.txt'中取出信息并将它们按原样放入'overview.txt'中(稍后我将弄清楚其余部分).
创建文件'overview.txt'并显示循环遍历文件'listings.txt'并写入'overview.txt'.但是,一旦我打开文件'overview.txt',它就是空的.
有人可以通过快速浏览我的代码并发现错误的东西吗?
package yesOverview;
import java.io.BufferedReader;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class yesOverview {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String strInput = "foo.bar";
System.out.print("Please enter the listings file (the full path to the file): ");
strInput = input.next();
//This makes sure that the inputed file is listings.txt as required for KET1 task 2
while (strInput.contains("listings.txt") == false) {
System.out.print("Incorrect file. Please enter listings …Run Code Online (Sandbox Code Playgroud)