Java scanner delimiter causes my integer to not be read properly - java

So I'm creating a scanner to read off of a simple text file:
import java.io.*;
import java.util.Scanner;
public class Weather {
public static void main(String[] args) throws FileNotFoundException {
int a;
File weatherData = new File("C:\\Users\\taddi\\eclipse-workspace\\COS_160_ASSIGNMENT_10\\src\\PortlandWeather1941to2018.txt");
Scanner scnr = new Scanner(weatherData);
scnr.useDelimiter("//");
int totalCount = scnr.nextInt();// this reads the number at the beginning and uses it so I know how many times to run the loop
String throwAway1 = scnr.nextLine();//these statement are used to throw a way the rest of line 1, and all of line 2 and 3
String throwAway2 = scnr.nextLine();
String throwAway3 = scnr.nextLine();
int[] month = new int[totalCount];
int[] day = new int[totalCount];
int[] year = new int[totalCount];
int[] tmax = new int[totalCount];
int[] tmin = new int[totalCount];
for(a = 0; a < totalCount; a ++) {
month[a] = scnr.nextInt();
System.out.println(month[a]);
day[a] = scnr.nextInt();
System.out.println(day[a]);
year[a] = scnr.nextInt();
tmax[a] = scnr.nextInt();
tmin[a] = scnr.nextInt();
}
}
}
The first part of the text file is an integer I'm trying to read. For some reason, it only reads that integer when I comment out the scnr.useDelimiter("//"); line, otherwise I get an InputMismatchException
I'd love to just get rid of all the unnecessary words and slashes in the text file but that wouldn't satisfy the assignment. What's going wrong with the delimiter? How do I read the integer?

Your delimiter is a string, and it will not work in your use case the way you want.
I assume your sample data is like this (ignoring the header lines) ...
01/01/1941 38 25
01/02/1941 32 20
... so you are looking to get each number - the date elements and the tmax/tmin values - so a single delimiter character of '/' would only break up the date.
For example:
final String data =
"01/01/1941 38 25 \n"+
"01/02/1941 32 20 \n";
Scanner scnr = new Scanner(data);
scnr.useDelimiter("/");
while(scnr.hasNext()) {
System.out.println(scnr.next());
}
scnr.close();
outputs the following ...
01
01
1941 38 25
01
02
1941 32 20
showing that it splits on the date d/m/y slashes, but the year and tmax and tmin are bundled together.
Adjusting the scanner to use a Pattern delimiter allows us to split on the slashes and the spaces.
final String data =
"01/01/1941 38 25 \n"+
"01/02/1941 32 20 \n";
Scanner scnr = new Scanner(data);
scnr.useDelimiter(Pattern.compile("[/ ]+"));
while(scnr.hasNext()) {
System.out.println(scnr.next());
}
scnr.close();
}
giving the output I think you want:
01
01
1941
38
25
01
02
1941
32
20
However, note that in my example data I have trailing whitespace on each line and they are thus also returned as empty String tokens. If I was scanning for nextInt() I would get an java.util.InputMismatchException error. Depending on the exact formatting of your input you may need to cater for that.

Related

Data evaluation from text file

