I'm trying to getting input from a file which has various different lines.
e.g. the format is Book title, Author:Borrower second name First Name:Borrower state
here's some example lines.
The Lord of the Rings, JRR Tolkien:McInnes Elizabeth:13 11 10
Crime And Punishment, Fyodor Dostoyevsky
The Clan Of The Cave Bear, Jean M Auel
The God Of Small Things, Arundhati Roy:Robins Joshua:20 11 10
So I tried to use useDelimiter after setting up a scanner, but since some line are shorter I don't no quite what to do.
Here is a solution based on regular expressions:
import java.io.*;
import java.util.regex.*;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
Pattern p = Pattern.compile("(.+?),(.+?)(?::(.+?):(\\d+ \\d+ \\d+))?");
String line;
while (null != (line = br.readLine())) {
Matcher m = p.matcher(line);
if (m.matches()) {
String title = m.group(1);
String author = m.group(2);
String borrower = m.group(3);
String data = m.group(4);
System.out.println("Title: " + title);
System.out.println("Author: " + author);
if (borrower != null) {
System.out.println(" Borrower: " + borrower);
System.out.println(" Data: " + data);
}
}
System.out.println();
}
br.close();
}
}
Given your sample input, it prints:
Title: The Lord of the Rings
Author: JRR Tolkien
Borrower: McInnes Elizabeth
Data: 13 11 10
Title: Crime And Punishment
Author: Fyodor Dostoyevsky
Title: The Clan Of The Cave Bear
Author: Jean M Auel
Title: The God Of Small Things
Author: Arundhati Roy
Borrower: Robins Joshua
Data: 20 11 10
Read the file line by line, use the [^,:] regular expression to match data in each line (sequential find will bring title, author, and borrower, state, if any).
You could use .split()
try {
FileInputStream fstream = new FileInputStream("input.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
String author;
String title;
String borrower;
String date;
while ((line = br.readLine()) != null) {
(author,title) = line.split(",");
if (line.contains(":")
(title,borrower,date) = title.split(":");
/*** Do what you need to do with the values here ***/
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
Why always for such trivial tasks such oversized tools like regular expressions are suggested? Why not simply use the good old line.indexOf() or line.lastIndexOf() methods?
I would split each line (using String.split) passing in the colon as the delimiter. Then use lastIndexOf(',') on the first element returned by split in order to separate the author from the book:
public class ReadCrappyInput {
public static List<String> testData() {
List<String> lines = new ArrayList<String>();
lines.add("The Lord of the Rings, JRR Tolkien:McInnes Elizabeth:13 11 10");
lines.add("Crime And Punishment, Fyodor Dostoyevsky");
lines.add("The Clan Of The Cave Bear, Jean M Auel");
lines.add("The God Of Small Things, Arundhati Roy:Robins Joshua:20 11 10");
return lines;
}
public Map<String, String> readLine(String line) {
String[] parts = line.split(":");
int endOfTitleIndex = parts[0].lastIndexOf(',');
Map<String, String> map = new HashMap<String, String>();
map.put("title", parts[0].substring(0, endOfTitleIndex));
map.put("author", parts[0].substring(endOfTitleIndex + 1).trim());
if (parts.length > 1) {
map.put("borrower", parts[1]);
}
if (parts.length > 2) {
map.put("data", parts[2]);
}
return map;
}
public static void main(String[] args) {
ReadCrappyInput r = new ReadCrappyInput();
for (String s : testData()) {
System.out.println(r.readLine(s));
}
}
}
Related
I would like to count countX and countX using the same loop instead of creating three different loops. Is there any easy way approaching that?
public class Absence {
private static File file = new File("/Users/naplo.txt");
private static File file_out = new File("/Users/naplo_out.txt");
private static BufferedReader br = null;
private static BufferedWriter bw = null;
public static void main(String[] args) throws IOException {
int countSign = 0;
int countX = 0;
int countI = 0;
String sign = "#";
String absenceX = "X";
String absenceI = "I";
try {
br = new BufferedReader(new FileReader(file));
bw = new BufferedWriter(new FileWriter(file_out));
String st;
while ((st = br.readLine()) != null) {
for (String element : st.split(" ")) {
if (element.matches(sign)) {
countSign++;
continue;
}
if (element.matches(absenceX)) {
countX++;
continue;
}
if (element.matches(absenceI)) {
countI++;
}
}
}
System.out.println("2. exerc.: There are " + countSign + " rows int the file with that sign.");
System.out.println("3. exerc.: There are " + countX + " with sick note, and " + countI + " without sick note!");
} catch (FileNotFoundException ex) {
Logger.getLogger(Absence.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
text file example:
# 03 26
Jujuba Ibolya IXXXXXX
Maracuja Kolos XXXXXXX
I think you meant using less than 3 if statements. You can actually so it with no ifs.
In your for loop write this:
Countsign += (element.matches(sign)) ? 1 : 0;
CountX += (element.matches(absenceX)) ? 1 : 0;
CountI += (element.matches(absenceI)) ? 1 : 0;
Both answers check if the word (element) matches all regular expressions while this can (and should, if you ask me) be avoided since a word can match only one regex. I am referring to the continue part your original code has, which is good since you do not have to do any further checks.
So, I am leaving here one way to do it with Java 8 Streams in "one liner".
But let's assume the following regular expressions:
String absenceX = "X*";
String absenceI = "I.*";
and one more (for the sake of the example):
String onlyNumbers = "[0-9]*";
In order to have some matches on them.
The text is as you gave it.
public class Test {
public static void main(String[] args) throws IOException {
File desktop = new File(System.getProperty("user.home"), "Desktop");
File txtFile = new File(desktop, "test.txt");
String sign = "#";
String absenceX = "X*";
String absenceI = "I.*";
String onlyNumbers = "[0-9]*";
List<String> regexes = Arrays.asList(sign, absenceX, absenceI, onlyNumbers);
List<String> lines = Files.readAllLines(txtFile.toPath());
//#formatter:off
Map<String, Long> result = lines.stream()
.flatMap(line-> Stream.of(line.split(" "))) //map these lines to words
.map(word -> regexes.stream().filter(word::matches).findFirst()) //find the first regex this word matches
.filter(Optional::isPresent) //If it matches no regex, it will be ignored
.collect(Collectors.groupingBy(Optional::get, Collectors.counting())); //collect
System.out.println(result);
}
}
The result:
{X*=1, #=1, I.=2, [0-9]=2}
X*=1 came from word: XXXXXXX
#=1 came from word: #
I.*=2 came from words: IXXXXXX and Ibolya
[0-9]*=2 came from words: 03 and 06
Ignore the fact I load all lines in memory.
So I made it with the following lines to work. It escaped my attention that every character need to be separated from each other. Your ternary operation suggestion also nice so I will use it.
String myString;
while ((myString = br.readLine()) != null) {
String newString = myString.replaceAll("", " ").trim();
for (String element : newString.split(" ")) {
countSign += (element.matches(sign)) ? 1 : 0;
countX += (element.matches(absenceX)) ? 1 : 0;
countI += (element.matches(absenceI)) ? 1 : 0;
This question already has answers here:
Sorting a text file in Java
(4 answers)
How can I sort Map values by key in Java?
(17 answers)
Closed 6 years ago.
This is the code I have that reads two files, one with words and the other with the scrambled letters. The program reads and matches the scrambled letters to words in the file. It works but I want to the output to be in alphabetical order. Where in my code can I put in a sort?
import java.io.*;
import java.util.*;
public class Tester
{
public static void main(String args[]) throws Exception
{
BufferedReader dictionary = new BufferedReader( new FileReader(args[0]) );
BufferedReader jumbles = new BufferedReader( new FileReader(args[1]) );
HashMap<String, List<String>> lookup = new HashMap<String, List<String>>();
while(dictionary.ready())
{
String word = dictionary.readLine();
addWord(word, lookup);
}
dictionary.close();
while(jumbles.ready())
{
String jWord = jumbles.readLine();
List<String>dWords= lookup.get(createKey(jWord));
String wordsString = Arrays.toString(dWords.toArray()).replace("[", "").replace("]", "").replace(",", "").trim();
if(dWords != null){
System.out.println(jWord + " " + wordsString);
}
}
jumbles.close();
}
private static String createKey(String word)
{
char[] cword = word.toCharArray();
Arrays.sort(cword);
return new String(cword);
}
private static void addWord(String word, Map<String, List<String>> lookup)
{
String key = createKey(word);
List<String> list = lookup.get(key);
if(list == null)
{
list = new ArrayList<String>();
lookup.put(key, list);
}
list.add(word);
}
}
outputs:
atc act cat tac
otsp post pots stop spot tops opts
gdo dog god
atr rat tar art
arpt trap tarp part
grof frog
sylogs glossy
What I want:
arpt part tarp trap
atc act cat tac
atr art rat tar
gdo dog god
grof frog
otsp opts post pots spot stop tops
sylogs glossy
nevermind.
Fixed it with:
while(jumbles.ready())
{
jSorted.add(jumbles.readLine());
}
jumbles.close();
Collections.sort(jSorted);
for(int i = 0; i < jSorted.size(); i++)
{
String jWord = jSorted.get(i);
List<String>dWords= lookup.get(createKey(jWord));
String wordsString = Arrays.toString(dWords.toArray()).replace("[", "").replace("]", "").replace(",", "").trim();
if(dWords != null){
System.out.println(jWord + " " + wordsString);
}
}
You can create a list of strings and then sort them
List<String> result = new ArrayList<>();
while(jumbles.ready())
{
String jWord = jumbles.readLine();
List<String>dWords= lookup.get(createKey(jWord));
String wordsString = Arrays.toString(dWords.toArray()).replace("[", "").replace("]", "").replace(",", "").trim();
if(dWords != null){
result.add(jWord + " " + wordsString);
}
}
jumbles.close();
result.sort(String::compareTo);
result.forEach(System.out::println);
I'm reading from the file:
name1 wordx wordy passw1
name2 wordx wordy passw2
name3 wordx wordy passw3
name (i) wordx wordy PASSW (i)
x
x word
x words
words
x
words
At the moment I can print line by line:
Line 1: name1 wordx wordy passw1
Line 2: name2 wordx wordy passw2
I plan to have access to:
users [0] = name1
users [1] = name2
users [2] = name3
..
passws [0] = passw1
passws [1] = passw2
passws [2] = passw3
..
My code is:
public static void main(String args[]) throws FileNotFoundException, IOException {
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = null;
try {
inFile = new Scanner(new File("C:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (inFile.hasNextLine()) {
list.add(inFile.nextLine()+",");
}
String listString = "";
for (String s : list) {
listString += s + "\t";
}
String[] parts = listString.split(",");
System.out.println("Line1: "+ parts[0]);
}
How do I get the following output:
User is name1 and password is passw1
User is name32 and password is passw32
Thanks in advance.
Something like this will do:
public static void main(String args[]) throws FileNotFoundException, IOException {
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = null;
try {
inFile = new Scanner(new File("C:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (inFile.hasNextLine()) {
list.add(inFile.nextLine());
}
int line = 0;
String[] parts = list.get(line).split(" ");
String username = parts[0];
String pass = parts[3];
System.out.println("Line" + (line + 1) + ": " + "User is " + username +" and password is " + pass);
}
EDIT: if you want to iterate through all lines just put last lines in a loop:
for (int line = 0; line < list.size(); line++) {
String[] parts = list.get(line).split(" ");
String username = parts[0];
String pass = parts[3];
System.out.println("Line" + (line + 1) + ": " + "User is " + username +" and password is " + pass);
}
First thing to do is, to add this loop to the end of your code :
for(int i = 0; i <= parts.length(); i++){
System.out.println("parts["+i+"] :" + parts[i] );
}
that will simply show the result of the split using ,.
Then adapt your code, you may want to use another regex to split() your lines, for instance a space.
String[] parts = listString.split(" ");
for documentation about split() method check this.
If you want to get that output then this should do the trick:
public static void main(String args[]) throws FileNotFoundException, IOException {
Scanner inFile = null;
try {
inFile = new Scanner(new File("F:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Map<String, String> userAndPassMap = new LinkedHashMap<>();
while (inFile.hasNextLine()) {
String nextLine = inFile.nextLine();
String[] userAndPass = nextLine.split(" ");
userAndPassMap.put(userAndPass[0], userAndPass[1]);
}
for (Map.Entry<String, String> entry : userAndPassMap.entrySet()) {
System.out.println("User is:" + entry.getKey() + " and password is:" + entry.getValue());
}
}
By storing in a map you are linking directly each username with its password. If you need to save them into separate arrays then you can do this in the while loop instead:
List<String> users = new LinkedList<>(),passwords = new LinkedList<>();
while (inFile.hasNextLine()) {
String nextLine = inFile.nextLine();
String[] userAndPass = nextLine.split(" ");
users.add(userAndPass[0]);
passwords.add(userAndPass[1]);
}
and later transform them to arrays
users.toArray()
I recommend you use a java.util.Map, a standard API which allows you to store objects and read each one of them by a key. (In your case, string objects indexed by string keys). Example:
Let's assume this empty map:
Map<String, String> map=new HashMap<String,String>();
If you store this:
map.put("month", "january");
map.put("day", "sunday");
You can expect that map.get("month") will return "january", map.get("day") will return "sunday", and map.get(any-other-string) will return null.
Back to your case: First, you must create and populate the map:
private Map<String, String> toMap(Scanner scanner)
{
Map<String, String> map=new HashMap<String, String>();
while (scanner.hasNextLine())
{
String line=scanner.nextLine();
String[] parts=line.split(" ");
// Validation: Process only lines with 4 tokens or more:
if (parts.length>=4)
{
map.put(parts[0], parts[parts.length-1]);
}
}
return map;
}
And then, to read the map:
private void listMap(Map<String,String> map)
{
for (String name : map.keySet())
{
String pass=map.get(name);
System.out.println(...);
}
}
You must include both in your class and call them from the main method.
If you need arbitraray indexing of the read lines, use ArrayList:
First, define a javabean User:
public class User
{
private String name;
private String password;
// ... add full constructor, getters and setters.
}
And then, you must create and populate the list:
private ArrayList<User> toList(Scanner scanner)
{
List<User> list=new ArrayList<User>();
while (scanner.hasNextLine())
{
String line=scanner.nextLine();
String[] parts=line.split(" ");
// Validation: Process only lines with 4 tokens or more:
if (parts.length>=4)
{
list.add(new User(parts[0], parts[parts.length-1]));
}
}
return list;
}
I have a text file:
John Smith 2009-11-04
Jenny Doe 2009-12-29
Alice Jones 2009-01-03
Bob Candice 2009-01-04
Carol Heart 2009-01-07
Carlos Diaz 2009-01-10
Charlie Brown 2009-01-14
I'm trying to remove the dashes and store them as separate types: first, last, year,month,day and then add it to a sortedset/hashmap. But for some reason. It's not working right.
Here is my code:
public class Test {
File file;
private Scanner sc;
//HashMap<Name, Date> hashmap = new HashMap<>();
/**
* #param filename
*/
public Test(String filename) {
file = new File(filename);
}
public void openFile(String filename) {
// open the file for scanning
System.out.println("Test file " + filename + "\n");
try {
sc = new Scanner(new File("birthdays.dat"));
}
catch(Exception e) {
System.out.println("Birthdays: Unable to open data file");
}
}
public void readFile() {
System.out.println("Name Birthday");
System.out.println("---- --------");
System.out.println("---- --------");
while (sc.hasNext()) {
String line = sc.nextLine();
String[] split = line.split("[ ]?-[ ]?");
String first = split[0];
String last = split[1];
//int year = Integer.parseInt(split[2]);
//int month = Integer.parseInt(split[3]);
//int day = Integer.parseInt(split[4]);
Resource name = new Name(first, last);
System.out.println(first + " " + last + " " + split[2] );
//hashmap.add(name);
}
}
public void closeFile() {
sc.close();
}
public static void main(String[] args) throws FileNotFoundException,
ArrayIndexOutOfBoundsException {
try {
Scanner sc = new Scanner( new File(args[0]) );
for( int i = 0; i < args.length; i++ ) {
//System.out.println( args[i] );
if( args.length == 0 ) {
}
else if( args.length >= 1 ) {
}
// System.out.printf( "Name %-20s Birthday", name.toString(), date.toString() );
}
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Usage: Birthdays dataFile");
// Terminate the program here somehow, or see below.
System.exit(-1);
} catch (FileNotFoundException e) {
System.err.println("Birthdays: Unable to open data file");
// Terminate the program here somehow, or see below.
System.exit(-1);
}
Test r = new Test(args[0]);
r.openFile(args[0]);
r.readFile();
r.closeFile();
}
}
Your splitting on dashes but your is program is build around a split using spaces.
Try just splitting on spaces
String[] split = line.split("\\s");
So "John Smith 2009-11-04".split("[ ]?-[ ]?"); results in ["John Smith 2009", "11", "04"] When what you want is for it to split on spaces ["John", "Smith", "2009-11-04"]
I would do this differently, first create a domain object:
public class Person {
private String firstName;
private String lastName;
private LocalDate date;
//getters & setters
//equals & hashCode
//toString
}
Now create a method that parses a single String of the format you have to a Person:
//instance variable
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public Person parsePerson(final String input) {
final String[] data = input.split("\\s+");
final Person person = new Person();
person.setFirstName(data[0]);
person.setLastName(data[1]);
person.setDate(LocalDate.parse(data[2], dateTimeFormatter));
return person;
}
Note that the DateTimeFormatter is an instance variable, this is for speed. You should also set the ZoneInfo on the formatter if you need to parse dates not in your current locale.
Now, you can read your file into a List<Person> very easily:
public List<Person> readFromFile(final Path path) throws IOException {
try (final Stream<String> lines = Files.lines(path)) {
return lines
.map(this::parsePerson)
.collect(toList());
}
}
And now that you have a List<Person>, you can sort or process them however you want.
You can even do this while creating the List:
public List<Person> readFromFile(final Path path) throws IOException {
try (final Stream<String> lines = Files.lines(path)) {
return lines
.map(this::parsePerson)
.sorted(comparing(Person::getLastName).thenComparing(Person::getFirstName))
.collect(toList());
}
}
Or have your Person implements Comparable<Person> and simply use natural order.
TL;DR: Use Objects for your objects and life becomes much simpler.
I would use a regex:
private static Pattern LINE_PATTERN
= Pattern.compile("(.+) (.+) ([0-9]{4})-([0-9]{2})-([0-9]{2})");
...
while (sc.hasNext()) {
String line = sc.nextLine();
Matcher matcher = LINE_PATTERN.matcher(line);
if (!matcher.matches()) {
// malformed line
} else {
String first = matcher.group(1);
String last = matcher.group(2);
int year = Integer.parseInt(matcher.group(3));
int month = Integer.parseInt(matcher.group(4));
int day = Integer.parseInt(matcher.group(5));
// do something with it
}
}
You are splitting on spaces and a hyphen. This pattern does not exist.
String[] split = line.split("[ ]?");
String first = split[0];
String last = split[1];
line = split[2];
//now split the date
String[] splitz = line.split("-");
or something like this might work:
String delims = "[ -]+";
String[] tokens = line.split(delims);
If i understood your question right then Here is answer. Check it out.
List<String> listGet = new ArrayList<String>();
String getVal = "John Smith 2009-11-04";
String[] splited = getVal.split("[\\-:\\s]");
for(int j=0;j<splited.length;j++)
{
listGet.add(splited[j]);
}
System.out.println("first name :"+listGet.get(0));
System.out.println("Last name :"+listGet.get(1));
System.out.println("year is :"+listGet.get(2));
System.out.println("month is :"+listGet.get(3));
System.out.println("day is :"+listGet.get(4));
OP :
first name :John
Last name :Smith
year is :2009
month is :11
day is :04
public class array {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("fruit.txt"));
System.out.println("enter the fruit you want to search");
Scanner input = new Scanner(System.in);
String fruit = input.nextLine();
String line;
List<String> list = new ArrayList<String>();
while((line=reader.readLine()) !=null)
{
list.add(line);
}
reader.close();
for (String s : list) {
System.out.println(s);
}
}
}
I have fruit.txt
apple 20 good
orange 30 good
banana 40 needmore
how do I retrieve how many oranges I have from the array list.
I want the program to read the user input in this case "orange" and display out 30 and the status is not good.
ideal output is
You have orange 30 of them and status is good
Try the following updated class.
public class array
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader("fruit.txt"));
System.out.println("enter the fruit you want to search");
Scanner input = new Scanner(System.in);
String fruit = input.nextLine();
String line;
boolean found = false;
int count = 0;
List<String> list = new ArrayList<String>();
while ((line = reader.readLine()) != null)
{
String[] items = line.split(" ");
if (fruit.equals(items[0]))
{
found = true;
count = Integer.parseInt(items[1]);
break;
}
list.add(line);
}
reader.close();
if (found)
{
System.out.println("You have " + fruit + " " + count + " of them and status is good");
}
}
}
You need to split your Strings in your List, and then print each elements of your array obtained within your specified string format: -
for (String s : list) {
String[] tokens = s.split(" ");
if (tokens[0].equals(fruit)) {
System.out.println("You have " + tokens[0] + " " + tokens[1] +
" of them and status is " + tokens[2]);
break;
}
}
Or, you can use: -
System.out.format("You have %s %s of them and status is %s",
tokens[0], tokens[1], tokens[2]);
You will need to split up the lines into the three fields using a StringTokenizer. Then I would create a new class to hold that information.
When you read a line, split the values into String array like this:
while((line=reader.readLine()) !=null)
{
String [] values = line.split(" ");
list.add("You have "+values[0] + " " + values[1] " of them and status is "+values[2] );
}
Not tested but should work, try:
public class array {
public static class Fruit {
private String name;
private String count;
private String status;
public Fruit(String name, String count, String status) {
this.name = name;
this.count = count;
this.status = status;
}
public String getName() {
return name;
}
public String getCount() {
return count;
}
public String getStatus() {
return status;
}
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("fruit.txt"));
System.out.println("enter the fruit you want to search");
Scanner input = new Scanner(System.in);
String fruit = input.nextLine();
String line= "";
HashMap<String, Fruit> map = new HashMap<String, Fruit>();
while ((line = reader.readLine()) != null) {
String[] strings = line.split(" ");
map.put(strings[0], new Fruit(strings[0], strings[1], strings[2]));
}
reader.close();
System.out.print("You have " + fruit + " " + map.get(fruit).getCount() + " of them and status is: " + map.get(fruit).getStatus());
}
}