I have different .txt files, which should be read by Java. Then java should give me the first number of first line or if I say differently, the first number of 4th word.
For instance:
"AAAAAA B Version 5.0.1" or "AAAAAA B Version 6.0.2"
5 or 6 should be the resulting number.
I've tried some methods such as bufferedReader with line.charAt but I don't know how I can make it work for my problem. What would you guys suggest ?
BufferedReader br = null;
br = new BufferedReader(new FileReader(new File("C:\\Users\\Desktop\\new2.txt")));
String line = null;
while((line = br.readLine()) != null) {
String[] parts = line.startsWith(...?.);
System.out.println("");
First of all just read First line of the file.
File f=new File("your_file.txt");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String firstLine=br.readLine();
Then split the line by spaces to get String array of words
String[] words=firstLine.split("\\s+");
Split the fourth word using " . "
String[] nos=words[3].split("\\.");
System.out.println(nos[0]);//Expected output, First number of fourth
//word in first line of given file
If you know the fourth word in each line is supposed to be the version number just grab that word and get the first character. If its not necessarily the fourth word try using indexOf() on the line and then get the character before that, that is if you know for sure each line is set up this way.
I meant to say if each line is known to have the format you showed above you should be able to use IndexOf() on the decimal.
AAAAAA B Version 5(.)0.1
Or use split() to split white space and get each word
parts = line.split(" ");
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(Your string here);
if(m.find()) {
System.out.println(m.group());
}
It tries to find a negative or positive integer and prints it if it can find any.
Well, first of all, make sure that you post your full code. It's much easier to diagnose the problem when you actually have the information along-side it.
Secondly, what you're going to want is a scanner that's declared to read this .txt file.
A regular scanner is delcared like this:
Scanner sc = new Scanner(System.in);
This will read the system imput for your own file. however, you could change system.in to your file if you declare the file as a variable.
Related
I want to split the file in the picture and i can't because of the lines...The output i get is all together.What is the right command in regex to split even if there is a new line ?
You are reading the file using a Scanner to get each line.
while(sc.hasNextLine())
You append the StringBuffer with each line (without a separators).
sb.append(sc.nextLine())
So at that point, a file with :
foo
bar
Would give a StringBuffer with
sb.toString(); //foobar
So there is not line separator left... Instead, do that with the Scanner.
while(sc.hasNextLine()){
String s = sc.nextLine();
System.out.println(s):
sb.append(s);
}
Last thing, please close every Scanner you open with sc.close() when you are done with them (that's a good practice to have).
EDIT:
If you want to keep that logic, simply append with a line separator after each lines:
while(sc.hasNextLine()){
sb.append(sc.nextLine()).append("\n");
}
That way, you will always have a line separator and the split will work
Quick example of what this would look like :
StringBuffer sb = new StringBuffer();
sb.append("foo").append("\n")
.append("bar").append("\n"); //fake "file reading"
String[] result = sb.toString().split("\n");
for(String s : result){
System.out.println(s);
}
foo
bar
This should do:
protasi[] = string.split("\\r?\\n");
split("\n") won't work because you need to provide a back-slash for \n which becomes \\n. I'd also added \\r which is what we should use for newline/carriage return.
I'm currently working on a SIC assembler and scanning lines from the following file:
begin START 0
main LDX zero
copy LDCH str1, x
STCH str2, x
TIX eleven
JLT copy
str1 BYTE C'TEST STRING'
str2 RESB 11
zero WORD 0
eleven WORD 11
END main
I'm using, as you might have already guessed, a regex to extract the fields from each line of code. Right now, I'm just testing if the lines match the regex (as they're supposed to). If they do, the program prints them. The problem is, it just recognizes the first line, and ignores the rest (i. e. from the second line on, they do not match the regex).
Here's the code so far:
public static void main(String args[]) throws FileNotFoundException {
Scanner scan = new Scanner(new File("/home/daniel/test.asm"));
Pattern std = Pattern.compile("(^$|[a-z0-9\\-\\_]*)(\\s+)([A-Z]+)(\\s+)([a-z0-9\\-\\_]*)");
String lineFromFile;
lineFromFile = scan.nextLine();
Matcher standard = std.matcher(lineFromFile);
while (standard.find()) {
System.out.println(lineFromFile);
lineFromFile = scan.nextLine();
}
}
It prints just the first line:
begin START 0
The weird thing comes here: if I copy the second line directly from the file, and declare a String object with it, and test it manually, it does work! And the same with the rest of the other lines. Something like:
public static void main(String args[]) throws FileNotFoundException {
Scanner scan = new Scanner(new File("/home/daniel/test.asm"));
Pattern std = Pattern.compile("(^$|[a-z0-9\\-\\_]*)(\\s+)([A-Z]+)(\\s+)([a-z0-9\\-\\_]*)");
String lineFromFile;
lineFromFile = "main LDX zero";
Matcher standard = std.matcher(lineFromFile);
if (standard.find())
System.out.println(lineFromFile);
}
And it does prints it!
main LDX zero
I don't know if it has something to do with the regex, or the file. I'd really appreciate if any of you guys help me to find the error.
Thanks for your time! :)
NOTE :- I am assuming your regex is correct
You need to update the Matcher object for every line you read from input. (For demonstration, I have just updated your code to read line by line from console and not file.)
Java Code
String pattern = "(^$|[a-z0-9\\-\\_]*)(\\s+)([A-Z]+)(\\s+)([a-z0-9\\-\\_]*)";
Pattern r = Pattern.compile(pattern);
String line = "";
Matcher m;
while((line = tmp.nextLine()) != null) {
m = r.matcher(line);
while(m.find()) {
System.out.println(m.group(1) + m.group(2)+ m.group(3)+ m.group(4)+ m.group(5));
}
}
Ideone Demo
Though, use of if will be sufficient here until there are multiple matches on single line
if(m.find()) {
System.out.println(m.group(1) + m.group(2)+ m.group(3)+ m.group(4)+ m.group(5));
}
EDIT
Assuming only three part in your input, you can use this regex instead
^((?:\w+)?\s+)(\w+\s+)(.*)$
Regex Demo
Your regex does appear to be incorrect, but that's not your immediate problem. Your while loop has to iterate through all lines, not just the ones that match. If you're using a Scanner, the test condition is the hasNextLine() method. You do the matching inside the loop. You can still create the Matcher ahead of time and apply it to each line using the reset() method:
Scanner sc = new Scanner(new File("test.asm"));
Pattern p = Pattern.compile("^([a-z0-9_-]*)\\s+([A-Z]+)\\s+(.*)");
Matcher m = p.matcher("");
while (sc.hasNextLine()) {
String lineFromFile = sc.nextLine();
if (m.reset(lineFromFile).find()) {
System.out.printf("%-8s %-6s %s%n", m.group(1), m.group(2), m.group(3));
}
}
As for your regex, the last part seemed to be too restrictive--it doesn't match your sample data, anyway. I changed it to consume everything after the second whitespace gap. I also simplified the first part and got rid of the unnecessary groups.
I am trying to read a CSV file value by value using Scanner.useDelimiter(";").
However Scanner.nextLine() still returns the whole line instead of a single Value.
The CSV-file looks like this:
0.00034;0.1;0.3;0.6;1,00E-13
My code:
Scanner iStream = new Scanner(new InputStreamReader(new FileInputStream(file.cvs);
iStream.useDelimiter(";");
String[] test = new String[5];
for (int i = 0; i < 5; i++) {
test[i] = iStream.nextLine();
}
Result:
"0.00034;0.1;0.3;0.6;1,00E-13"
Expected Result:
"0.00034", "0.1", "0.3", "0.6", "1,00E-13"
Is this possible, or should I use String.split()?
Am I missing something?
Apart from the fact that this problem is ready-made for a parsing library such as OpenCSV, nextLine doesnt account for delimiter patterns. Use next instead
test[i] = iStream.next();
From the Java Scanner documentation:
public String next()
Finds and returns the next complete token from this scanner.
A complete token is preceded and followed by input that matches the delimiter pattern.
This literally answers your question. However, I am not sure about next's behaviour at the start and end becuase it has to be "preceded and followed" by the delimiter. Maybe someone can fill in on this?
You could add extra characters to your delimiter, like \netc.
I have a String from which I would like to parse an integer and cannot find a way past this runtime exception. I understand that it is meant to display at times when a parseNUMBERTYPE function is applied to an inappropriately defined String, and that blank spaces or letters where the code expects numbers to be can trigger it. However, the String I am using as a test dummy is as far as I can tell simply the numeral 5. I have seen several suggestions in response to other users' NumberFormatException problems advocating the application of a trim() function before parsing, and I have tried this with no success.
I have also tried replacing the String I wish to parse with the simple, unstored value "5". This is the same as what the program seems to report as the relevant variable's stored String value, but while parsing that variable fails with this post's eponymous exception, the unstored value appears to run perfectly well in its place.
Note that the String variable is read by a File Scanner. I must suppose my problem has something to do with unknown, unwanted, 'invisible' characters that are being read in addition to the number five, but I cannot determine why this is happening or how to stop it. The file is formatted as .txt
Here is my code:
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name:");
String filename = scan.nextLine();
File tempfile = new File(filename);
Scanner filereader = new Scanner(tempfile);
//this is meant to assign to the String line1 the numerical value (5)
//of the file's first line
String line1 = filereader.nextLine();
//this was added later to determine if line1 held the value I expect
//it outputs the single digit 5, as expected and appropriate
System.out.println(line1);
//this was added to test how flawed my system was
//it runs fine, indicating to me that the problem may in my reader
int num2 = Integer.parseInt("5");
//this is where the exception is cast
int num1 = Integer.parseInt(line1);
I am presented with these errors:
Exception in thread "main" java.lang.NumberFormatException: For input string: "5"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at Driver.main(Driver.java:26)
Your assistance is appreciated.
In response to the suggestions given so far, the code has been modified to appear as follows:
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name:");
String filename=scan.nextLine();
File tempfile = new File(filename);
Scanner filereader = new Scanner(tempfile);
//this is meant to assign the String line1 the numerical value (5)
//of the file's first line
String line1 = filereader.nextLine();
//this was added after to determine if line1 held the value I expect
//it outputs the single digit 5, as expected and appropriate
System.out.println("["+line1+"]");
//this was added to test how flawed my system was
//it runs fine, indicating to me that the problem is in my reader
int num2 = Integer.parseInt("5");
//this is where the exception is cast
int num1 = Integer.parseInt(line1.trim());
Please correct me if I have misinterpreted your guidance in some way. As it stands, the problem persists, with an identical error report.
Looking at the message in the exception, it shows that there are no additional visible characters or whitespace in the string since the 5 is surrounded by quotes (and a quick check of the Java source code shows that the message printed here does not appear to be modified to remove whitespace before surrounding the string with quotes).
This means that either there are hidden non-printing characters in your string, or the 5 itself is actually not a 5 at all, and is instead some unicode character from another language that resembles a 5.
Simple debug case to sort this out would be to print the length of your string, as this will quickly sort out whether there are additional hidden characters in it.
System.out.println("String: [" + line1 + "] size: " + line1.size());
After that, a regex can be used to get the first consecutive set of digits, if that is the desired behaviour:
Pattern pattern = Pattern.compile("(\\d+)");
Matcher matcher = pattern.matcher(line1);
if (matcher.find())
{
String digits = matcher.group();
int num = Integer.parseInt(digits);
}
Alternatively, if it is possible or desired to remove characters in between digits then a replaceAll can be used:
String digits = line1.replaceAll("[^0-9]","");
For the string "5 6", the first method will give you 5, and the second will give you 56.
So I take in a line from a .txt file and turn it into a string. I would like to split the string up by |, but I also have spaces before and after it that is messing with the code, here is what I have so far:
File file = new File(fileLocation);
Scanner sc = new Scanner(file);
String line;
String[] words;
while(sc.hasNext()){
line = sc.next();
words = line.split("\\|");
this.german.add(words[0]);
this.english.add(words[1]);
}
An example line would be something like: in blue|in blau
I would also like to keep the spaces.
The .txt file would be:
in Rot|in red
in Blau|in blue
in Grun|in green
in Gelb|in Yellow
It would add all the items on the left of the | to the german list, and all of the ones on the right to the english list.
Ah, figured it out, the sc.next() is the next String, not the next line, I replaced it with sc.nextLine() and everything worked, thanks.
Call
line.replaceAll(" ", "");
beforehand; this will get rid of all the spaces. If you only want leading and trailing spaces from the split strings removed, use
words[i].trim()
instead.
Use the following pattern:
words = line.split("\\s+\\|\\s+");