I need some help. I dont know how to solve my problem.
I have text file in this format:
personx 25
personz 2
persony 5
persony 7
personi 55
personx 25
I need to count the numbers for every person. The output should be "personx = 50" etc.
I can not use my old system where I knew there is 10 people. So I had 10 variables and I just went through the file with scanner and checked if line starts with "personx" then count the number to variable personx etc. I dont want to use these variables now. I dont want to change code after every new person.
How to solve this? I want to have this output sorted from highest to lowest:
personi = 55
personx = 50
persony = 12
personz = 2
Is that possible without using variables personi, personx, persony, personz ? My idea was to go through the file and scan the names. Save the name into an array and add another name into an array if that name is not in the array yet. So I will have the names.
Next I will scan the file again and check for the name and number. Check name + number and then save the number into another array on the same possition as the name in the first array. So I will have
Names[0] = "personi";
Count[0] = 55;
Names[1] = "personx";
Count[1] = 50;
And then I will just print these two arrays with for cycle.
I dont think that it is the best solution. How would you do it? Is there something better/faster/easier ? And how to solve that Sorting?
Thank you for your time.
You can us Map<String,Integer>
In this case i used TreeMap which will sort everything for you. If you dont need sorting then just use HashMap instead of TreeMap.
Map<String, Integer> map = new TreeMap();
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("C:/iPhone/persons.txt")));
String line = "";
String [] person = new String[2];
while ((line = reader.readLine()) != null) {
person = line.split(" ");
String name = person[0];
int number = Integer.parseInt(person[1]);
map.put(name,map.getOrDefault(name,0)+number);
}
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
map.forEach((k,v)->System.out.println(k + " = " + v));
}
persons.txt:
personx 25
personz 2
persony 5
persony 7
personi 55
personx 25
Output:
personi = 55
personx = 50
persony = 12
personz = 2
1) Can I use this on file where line is not in my format but it has for example.. this format ? "personx bla bla 25" ? Is it possible to convert it too? how?
Yes you can create method which will do it for you. You can use either string splits or some regex.
2) Why is there String [] person = new String[2]; ?
Mistake, it should be String[1]. Corrected now
3) what is String line = ""; ?
It is just new String where i'm storing every line that i read from file. As you can see, im assigning reder.readLine() in while loop. After that im just splitting it.
EDIT:
Changed code so person can have multiple params but will take only first as name and last as number.
public static void main(String[] args) {
Map<String, Integer> map = new TreeMap();
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("C:/iPhone/persons.txt")));
String line = "";
String [] person;
while ((line = reader.readLine()) != null) {
person = line.split(" ");
String name = person[0];
int number = Integer.parseInt(person[person.length-1]);
map.put(name,map.getOrDefault(name,0)+number);
}
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
map.forEach((k,v)->System.out.println(k + " = " + v));
}
persons.txt:
personx asdasdas dasd asd a 25
personz 2
persony asd asd asd a 5
persony 7
personi 55
personx 25
output:
personi = 55
personx = 50
persony = 12
personz = 2

For-loop calculation of arbitrary times

I have a plain format txt file named enroll.txt which contains:
1997 2000
cs108 40 35
cs111 90 100
cs105 14 8
cs101 180 200
The first row shows the years of class
The second row first column shows the class name, and the following two columns show the number of students in the class in the years mentioned on the first row.
ex) In 1997, there were 40 students in class cs108.
My desired outcome: code prints as follows using
(i) split (ii) parseInt (iii) for-loop
student totals:
1997: 324
2000: 343
But this code should also work for any number of years (for example if I had student numbers for four years for each class instead of two, the code would still give me a similar output as below with student totals of say 1997, 2000, 2001, 2002 etc.)
What I have so far:
import java.util.*;
import java.io.*;
public class ProcessCourses{
public static void main(String[] args) throws FileNotFoundException{
Scanner console = new Scanner(System.in);
String fileName = console.nextLine();
Scanner input = new Scanner(new File(fileName));
while(input.hasNextLine()){
String line = input.nextLine();
String[] arr = line.split(" ");
//......????
}
}
}
What would go inside the //....????
So in the first line you have years, read them first :
Scanner input = new Scanner(new File(fileName));
String str = input.nextLine();
String[] years = str.split(" ");
Now you have set of student's information,
int[] total = new int[years.length];
while(input.hasNextLine()){
String line = input.nextLine();
String[] strength = line.split(" ");
int len = strength.length; // no of entries which includes course id + "years" no.of numbers.
for(int i=1;i<len;i++){ // from 1 because you don't care the course id
total[i-1] = total[i-1] + Integer.parseInt(strength[i]);
}
}
Then just print it :
for(int i=0;i<years.length;i++){
System.out.println(years[i]+ " : " + total[i]);
}

How to read next 10 characters after String variable

