here is a piece of code:
class Main {
public static void main(String[] args) {
try {
CLI.parse (args, new String[0]);
InputStream inputStream = args.length == 0 ?
System.in : new java.io.FileInputStream(CLI.infile);
ANTLRInputStream antlrIOS = new ANTLRInputStream(inputStream);
if (CLI.target == CLI.SCAN || CLI.target == CLI.DEFAULT)
{
DecafScanner lexer = new DecafScanner(antlrIOS);
Token token;
boolean done = false;
while (!done)
{
try
{
for (token=lexer.nextToken();
token.getType()!=Token.EOF; token=lexer.nextToken())
{
String type = "";
String text = token.getText();
switch (token.getType())
{
case DecafScanner.ID:
type = " CHARLITERAL";
break;
}
System.out.println (token.getLine() + type + " " + text);
}
done = true;
} catch(Exception e) {
// print the error:
System.out.println(CLI.infile+" "+e);
}
}
}
else if (CLI.target == CLI.PARSE)
{
DecafScanner lexer = new DecafScanner(antlrIOS);
CommonTokenStream tokens = new CommonTokenStream(lexer);
DecafParser parser = new DecafParser (tokens);
parser.program();
}
} catch(Exception e) {
// print the error:
System.out.println(CLI.infile+" "+e);
}
}
}
It prints out as it is but somehow it does not print the type out only the default value of it which is an empty string. How can I make it to print out from the switch statement?
Thanks!
Try debugging.
Try printing the value from within the switch section, to see if you ever get into it.
Try replacing the switch with a simple "==" to see if you ever get "token.getType() == DecafScanner.ID"
General suggestion - move the definition of "type" and "next" outside the loop to avoid recreating them again and again.
Related
I have written the following method to validate an input String and output it as an int array. The method works completely as I need it to but I would like to add some extra validation to it so that it only allows integers and commas in the input so there are no errors.
An example correct input would be:
"7,23,62,8,1130"
The method is:
public static int[] validator (String [] check) {
int [] out = new int[5];
try
{
if (0 < Integer.parseInt(check[0]) && Integer.parseInt(check[0]) < 100)
{
out[0] = Integer.parseInt(check[0]);
}
else
{
throw new InvalidMessageException();
}
}
catch (InvalidMessageException ex)
{
System.err.println("Invalid instruction message");
return null;
}
try
{
if (0 < Integer.parseInt(check[1]))
{
out[1] = Integer.parseInt(check[1]);
}
else
{
throw new InvalidMessageException();
}
}
catch (InvalidMessageException ex)
{
System.err.println("Invalid instruction message");
return null;
}
try
{
if(0 < Integer.parseInt(check[2]))
{
out[2] = Integer.parseInt(check[2]);
}
else
{
throw new InvalidMessageException();
}
}
catch (InvalidMessageException ex)
{
System.err.println("Invalid instruction message");
return null;
}
try
{
if (0 <= Integer.parseInt(check[3]) && Integer.parseInt(check[3]) < 256)
{
out[3] = Integer.parseInt(check[3]);
}
else
{
throw new InvalidMessageException();
}
}
catch (InvalidMessageException ex)
{
System.err.println("Invalid instruction message");
return null;
}
try
{
if(0 < Integer.parseInt(check[4]))
{
out[4] = Integer.parseInt(check[4]);
}
else
{
throw new InvalidMessageException();
}
}
catch (InvalidMessageException ex)
{
System.err.println("Invalid instruction message");
return null;
}
return out;
}
I have considered doing something like:
inputText = inputText.replace(".", "");
inputText = inputText.replace(":", "");
inputText = inputText.replace(";", "");
inputText = inputText.replace("\"", "");
etc... but it does not seem a particularly great solution. If anyone has a better idea, please let me know. Thanks very much for any help!
I'd say something like this should replace your method, without having read your code, just your requirements:
String input = "7,23,62,8,1130";
if (input.matches("(?:\\d+(?:,|$))+")) {
int[] result = Arrays.stream(input.split(",")).mapToInt(Integer::parseInt).toArray();
} else {
throw new InvalidMessageException("");
}
You can use a regex expression to validate your input:
[0-9]+(,[0-9]+)*,?
Check it with the String matches(regex) method as:
if (yourString.matches("[0-9]+(,[0-9]+)*,?")) {
}
This is exactly what regular expressions are for:
return inputText.matches("\\d+(,\\d+)*");
i trying to apply a dictionary (File of Words) on text(File of text):
we test if the word exists in a line of the text, if yes we will print it (the line). we test all word of dictionary for every line of text.
i used EXPREG pattern+matcher but the problem is the time. the operation take 5H.
The 2 File have 3330ko and 55ko
.
my question is is there another method to do this like UNITEX but in java
public class Tratemant_Dic extends Thread {
Tratemant_Dic() {
}
public void run() {
try {
BufferedReader file_corpus = new BufferedReader(
new InputStreamReader(new FileInputStream(
"corpus-medical.TXT"), "UTF-16LE"));
PrintWriter ecrire = new PrintWriter("sort.html");
String line;
String nom = null;
ecrire.write("<mot><span style=\"color:red\">startsss</span></mot></br><ligne>start\n");
while ((line = file_corpus.readLine()) != null) {
BufferedReader file_nom = new BufferedReader(
new InputStreamReader(new FileInputStream(
"Fichie_sorte.DIC"), "UTF-16LE"));
while ((nom = file_nom.readLine()) != null) {
nom = nom.substring(0, nom.length() - 3);
Pattern p = Pattern.compile("(.*)\\W+" + nom + "\\b.*",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(line);
if (m.find()) {
System.out.println(nom + "==>" + line);
ecrire.write("<mot><span style=\"color:red\">" + nom
+ "</span></mot></br><ligne>" + line + "\n");
}
}
file_nom.close();
}
ecrire.close();
System.out.println("FIN");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
If I understand what you are trying to do correctly, I would not use regular expressions to do it. They're slow and you do not need them.
This is really a string matching problem. Your dictionary should probably be stored in a hash table, using the hashCode() method to get a key for the string. You then search in your dictionary for each word as you read it ( calculating the appropriate hash code as you read it ) from the text. Properly done that should be as fast as it gets.
Remember that hash codes are not guaranteed to be unique, so always make sure the actual strings match even if the hash code is found in the table.
I would start by attempting to time each of the "things" your application does than then target the slowest item (as mentioned in a comment by Jay, one issue in your case is the fact you are loading the dictionary every time) rather than base the improvements on a guess of what is wrong (the regex being slow).
You can use System.nanoTime() or one of the many stopwatches to do this. I normally use guava.
Why you not use instead of
Pattern p = Pattern.compile("(.*)\\W+" + nom + "\\b.*",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(line);
if (m.find()) {
...
just
if(line.indexOf(nom) > -1) {
...
?
Update: if you need word boundary stuff use something:
String lineToLowerCase = line.toLowerCase(); // before second while
...
int index = lineToLowerCase.indexOf(nom.toLowerCase());
if(index > -1) {
if(index ==0 || Character.isWhitespace(lineToLowerCase.charAt(index-1))) {
int indexEnd = index + nom.length();
if (indexEnd >= lineToLowerCase.length() || !Character.isAlphabetic(lineToLowerCase.charAt(indexEnd))) {
...
for testing
public static void main(String[] s) {
check("skdc s dcd dsf", "dcd"); // print true
check("skdc sdcd dsf", "dcd"); // print false
check("dcd dsf", "dcd"); // print true
check("afasa dcd", "dcd"); // print true
check("afasa dCD11", "dcD"); // print true
check("skdc s dcda dsf", "dcd"); // print false
}
public static void check(String line, String nom) {
String lineToLowerCase = line.toLowerCase();
int index = lineToLowerCase.indexOf(nom.toLowerCase());
if(index > -1) {
if(index ==0 || Character.isWhitespace(lineToLowerCase.charAt(index-1))) {
int indexEnd = index + nom.length();
if (indexEnd >= lineToLowerCase.length() || !Character.isAlphabetic(lineToLowerCase.charAt(indexEnd))) {
System.out.println("true");
return;
}
}
}
System.out.println("false");
}
So my code looks like this:
try {
t.delete("word");
result = t.getRootItem().getWord().equals("humpty");
} catch (Exception e) {
result = false;
}
The problem is my compiler keeps saying I have a catch without a previous try but I do have a previous try so what's wrong? Here's my entire main method (I can also post the entire class if you want:
public static void main(String args[]) {
BSTRefBased t;
AbstractBinaryTree tt;
int i;
boolean result;
String message;
message = "Test 1: inserting 'word0' -- ";
t = new BSTRefBased();
try {
t.insert("word0");
result = t.getRootItem().getWord().equals("word0");
} catch (Exception e) {
result = false;
}
System.out.println(message + (result ? "passed" : "FAILED"));
message = "Test 2: inserting 'word1', 'word2', 'word3' -- ";
t = new BSTRefBased();
try {
t.insert("word1");
t.insert("word2");
t.insert("word3");
result = t.getRootItem().getWord().equals("word1");
tt = t.detachLeftSubtree();
result &= tt.getRootItem().getWord().equals("word2");
tt = t.detachRightSubtree();
result &= tt.getRootItem().getWord().equals("word3");
} catch (Exception e) {
result = false;
}
System.out.println(message + (result ? "passed" : "FAILED"));
message = "Test 3: deleting 'word3'";
t = new BSTRefBased
try {
t.delete("word3");
result = t.getRootItem().getWord().equals("word3");
} catch (Exception e) {
result = false;
}
System.out.println(message + (result ? "passed" : "FAILED"));
}
This line appears to be incorrect:
t = new BSTRefBased
There is no () for a constructor call, and there is no semicolon. It's immediately before the try, and those errors must be messing up the parser, such that it no longer recognizes the try. Try
t = new BSTRefBased(); // or a similar constructor call
I've been working on this for a few days now and I just can't make any headway. I've tried using Scanner and BufferedReader and had no luck.
Basically, I have a working method (shortenWord) that takes a String and shortens it according to a text file formatted like this:
hello,lo
any,ne
anyone,ne1
thanks,thx
It also accounts for punctuation so 'hello?' becomes 'lo?' etc.
I need to be able to read in a String and translate each word individually, so "hello? any anyone thanks!" will become "lo? ne ne1 thx!", basically using the method I already have on each word in the String. The code I have will translate the first word but then does nothing to the rest. I think it's something to do with how my BufferedReader is working.
import java.io.*;
public class Shortener {
private FileReader in ;
/*
* Default constructor that will load a default abbreviations text file.
*/
public Shortener() {
try {
in = new FileReader( "abbreviations.txt" );
}
catch ( Exception e ) {
System.out.println( e );
}
}
public String shortenWord( String inWord ) {
String punc = new String(",?.!;") ;
char finalchar = inWord.charAt(inWord.length()-1) ;
String outWord = new String() ;
BufferedReader abrv = new BufferedReader(in) ;
// ends in punctuation
if (punc.indexOf(finalchar) != -1 ) {
String sub = inWord.substring(0, inWord.length()-1) ;
outWord = sub + finalchar ;
try {
String line;
while ( (line = abrv.readLine()) != null ) {
String[] lineArray = line.split(",") ;
if ( line.contains(sub) ) {
outWord = lineArray[1] + finalchar ;
}
}
}
catch (IOException e) {
System.out.println(e) ;
}
}
// no punctuation
else {
outWord = inWord ;
try {
String line;
while( (line = abrv.readLine()) != null) {
String[] lineArray = line.split(",") ;
if ( line.contains(inWord) ) {
outWord = lineArray[1] ;
}
}
}
catch (IOException ioe) {
System.out.println(ioe) ;
}
}
return outWord;
}
public void shortenMessage( String inMessage ) {
String[] messageArray = inMessage.split("\\s+") ;
for (String word : messageArray) {
System.out.println(shortenWord(word));
}
}
}
Any help, or even a nudge in the right direction would be so much appreciated.
Edit: I've tried closing the BufferedReader at the end of the shortenWord method and it just results in me getting an error on every word in the String after the first one saying that the BufferedReader is closed.
So I took at look at this. First of all, if you have the option to change the format of your textfile I would change it to something like this (or XML):
key1=value1
key2=value2
By doing this you could later use java's Properties.load(Reader). This would remove the need for any manual parsing of the file.'
If by any change you don't have the option to change the format then you'll have to parse it yourself. Something like the code below would do that, and put the results into a Map called shortningRules which could then be used later.
private void parseInput(FileReader reader) {
try (BufferedReader br = new BufferedReader(reader)) {
String line;
while ((line = br.readLine()) != null) {
String[] lineComponents = line.split(",");
this.shortningRules.put(lineComponents[0], lineComponents[1]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
When it comes to actually shortening a message I would probably opt for a regex approach, e.g \\bKEY\\b where key is word you want shortened. \\b is a anchor in regex and symbolizes a word boundery which means it will not match spaces or punctuation.
The whole code for doing the shortening would then become something like this:
public void shortenMessage(String message) {
for (Entry<String, String> entry : shortningRules.entrySet()) {
message = message.replaceAll("\\b" + entry.getKey() + "\\b", entry.getValue());
}
System.out.println(message); //This should probably be a return statement instead of a sysout.
}
Putting it all together will give you something this, here I've added a main for testing purposes.
I think you can have a simpler solution using a HashMap. Read all the abbreviations into the map when the Shortener object is created, and just reference it once you have a word. The word will be the key and the abbreviation the value. Like this:
public class Shortener {
private FileReader in;
//the map
private HashMap<String, String> abbreviations;
/*
* Default constructor that will load a default abbreviations text file.
*/
public Shortener() {
//initialize the map
this.abbreviations = new HashMap<>();
try {
in = new FileReader("abbreviations.txt" );
BufferedReader abrv = new BufferedReader(in) ;
String line;
while ((line = abrv.readLine()) != null) {
String [] abv = line.split(",");
//If there is not two items in the file, the file is malformed
if (abv.length != 2) {
throw new IllegalArgumentException("Malformed abbreviation file");
}
//populate the map with the word as key and abbreviation as value
abbreviations.put(abv[0], abv[1]);
}
}
catch ( Exception e ) {
System.out.println( e );
}
}
public String shortenWord( String inWord ) {
String punc = new String(",?.!;") ;
char finalchar = inWord.charAt(inWord.length()-1) ;
// ends in punctuation
if (punc.indexOf(finalchar) != -1) {
String sub = inWord.substring(0, inWord.length() - 1);
//Reference map
String abv = abbreviations.get(sub);
if (abv == null)
return inWord;
return new StringBuilder(abv).append(finalchar).toString();
}
// no punctuation
else {
//Reference map
String abv = abbreviations.get(inWord);
if (abv == null)
return inWord;
return abv;
}
}
public void shortenMessage( String inMessage ) {
String[] messageArray = inMessage.split("\\s+") ;
for (String word : messageArray) {
System.out.println(shortenWord(word));
}
}
public static void main (String [] args) {
Shortener s = new Shortener();
s.shortenMessage("hello? any anyone thanks!");
}
}
Output:
lo?
ne
ne1
thx!
Edit:
From atommans answer, you can basically remove the shortenWord method, by modifying the shortenMessage method like this:
public void shortenMessage(String inMessage) {
for (Entry<String, String> entry:this.abbreviations.entrySet())
inMessage = inMessage.replaceAll(entry.getKey(), entry.getValue());
System.out.println(inMessage);
}
I am writing a program and if it catches an Exception I want to reset the whole program is there anyway please tell me I really need to finish it tonight ?
public static void readinfile(ArrayList<ArrayList> table,
int numberOfColumns,ArrayList<String> header,
ArrayList<ArrayList<String>> original,
ArrayList<String> sntypes, ArrayList<Integer> displaySize,
ArrayList<String> writeOut, Scanner inputStream) {
//System.out.print("enter data file: ");
Scanner keyboard = new Scanner(System.in);
System.out.print("enter data file: ");
String fileName = keyboard.nextLine();
try {
System.out.println("try " + fileName);
inputStream = new Scanner(new FileInputStream(fileName));
System.out.println(inputStream);
} catch (FileNotFoundException E) {
System.out.println("Error in opening file ");
//readinfile(table, numberOfColumns, header,
//original, sntypes,displaySize, writeOut, inputStream );
}
// file is now open and input scanner attached
if (inputStream.hasNextLine()) {
String Line = inputStream.nextLine();
Scanner lineparse = new Scanner(Line);
lineparse.useDelimiter(",");
ArrayList<String> rowOne = new ArrayList<String>();
while (lineparse.hasNext()) {
String temp = lineparse.next();
String originaltemp = temp;
writeOut.add(temp);
temp = temp + "(" + (++numberOfColumns) + ")";
displaySize.add(temp.length());
// row.add(lineparse.next());
if (temp.trim().substring(0, 2).equalsIgnoreCase("S ")
|| temp.trim().substring(0, 2).equalsIgnoreCase("N ")) {
rowOne.add(originaltemp);
header.add(temp.substring(2));
sntypes.add(temp.toUpperCase().substring(0, 2).trim());
} else {
System.out.println("Invalid file please enter a new file: ");
//readinfile(table, numberOfColumns, header, original, sntypes,displaySize,writeOut,Name);
readinfile(table, numberOfColumns, header,
original, sntypes, displaySize, writeOut, inputStream);
}
}
// add table here it gives problem later on...
original.add(rowOne);
}
while (inputStream.hasNextLine()) {
String Line = inputStream.nextLine();
Scanner lineparse = new Scanner(Line);
lineparse.useDelimiter(",");
ArrayList row = new ArrayList();
int j = 0;
while (lineparse.hasNextLine()) {
String temp = lineparse.next().trim();
int sizeOfrow = temp.trim().length();
if (sizeOfrow > displaySize.get(j)) {
displaySize.set(j, sizeOfrow);
}
if (j < numberOfColumns && sntypes.get(j).equalsIgnoreCase("N")) {
try {
if (temp.equalsIgnoreCase("")) {
row.add(new Double(0.0));
} else {
row.add(new Double(temp.trim()));
}
} catch (NumberFormatException E) {
System.out.println("Opps there is a mistake "
+ "I was expecting a number and I found: " + temp);
System.out.println("This row will be ignored");
// break;
}
} else {
if (temp.equalsIgnoreCase("")) {
row.add((" "));
} else {
row.add(temp);
}
}
j++;
}
if (row.size() == numberOfColumns) {
table.add(row);
}
}// close for while
inputStream.close();
}
homework?
Here's a clue on how to think about it:
main:
start loop
start
do stuff
set ok to end
catch exception
set not ok to end
loop if not ok to end
I'm not sure if you meant this, but the following code will run again and again until it succeeds (as in: doesn't throw an exception):
public static void main(String[] args){
while(true){
try{
// execute your code
break; // if successful, exit loop
}catch(SomeException e){
// handle exception
}catch(SomeOtherException e){
// handle exception
}finally{
// clean up, if necessary
}
}
}
Note: while(true) is an awful construct that I'm sure your teachers won't like. Perhaps you'll find a better way to rephrase that.
This is a bit of a hack but you could try calling the main method again, passing the arguments. As long as you didn't modify the string array of arguments, just call main(args); from a try/catch block in the main routine. Of course, if the exception keeps happening you'll loop infinitely and blow the stack:P