A couple of things I noticed:
Using a TreeMap<Character, Integer>, instead of converting a StringBuilder to a char[] and creating the map, is much more efficient.
You don't test for an empty argument list.
Telling the user they have to use .txt
in the filename is inaccurate as your code doesn't test for that.
When you're printing multiple values on one line, it is more efficient and easier to maintain to use printf
, rather than concatenating strings;
Something like this will work:
public static void main(String[] args) throws IOException {
if (args.length < 1 || args[0].equals("help")) {
System.out.println("usage: java Main filename");
return;
}
fileToChars(args[0])
.forEach((k, v) -> System.out.printf("%1$s: %2$d\n", k, v));
}
private static TreeMap<Character, Integer> fileToChars(String fileName) throws IOException {
final char EOF = (char) -1;
File file = new File(fileName);
if (!file.exists()) {
// Maybe do something here?
}
TreeMap<Character, Integer> builder = new TreeMap<>();
try (FileReader reader = new FileReader(file)) {
Character input;
while ((input = (char) reader.read()) != EOF) {
Integer count = builder.getOrDefault(input, 0);
builder.put(input, ++count);
}
} catch (IOException e) {
throw new IOException("Problem reading file");
}
return builder;
}
This gives a sorted output of the mapped Characters. If an 'as is' output is wanted, change the TreeMap
to a HashMap
.
A couple of things I noticed:
Using a TreeMap<Character, Integer>, instead of converting a StringBuilder to a char[] and creating the map, is much more efficient.
You don't test for an empty argument list.
Telling the user they have to use .txt
in the filename is inaccurate as your code doesn't test for that.
When you're printing multiple values on one line, it is more efficient and easier to maintain to use printf
, rather than concatenating strings;
Something like this will work:
public static void main(String[] args) throws IOException {
if (args.length < 1 || args[0].equals("help")) {
System.out.println("usage: java Main filename");
return;
}
fileToChars(args[0])
.forEach((k, v) -> System.out.printf("%1$s: %2$d\n", k, v));
}
private static TreeMap<Character, Integer> fileToChars(String fileName) throws IOException {
final char EOF = (char) -1;
File file = new File(fileName);
if (!file.exists()) {
// Maybe do something here?
}
TreeMap<Character, Integer> builder = new TreeMap<>();
try (FileReader reader = new FileReader(file)) {
Character input;
while ((input = (char) reader.read()) != EOF) {
Integer count = builder.getOrDefault(input, 0);
builder.put(input, ++count);
}
} catch (IOException e) {
throw new IOException("Problem reading file");
}
return builder;
}
This gives a sorted output of the mapped Characters. If an 'as is' output is wanted, change the TreeMap
to a HashMap
.
A couple of things I noticed:
Using a TreeMap<Character, Integer>, instead of converting a StringBuilder to a char[] and creating the map, is much more efficient.
You don't test for an empty argument list.
Telling the user they have to use .txt
in the filename is inaccurate as your code doesn't test for that.
When you're printing multiple values on one line, it is more efficient and easier to maintain to use printf
, rather than concatenating strings;
Something like this will work:
public static void main(String[] args) throws IOException {
if (args.length < 1 || args[0].equals("help")) {
System.out.println("usage: java Main filename");
return;
}
fileToChars(args[0])
.forEach((k, v) -> System.out.printf("%1$s: %2$d\n", k, v));
}
private static TreeMap<Character, Integer> fileToChars(String fileName) throws IOException {
final char EOF = (char) -1;
File file = new File(fileName);
TreeMap<Character, Integer> builder = new TreeMap<>();
try (FileReader reader = new FileReader(file)) {
Character input;
while ((input = (char) reader.read()) != EOF) {
Integer count = builder.getOrDefault(input, 0);
builder.put(input, ++count);
}
} catch (IOException e) {
throw new IOException("Problem reading file");
}
return builder;
}
This gives a sorted output of the mapped Characters. If an 'as is' output is wanted, change the TreeMap
to a HashMap
.
A couple of things I noticed:
Using a TreeMap<Character, Integer>, instead of converting a StringBuilder to a char[] and creating the map, is much more efficient.
You don't test for an empty argument list.
Telling the user they have to use .txt
in the filename is inaccurate as your code doesn't test for that.
When you're printing multiple values on one line, it is more efficient and easier to maintain to use printf
, rather than concatenating strings;
Something like this will work:
public static void main(String[] args) throws IOException {
if (args.length < 1 || args[0].equals("help")) {
System.out.println("usage: java Main filename");
return;
}
fileToChars(args[0])
.forEach((k, v) -> System.out.printf("%1$s: %2$d\n", k, v));
}
private static TreeMap<Character, Integer> fileToChars(String fileName) throws IOException {
final char EOF = (char) -1;
File file = new File(fileName);
if (!file.exists()) {
// Maybe do something here?
}
TreeMap<Character, Integer> builder = new TreeMap<>();
try (FileReader reader = new FileReader(file)) {
Character input;
while ((input = (char) reader.read()) != EOF) {
Integer count = builder.getOrDefault(input, 0);
builder.put(input, ++count);
}
} catch (IOException e) {
throw new IOException("Problem reading file");
}
return builder;
}
This gives a sorted output of the mapped Characters. If an as is'as is' output is wanted, change the TreeMap
to a HashMap
.
A couple of things I noticed:
Using a TreeMap<Character, Integer>, instead of converting a StringBuilder to a char[] and creating the map, is much more efficient.
You don't test for an empty argument list.
Telling the user they have to use .txt
in the filename is inaccurate as your code doesn't test for that.
When you're printing multiple values on one line, it is more efficient and easier to maintain to use printf
, rather than concatenating strings;
Something like this will work:
public static void main(String[] args) throws IOException {
if (args.length < 1 || args[0].equals("help")) {
System.out.println("usage: java Main filename");
return;
}
fileToChars(args[0])
.forEach((k, v) -> System.out.printf("%1$s: %2$d\n", k, v));
}
private static TreeMap<Character, Integer> fileToChars(String fileName) throws IOException {
final char EOF = (char) -1;
File file = new File(fileName);
if (!file.exists()) {
// Maybe do something here?
}
TreeMap<Character, Integer> builder = new TreeMap<>();
try (FileReader reader = new FileReader(file)) {
Character input;
while ((input = (char) reader.read()) != EOF) {
Integer count = builder.getOrDefault(input, 0);
builder.put(input, ++count);
}
} catch (IOException e) {
throw new IOException("Problem reading file");
}
return builder;
}
This gives a sorted output of the mapped Characters. If an as is output is wanted, change the TreeMap
to a HashMap
.
A couple of things I noticed:
Using a TreeMap<Character, Integer>, instead of converting a StringBuilder to a char[] and creating the map, is much more efficient.
You don't test for an empty argument list.
Telling the user they have to use .txt
in the filename is inaccurate as your code doesn't test for that.
When you're printing multiple values on one line, it is more efficient and easier to maintain to use printf
, rather than concatenating strings;
Something like this will work:
public static void main(String[] args) throws IOException {
if (args.length < 1 || args[0].equals("help")) {
System.out.println("usage: java Main filename");
return;
}
fileToChars(args[0])
.forEach((k, v) -> System.out.printf("%1$s: %2$d\n", k, v));
}
private static TreeMap<Character, Integer> fileToChars(String fileName) throws IOException {
final char EOF = (char) -1;
File file = new File(fileName);
if (!file.exists()) {
// Maybe do something here?
}
TreeMap<Character, Integer> builder = new TreeMap<>();
try (FileReader reader = new FileReader(file)) {
Character input;
while ((input = (char) reader.read()) != EOF) {
Integer count = builder.getOrDefault(input, 0);
builder.put(input, ++count);
}
} catch (IOException e) {
throw new IOException("Problem reading file");
}
return builder;
}
This gives a sorted output of the mapped Characters. If an 'as is' output is wanted, change the TreeMap
to a HashMap
.
A couple of things I noticed:
Using a TreeMap<Character, Integer>, instead of converting a StringBuilder to a char[] and creating the map, is much more efficient.
You don't test for an empty argument list.
Telling the user they have to use .txt
in the filename is inaccurate as your code doesn't test for that.
When you're printing multiple values on one line, it is more efficient and easier to maintain to use printf
, rather than concatenating strings;
Something like this will work:
public static void main(String[] args) throws IOException {
if (args.length !=< 1 || args[0].equals("help")) {
System.out.println("usage: java Main filename");
return;
}
fileToChars(args[0])
.forEach((k, v) -> System.out.printf("%1$s: %2$d\n", k, v));
}
private static TreeMap<Character,Integer> fileToChars(String fileName) throws IOException {
final char EOF = (char) -1;
File file = new File(fileName);
if (!file.exists()) {
// Maybe do something here?
}
TreeMap<Character,Integer> builder = new TreeMap<>();
try (FileReader reader = new FileReader(file)) {
Character input;
char eof = (char)-1;
while ((input = (char)reader.read()) != eofEOF) {
Integer count = builder.getgetOrDefault(input, 0);
builder.put(input, (count == null) ? 1: ++count);
}
} catch (IOException e) {
throw new IOException("Problem reading file");
}
return builder;
}
This gives a sorted output of the mapped Characters. If an as is output is wanted, change the TreeMap
to a HashMap
.
A couple of things I noticed:
Using a TreeMap<Character, Integer>, instead of converting a StringBuilder to a char[] and creating the map, is much more efficient.
You don't test for an empty argument list.
Telling the user they have to use .txt
in the filename is inaccurate as your code doesn't test for that.
Something like this will work:
public static void main(String[] args) throws IOException {
if (args.length != 1 || args[0].equals("help")) {
System.out.println("usage: java Main filename");
return;
}
fileToChars(args[0])
.forEach((k, v) -> System.out.printf("%1$s: %2$d\n", k, v));
}
private static TreeMap<Character,Integer> fileToChars(String fileName) throws IOException {
File file = new File(fileName);
if (!file.exists()) {
// Maybe do something here?
}
TreeMap<Character,Integer> builder = new TreeMap<>();
try (FileReader reader = new FileReader(file)) {
Character input;
char eof = (char)-1;
while ((input = (char)reader.read()) != eof) {
Integer count = builder.get(input);
builder.put(input, (count == null) ? 1: ++count);
}
} catch (IOException e) {
throw new IOException("Problem reading file");
}
return builder;
}
This gives a sorted output of the mapped Characters. If an as is output is wanted, change the TreeMap
to a HashMap
.
A couple of things I noticed:
Using a TreeMap<Character, Integer>, instead of converting a StringBuilder to a char[] and creating the map, is much more efficient.
You don't test for an empty argument list.
Telling the user they have to use .txt
in the filename is inaccurate as your code doesn't test for that.
When you're printing multiple values on one line, it is more efficient and easier to maintain to use printf
, rather than concatenating strings;
Something like this will work:
public static void main(String[] args) throws IOException {
if (args.length < 1 || args[0].equals("help")) {
System.out.println("usage: java Main filename");
return;
}
fileToChars(args[0])
.forEach((k, v) -> System.out.printf("%1$s: %2$d\n", k, v));
}
private static TreeMap<Character,Integer> fileToChars(String fileName) throws IOException {
final char EOF = (char) -1;
File file = new File(fileName);
if (!file.exists()) {
// Maybe do something here?
}
TreeMap<Character,Integer> builder = new TreeMap<>();
try (FileReader reader = new FileReader(file)) {
Character input;
while ((input = (char)reader.read()) != EOF) {
Integer count = builder.getOrDefault(input, 0);
builder.put(input, ++count);
}
} catch (IOException e) {
throw new IOException("Problem reading file");
}
return builder;
}
This gives a sorted output of the mapped Characters. If an as is output is wanted, change the TreeMap
to a HashMap
.