I have list of text files I need to read a specific string from, which is always preceded by the string "SWEUserName=". I have been able to print the entire line from the log, but not just the string I need. I do want to print the line number, just not the whole line
So far this is what I've got:
public static String [] openFile() throws FileNotFoundException, IOException{
String searchTech = "SWEUserName=";
int s;
String foundTech = "";
File logs = new File("C:\\Users\\wfedric\\Desktop\\GD\\Java\\Learning\\app\\src\\main\\java\\com\\fedrictechnologies\\learning\\FSDS2.txt");
Scanner scnr = new Scanner(logs);
int lineNumber = 1;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
lineNumber++;
if(line.contains(searchTech)){
s = 10;
foundTech = lineNumber +" :"+ searchTech + s;
System.out.println(foundTech);
System.out.println(line);
}else;
}
return null;
}
I know I am missing something, but I can't for the life of me figure how to count the next 10 characters. I realize at it stands in my code, I am simply printing the Line number followed by my searchTech variable, and the number 10.
I need s to hold on to the 10 characters following searchTech. Perhaps an array is the best way? Just not sure :(
With the above code, I have the following output, which I should expect:
141 :SWEUserName=10
[09/04/14 EDT:8:15:48 AM- INFO- MASC1050141409832948329] - [ HomePageURL ] - ThinClient Home Page URL - https://wls.rio.directv.com/wpservsm_enu/start.swe?SWECmd=ExecuteLogin&SWENeedContext=false&SWEUserName=masc105014&SWEPassword=%5BNDSEnc-D%5Dji%2Fic25k%2FTB%2Fy7mqG2kcb2ndd1S3hgWC8Rfa4e1DvtwKWMGQmTzngA%3D%3D&
143 :SWEUserName=10
[09/04/14 EDT:8:15:48 AM- INFO- ] - [ webServiceRequest ] - Web service Call - RetryCounter: 0, URL: https://wls.rio.directv.com/wpservsm_enu/start.swe?SWECmd=ExecuteLogin&SWENeedContext=false&SWEUserName=masc105014&SWEPassword=%5BNDSEnc-D%5Dji%2Fic25k%2FTB%2Fy7mqG2kcb2ndd1S3hgWC8Rfa4e1DvtwKWMGQmTzngA%3D%3D&, Type: GET
1st and 3rd lines are the General format I want, 2nd and 4th lines are where I get stuck returning the specific values after searchTech.
SOLUTION (During this process, I played with the indexOf method to include the date, and left it there)
public class techMatching {
static int s;
static int d;
static String sTech;
static String dTech;
public static String [] openReadFile() throws FileNotFoundException, IOException{
String searchTech = "SWEUserName=";
String foundTech;
File logs = new File("C:\\FSDS2.txt");
Scanner scnr = new Scanner(logs);
int lineNumber = 1;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
lineNumber++;
if(line.contains(searchTech)){
s = line.indexOf(searchTech);
sTech = line.substring(s+12,s+22);
d = line.indexOf("[");
dTech = line.substring(1, 22);
foundTech = lineNumber +": "+ "(" + dTech + ")" + "|"+ sTech.toUpperCase();
System.out.println(foundTech);
}else;
}
return null;
}
Which returned the expected output:
141: (09/04/14 EDT:8:15:48 )|MASC105014
143: (09/04/14 EDT:8:15:48 )|MASC105014
And so on.
"" ""
I suggest you look at the methods available in the String class. Using indexOf(searchTech), you know where in the line the "SWEUserName=" is. Using substring, you can get a String consisting of part of the line.

java splitting a string by line \n not working

So I have a string which is from a text file, essentially the text file is just 5 lines which read:
x=1
y=15
z=128
topx=100
leftx=150
label= this is a test
I am able to get the split to work once which separates via the '=' sign, but when I try to split the string again by \n nothing works, I have tried using "\r?\n", line.Separator etc. but the string value always stays the same, basically the 5 lines without the characters before the = sign. How would I pull out the individual lines to assign variables to?
Here is the code I have, basically the println is to try and see if I can get the first value '1' to list separate from the rest of the lines.
public static void main(String[] a) {
15 draw d = new draw();
16 Read r = new Read();
17 String m = r.doRead("variables.txt");
18
19 String[] ss = new String[5];
20 ss = m.split("\n");
21
22 String[] kv= new String[5];
23 for (int i=0; i<ss.length; i++) {
24 kv = ss[i].split("=");
25 String eol = System.getProperty("line.seperator");
26 String test = kv[1];
27 String[] split = new String[5];
28 split = test.split("\n");
29
30
31
32
33 String first = split[0];
34 //String second = split[1];
35 //String third = split[2];
36 //String fourth = split[3];
37 //String fifth = split[4];
38 System.out.println(first);
39 }
When every line looks like
x=1 y=15 z=128 topx=100 leftx=150 label= this is a test
you should first split at a whitespace to get 5 parts (x=1, y=15, ...) and then at = to get the "key" and "value" part of each part.
check this out:
String s = "x=1\ny=15\nz=128\ntopx=100\nleftx=150\nlabel= this is a test";
String[] ss = s.split("\n");
System.err.println( Arrays.asList(ss[0].split("=")) );

Reading from a file, InputMismatchException

public static void main(String[] args) throws FileNotFoundException {
File file = new File("data.txt"); // select file
try{
Scanner sc = new Scanner(file); // set scanner to file
try{
while (sc.hasNextLine()){
Employee employee = new Employee(); //create employee to hold data
assignData(sc); //read data into employee
employee.getGross(); //calculate gross pay
addEmployee(employee);//assign employee to array
}//end while
}//end try2
finally {
sc.close(); // close file, saving resource usage
}//end finally
}//end try1
catch(IOException e) {
e.printStackTrace();
}
public static void assignData(Scanner input){
//accept scanner
//read necessary input for employee
EmpID = input.next();****
LastName = input.next();****
FirstName = input.next();****
(LINE 36)Hours = input.nextDouble();
Rate = input.nextDouble();
}
Data File contents
42345 Bruch Max 40 21.50
23456 Elgar Eddie 43 20.0
34567 Bach John 30 30
12345 Wagner Rick 41 30
88888 Mozart Wolfie 36 40
65432 Chopin Fred 45 23.25
72345 Strauss Butch 50 25
compiling, i get the error
"Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at programassignment1c.Employee.assignData(Employee.java:36) (
at programassignment1c.ProgramAssignment1C.main(ProgramAssignment1C.java:44)
Java Result: 1
Line 36 in Employee class, is highlighted above, it is the nextDouble() What am i doing wrong? Can doubles be scanned for even if they have no decimal place?
Writing this out by hand I see the scanner starting on: 42345 and it sets this to EmpID, then scanner advances past whitespace to
Bruch and it sets this to LastName, then scanner advances past whitespace to Max and it sets this to FirstName, then scanner advances past whitespace to 40 and should set this to Hours???? i am not following the scanner correctly?
I believe you need to use specific data type methods here :
EmpID = input.nextLine();
LastName = input.nextLine();
FirstName = input.nextLine();
try using nextInt/next instead of nextLine.As nextLine will read the entire line and when you try to assign it to an int value it throws InputMismatchException.
nextLine() won't fit in your requirement(because data in the file is separated by space not the new line).
Since It advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
So try this
public static void assignData(Scanner input)
{
empID = input.nextInt();
lastName = input.next();
firstName = input.next();
hours = input.nextDouble();
rate = input.nextDouble();
}
Note: I have changed your variables names to follow java naming convention.
You are calling nextLine() instead of next() which reads (and skips but doesn't consume line separators) the line each time. You have
EmpID = input.nextLine();
LastName = input.nextLine();
FirstName = input.nextLine();
(LINE 36)Hours = input.nextDouble();
Rate = input.nextDouble();
So
42345 Bruch Max 40 21.50 // EmpID
23456 Elgar Eddie 43 20.0 // LastName
34567 Bach John 30 30 // FirstName, but not the new line characters
12345 Wagner Rick 41 30
88888 Mozart Wolfie 36 40
65432 Chopin Fred 45 23.25
72345 Strauss Butch 50 25
See comments.
The javadoc for nextLine() states
This method returns the rest of the current line, excluding any line
separator at the end.
so the nextDouble() call is trying to read the line separator which isn't of type double and so throws an InputMismatchException.
You want each token on the line, use
EmpID = input.next();
LastName = input.next();
FirstName = input.next();
Hours = input.nextDouble();
Rate = input.nextDouble();
The javadoc for next() states
Finds and returns the next complete token from this scanner. A
complete token is preceded and followed by input that matches the
delimiter pattern.
where the delimiter by default is
Pattern.compile("\\p{javaWhitespace}+");
ie. whitespace characters.
Java naming conventions dictate that variable names should start with a lowercase alphabetic character.

Categories