Hi I am trying to write a word counter class on Java. I suppose I am reading a file with scanner from the base folder and printing them to the console. However, first item of file returns with a prefix ÿş or sometimes ?? two question mark. Every item in files are string words. Here is my source code, I could not managed to handle this, so please any help would be appreciated, thanks... (By the way I am using JCreator LE 4.5)
import java.io.*;
import java.util.*;
public class WordCounter implements Comparator<Integer>{
public static Scanner myScanner;
private static int orderNumber = 0;
private static String inputName = "";
private static String outputName = "";
private static LinkedHashMap<String, Integer> dictionary = new LinkedHashMap<String, Integer>();
private static ArrayList<String> words = new ArrayList<String>();
private static SortedSet<String> keys;
private static Scanner in;
public static void main(String[] args) {
myScanner = new Scanner(System.in);
System.out.println("Please enter a file name to read...");
inputName = myScanner.nextLine();
System.out.println("Please enter a file name to write in...");
outputName = myScanner.nextLine();
askForOptions();
readFromFile(inputName);
writeToFile(outputName, orderNumber);
}
private static void readFromFile(String fileName){
try {
in = new Scanner(new File(fileName+".txt"));
while(in.hasNext()){
String lowered = in.next().toLowerCase();
if(!lowered.equals(" ") || !lowered.equals("") || !lowered.equals(null)){
System.out.println(lowered);
int lastInd = lowered.length()-1;
char lastChar = lowered.charAt((lastInd-1));
System.out.println(lastChar);
if (lastChar == '?' || lastChar == ',' || lastChar == '.'){
String newLowered = lowered.substring(0, (lastInd-1));
words.add(newLowered);
}else{
words.add(lowered);
}
}
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void writeToFile(String fileName, int orderNumber){
for(String word: words) {
if(dictionary.containsKey(word)){
int val = (int) dictionary.get(word);
dictionary.put(word, val+1);
}else{
dictionary.put(word, 1);
}
}
if(orderNumber == 1){
keys = new TreeSet<String>(dictionary.keySet());
try {
FileWriter writer = new FileWriter(fileName+".txt");
for(String key:keys){
writer.write(key + "\t" + dictionary.get(key) + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}else if(orderNumber == 2){
Comparator<Integer> comp = new Comparator<Integer>() ;
TreeMap<Integer, String> wordsMap = new TreeMap<Integer, String>(comp);
for(Map.Entry<String, Integer> entry:dictionary.entrySet()){
wordsMap.put(entry.getValue(),entry.getKey());
}
try {
FileWriter writer = new FileWriter(fileName+".txt");
for(Map.Entry<Integer, String> entry: wordsMap.entrySet()){
writer.write(entry.getValue() + "\t" + entry.getKey() + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void askForOptions(){
System.out.println("How do you want the result? \nPress 1 to get result in alphabethic order, \nPress 2 to in frequency order.");
int option = myScanner.nextInt();
if (option == 1){
orderNumber = 1;
System.out.println("Thank you! Good luck...");
}else if(option == 2){
orderNumber = 2;
System.out.println("Thank you! Good luck...");
}else{
System.out.println("Invalid choice! Plese try again...");
askForOptions();
}
}
#Override
public int compare(Integer arg0, Integer arg1) {
if (arg0 == arg1) return 0;
if (arg0 > arg1) return 1;
if (arg0 < arg1) return -1;
return 0;
}
}
ÿş are UTF-16 BOM bytes FF FE printed using Windows 1254 codepage, which is your system default I believe.
To read file correcly, you need to skip BOM, which can be done using Apache Commons IO BOMInputStream wrapper:
try (BOMInputStream bis = new BOMInputStream(new FileInputStream(filename));
Scanner in = new Scanner(bis, bis.getBOMCharsetName() == null
? Charset.defaultCharset().name()
: bis.getBOMCharsetName())) {
// read lines
} catch (IOException e) {
// ...
}
Or you can skip those 2 bytes manually as described in answers for this post.
Related
im a first year computer science student learning java and im trying to read a csv file line by line and convert each row to an object, then create an array out of these objects with each element separated by a comma ",". Program keeps returning unusual error: method readFile() in class cannot be applied to given types. im not sure what to do.
main class:
import java.io.*;
import java.util.*;
public class FlightOperations
{
static String fileName = "LaxData.csv";
public static void main(String[] args)
{
// Parsing a CSV file into Scanner class constructor
Scanner sc = new Scanner(System.in);
int fileLength = 0;
fileLength = getFileCount(fileName);
int again = 0;
Date[] LAXarray = new Date[fileLength];
LAXarray = readFile(fileName, fileLength);
menu(LAXarray, sc);
do
{
try
{
System.out.println("\n\nRun program? (1)YES (2)NO");
again = sc.nextInt();
if(again == 1)
{
menu(LAXarray, sc);
}
else
{
System.exit(1);
}
}
catch (InputMismatchException exception)
{
System.out.println("\nInvalid input");
sc.next();
}
}
while (again != 1 || again != 2);
sc.close(); // closes the scanner
}
readFile class:
public static Date[] readFile(String fileName)
{
FileInputStream fileStream = null;
InputStreamReader Read;
BuffferedReader bufRead;
/* int fileLength = getFileLength(fileName); */
String line;
Date[] LAXarray = new Date[getFileCOunt(fileLength)];
int LAXIndex = 0;
try
{
fileStream = new FileInputStream(fileName);
Read = new InputStreamReader(fileStream);
bufRead = new BufferedReader(Read);
line = bufRead.readLine();
for(int i = 1; i < fileLength; i++)
{
line = bufRead.readLine();
LAXarray[i] = processLine(line);
}
}
catch(IOException errorDetails)
{
if(fileStream != null)
{
try
{
fileStream.close();
}
catch(IOException ex2)
{
}
}
System.out.println("Error in fileProcessing: " + errorDetails.getMessage());
}
return LAXarray;
}
I want to create a numbered list, where it shows the index number beside each line. I'm not too sure how to achieve this.
This is the code where i create my list:
public void readFile(Scanner in)
{
inputStudentID = null;
inputMark = 0;
try
{
File file = new File("Marks.txt");
in = new Scanner(file);
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
System.out.println("in " + System.getProperty("user.dir"));
System.exit(1);
}
while (in.hasNextLine())
{
String studentRecord = in.nextLine();
List<String> values = Arrays.asList(studentRecord.split(","));
String inputStudentID = values.get(0);
String sInputMark;
sInputMark = values.get(1);
int inputMark = Integer.parseInt(sInputMark);
addStudent(inputStudentID, inputMark);
}
in.close();
}
This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 7 years ago.
EDIT: While this issue has been marked as a duplicate, the other issue is different to my situation; it seems as though the program is ignoring this line:
decision = scan.nextInt();
I'm having some trouble with my scanner. I have a program that runs off a simple menu system.
The program works, but whenever it goes back to repeating the program again (I made the program have the ability to repeat by putting it in a while loop) it throws this error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:919)
at java.util.Scanner.next(Scanner.java:1542)
at java.util.Scanner.nextInt(Scanner.java:2172)
at java.util.Scanner.nextInt(Scanner.java:2131)
at GuardSearch.Menu(GuardSearch.java:29)
at GuardSearch.main(GuardSearch.java:8)
The program all works on the first run through the menu and its actions, but then when the call of the if statement is completed and the program returns to the while loop, it throws this exception. What am I missing?
I can post all the classes of the program if needed, however I believe my problem is within the following class:
import java.util.Scanner;
import java.lang.*;
public class GuardSearch {
public static void main(String[] args) {
Menu();
}
public static void Menu(){
Scanner scan = new Scanner(System.in);
boolean leave = false;
while(leave!=true){
final String ANSI_CLS = "\u001b[2J";
final String ANSI_HOME = "\u001b[H";
System.out.print(ANSI_CLS + ANSI_HOME);
System.out.flush();
int decision = 0;
System.out.println("Welcome to GuardSearch, our little slice of Google.\n");
while ((decision != 1) || (decision != 2)){
System.out.println("Please enter the number of what you would like to do from the following list:");
System.out.println("1. Submit knowledge.");
System.out.println("2. Search.");
System.out.println("3. Quit.");
decision = scan.nextInt();
if (decision == 1) {
Submit submit = new Submit();
submit.takeinfo();
break;
}
else if (decision == 2){
Search search = new Search();
search.takekeywords();
break;
}
else if (decision == 3){
leave = true;
break;
}
}
}
scan.close();
}
}
Thanks in advance. I have researched this issue and found no occurrences relevant to my exact issue.
EDIT: Here is my Submit and Search class as requested:
import java.io.*;
import java.util.Scanner;
public class Submit {
public static void takeinfo() {
final String ANSI_CLS = "\u001b[2J";
final String ANSI_HOME = "\u001b[H";
System.out.print(ANSI_CLS + ANSI_HOME);
System.out.flush();
System.out.println("Submit your knowledge to the system.\n");
Scanner sc = new Scanner(System.in);
int decision = 0;
while ((decision != 1) || (decision != 2)){
System.out.println("1. Change an existing article.");
System.out.println("2. Create a new article.");
System.out.println("3. Delete an article.");
System.out.println("4. Back.");
decision = sc.nextInt();
if (decision == 1) {
ChangeArticle chngArt = new ChangeArticle();
chngArt.takeinfo();
break;
}
else if (decision == 2){
CreateArticle createArt = new CreateArticle();
createArt.title();
break;
}
else if (decision == 3){
DeleteArticle deleteArt = new DeleteArticle();
deleteArt.takeinfo();
break;
}
else if (decision == 4){
GuardSearch gs = new GuardSearch();
gs.Menu();
break;
}
else {
System.out.println("Please enter a valid number.\n");
}
}
sc.close();
}
}
And the Search class:
import java.util.*;
import java.io.*;
public class Search {
Scanner sc = new Scanner(System.in);
public void takekeywords() {
final String ANSI_CLS = "\u001b[2J";
final String ANSI_HOME = "\u001b[H";
System.out.print(ANSI_CLS + ANSI_HOME);
System.out.flush();
System.out.println("Search the system.\n");
boolean fin = false;
int dec;
while (fin != true){
System.out.println("Please select which search type you want:\n");
System.out.println("1. Keywords.");
System.out.println("2. Category listing.");
System.out.println("3. Back\n");
dec = sc.nextInt();
if (dec == 1) {
// Do a keyword thing
fin = true;
}
else if (dec == 2) {
// Do a category thing
searchCategories();
fin = true;
}
else if (dec == 3) {
GuardSearch gs = new GuardSearch();
gs.Menu();
break;
}
}
}
public void searchCategories(){
// Create an empty list of subcategories, that will be added to when the user wants to add sub categories
LinkedList<Category> newSubCategories = new LinkedList<Category>();
// Create a list of .txt's relevant to the category
LinkedList<String> relevantArticles = new LinkedList<String>();
relevantArticles.add("example.txt");
LinkedList<String> dirListing = new LinkedList<String>();
//System.out.println("Please select your category:\n");
String s = "Example Category";
// Create the first category, passing in its name, an empty list of subCategories (which are of type Category), and a list of .txt's relevant to the category
Category firstCategory = new Category(s, newSubCategories, relevantArticles);
File wd = new File("/bin");
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd /var/tmp/cholland/GuardSearch/");
out.println("ls *.art");
out.println("exit");
String line;
System.out.println();
try {
int x = 1;
while ((line = in.readLine()) != null) {
System.out.println(x + ") " + line);
dirListing.add(line);
x++;
}
System.out.println("Please select the article you want:\n");
int dec = sc.nextInt();
//System.out.println(Arrays.toString(dirListing.toArray()));
System.out.println(dirListing.size());
try {
for (int y=1; y<=dirListing.size(); y++) {
if (y == dec){
boolean fin = false;
while (fin != true){
System.out.println("You chose: " + (dirListing.get(boundIndex(y))) + ". Opening file...");
System.out.println("===========================================================\n");
String text;
String filepath = ("/var/tmp/cholland/GuardSearch/" + (dirListing.get(boundIndex(y))));
BufferedReader br = null;
StringBuffer contents = new StringBuffer();
try {
br = new BufferedReader(new FileReader(filepath));
text = null;
while ((text = br.readLine()) != null) {
contents.append(text).append(System.getProperty("line.separator"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
text = (contents.toString());
System.out.println(text);
System.out.println("\n===========================================================\n");
fin = true;
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
proc.waitFor();
out.close();
in.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}
sc.close();
}
public int boundIndex(int x){
if (x != 0){
return (x - 1);
}
else {
return 0;
}
}
}
I think the problem is here decision = scan.nextInt();
NoSuchElementException will be thrown if no more tokens are available. This is caused by invoking nextInt() without checking if there's any integer available. You can hasNextInt() to check if any more tokens are available.
Something like:
if(scan.hasNextInt() )
decision = scan.nextInt(); // if there is another number
else
decision = 0; // nothing added in the input
The cause of the error is that you are closing the scanner at the end of searchCategories.
So here I have code I have HashMap made up by the words in file, I am adding words and writing them on file and it works, but when I use my remove function for some reaseon doesnt do anything here is the code :
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static File file = new File( C:\\Users\\N\\Desktop\\Newfolder\\Dictionary\\src\\nmishewa\\geekycamp\\dictionary\\bg_win1251.txt");
public static int value = 1;
private static Scanner input;
public static Scanner in = new Scanner(System.in);
public static Map<String, Integer> map = new HashMap<String, Integer>();
public static void main(String[] args) throws FileNotFoundException {
readFile();
System.out.println("Enter number of function wanted" + "\n1 to add"
+ "\n2 for searching by prefix" + "\n3 for deleting");
int choice = in.nextInt();
if (choice == 1) {
System.out.println("enter words seprated by comma");
String wd = in.next();
add(wd);
}
if (choice == 2) {
System.out.println("Enter prefix");
String wd = in.next();
prefixSearch(wd);
}
if (choice == 3) {
System.out.println("ENTER word to delete");
String wd = in.next();
remove(wd);
}
}
public static void readFile() throws FileNotFoundException {
input = new Scanner(file);
boolean done = false;
int value = 1;
while (input.hasNext()) {
String word = input.next().toLowerCase();
String[] line = word.split("[,\\s]+");
for (int j = 0; j < line.length; j++) {
map.put(line[j], value);
value++;
done = true;
}
}
if (done == true) {
System.out.println("Succes");
}
}
public static void prefixSearch(String wd) {
System.out.println("Enter prefix");
String prefix = wd.toLowerCase();
for (Map.Entry<String, Integer> key : map.entrySet()) {
if (key.getKey().startsWith(prefix)) {
System.out.println(key.getKey());
}
}
}
public static void add(String wd) {
boolean done = false;
String word = wd.toLowerCase();
String[] line = word.split("[,\\s]+");
for (int j = 0; j < line.length; j++) {
if (!map.containsKey(line[j])) {
map.put(line[j], value);
value++;
try {
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(map.toString());
bw.close();
done = true;
} catch (Exception e) {
e.printStackTrace();
}
} else {
continue;
}
}
if (done == true) {
System.out.println("Success");
}
}
public static void remove(String wd) {
boolean done = false;
String word = wd.toLowerCase();
String[] line = word.split("[,\\s]+");
for (int j = 0; j < line.length; j++) {
for (Map.Entry<String, Integer> key : map.entrySet()) {
if (key.getKey().equals(line[j])) {
map.remove(key.getKey(), key.getValue());
try {
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(map.toString());
bw.close();
done = true;
} catch (Exception e) {
e.printStackTrace();
}
} else {
continue;
}
}
}
if (done == true) {
System.out.println("Succes");
}
}
}
Every other method is working just fine, but remove. Is there something wrong with the loops, maybe use more optimal way or?
The reason for failure is that you're trying to change the map while iterating the entries. As with any collection - if you try to modify it while iterating it you'll get ConcurrentModificationException.
Further, there's a redundant inner for-loop (redundant because the whole purpose of a map is that you won't have to iterate it when you're looking for a specific value/s) which means that you'll try to override the file many times when only once is sufficient.
public static void remove(String wd) {
boolean done = false;
String word = wd.toLowerCase();
String[] line = word.split("[,\\s]+");
for (int j = 0; j < line.length; j++) {
map.remove(line[j]);
}
try {
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(map.toString());
bw.close();
done = true;
} catch (Exception e) {
e.printStackTrace();
}
if (done == true) {
System.out.println("Success");
}
}
The issues that I can see in your code are the following:
You forgot a quote when defining the file:
public static File file = new File( C:\\Users\\N\\Desktop\\Newfolder\\Dictionary\\src\\nmishewa\\geekycamp\\dictionary\\bg_win1251.txt")
should be:
public static File file = new File("C:\\Users\\N\\Desktop\\Newfolder\\Dictionary\\src\\nmishewa\\geekycamp\\dictionary\\bg_win1251.txt");
The remove() function in a map receives only one parameter, which is the key of the entry you want to remove, so:
map.remove(key.getKey(), key.getValue());
should be:
map.remove(key.getKey());
Also, since your getting the entrySet of your map, maybe you should consider renaming the key variable in the rename() function to entry.
Hi there I have been having trouble appending entered data to the end of a binary file, having looked up how to do so I found a solution here on stack overflow:
try {
ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("BinaryWrite.hagl", true));
out.writeObject(allTowns);
out.flush ();
}
catch (Exception e){
System.out.println("IMPOSSSIBLE");
}
In this piece of code allTowns is my array in which the data I wish to add is held. The problem I am getting is that when I run my program and it displays what is in the file at the end this piece of code never writes to the file at all, I was wondering if anyone could help me understand why this does not work or even just recommend a different method if necessary.
My full code (this part is currently commented out so one can easily create the file):
import java.util.*;
import java.io.*;
public class CoastaslTowns implements Serializable
{
public static Scanner input = new Scanner(System.in);
String name, county;
int population, area;
public static int count = 0;
public static int continuation = 0;
public static CoastaslTowns[] allTowns = new CoastaslTowns[50];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int loop1 = 0;
for (int i=0; i < 50; i++) {
allTowns[i] = new CoastaslTowns();
}
while (loop1 == 0) {
System.out.println("Please enter the name of the town.");
String nameEntered = input.nextLine();
System.out.println("Please enter the county in which the town resides.");
String countyEntered = input.nextLine();
System.out.println("Please enter the population of the town.");
int populationEntered = input.nextInt();
System.out.println("Please enter the area of the town.");
int areaEntered = input.nextInt();
input.nextLine();
System.out.println("Are you satisfied with your entries?");
String satisfaction = input.nextLine();
if (satisfaction.equals("yes")) {
loop1 = 5;
System.out.println("Thank you for entering your details");
continuation = 1;
}
else if (satisfaction.equals("no")) {
System.out.println("Would you like to continue and enter more towns?");
String countychecker = input.nextLine();
if (countychecker.equals("yes")) {
}
else {
break;
}
}
writeToFile(nameEntered, countyEntered, populationEntered, areaEntered);
}
ReturnTowns();
}
public static void inputVariations(){
}
public static void writeToFile(String nameEntered, String countyEntered, int populationEntered, int areaEntered) {
int loop2 = 0;
while (loop2 == 0){
allTowns[count].population = populationEntered;
allTowns[count].name = nameEntered;
allTowns[count].county = countyEntered;
allTowns[count].area = areaEntered;
if (continuation == 1) {
loop2 = 1;
}
else {
loop2 = 1;
}
count = count + 1;
}
try {
FileOutputStream fileOut = new FileOutputStream("BinaryWrite.hgal");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(allTowns);
out.close();
fileOut.close();
} catch (IOException i) {}
/*
try {
ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("BinaryWrite.hagl", true));
out.writeObject(allTowns);
out.flush ();
}
catch (Exception e){
System.out.println("IMPOSSSIBLE");
}
*/
}
public static void ReturnTowns(){
{
int x = 0;
CoastaslTowns[] bw = null;
try {
FileInputStream fileIn =
new FileInputStream("BinaryWrite.hgal");
ObjectInputStream in = new ObjectInputStream(fileIn);
bw = (CoastaslTowns[]) in.readObject();
while (bw[x].population != 0) {
System.out.println(bw[x].name);
System.out.println(bw[x].county);
System.out.println(bw[x].population);
System.out.println(bw[x].area);
System.out.println();
x++;
}
in.close();
fileIn.close();
} catch (IOException i) {
} catch (ClassNotFoundException c) {
}
}
}
}
Any help would be greatly appreciated.