Java says a nonempty file is empty? - java

I have a particular file that Java says is empty...
Source Code
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MinimumWorkingExample
{
public static void main(String[] args) throws FileNotFoundException
{
String filename = "/home/tyson/Data/English-French_test/test/test.f";
Scanner fileIn = new Scanner(new File(filename));
System.out.println("***START***");
while(fileIn.hasNextLine())
{
System.out.println(fileIn.nextLine());
}
System.out.println("***FINISH***");
}
}
Output
***START***
***FINISH***
...but the file is not empty:
Console
tyson#tyson-desktop:~$ head /home/tyson/Data/English-French_test/test/test.f
<s snum=0001> 2 . </s>
<s snum=0002> 2 . </s>
<s snum=0003> oh , oh ! </s>
<s snum=0004> oh , oh ! </s>
<s snum=0005> oh , oh ! </s>
<s snum=0006> souvenons - nous , monsieur le Orateur , que ce sont ces secteurs de notre soci�t� qui servent de �pine dorsale � notre �conomie . </s>
<s snum=0007> bravo ! </s>
<s snum=0008> bravo ! </s>
<s snum=0009> monsieur le Orateur , ma question se adresse � le ministre charg� de les transports . </s>
<s snum=0010> tous deux poss�dent de nombreuses ann�es de exp�rience dans la fabrication et la distribution de les produits forestiers . </s>
tyson#tyson-desktop:~$
Question
Why is this happening???

Also do Scanner fileIn = new Scanner(new File(filename), "Cp1252"); as this is the encoding for French, and your system seems to be UTF-8.
The Scanner might have encoding problems if it thinks to read UTF-8 multibytes.

You may be missing the Scanner's default delimiter so it sees your whole file as one line without end, thus hasNextLine() is false. Make sure that the character you get from
Scanner.delimiter()
Is present in your file. If they don't match, you can use
Scanner.useDelimiter("\\s or your regex/string here")
to set it to the correct one.

According to Java Docs, the Line Separators are any of the ones below. Does your file contain any?
private static final String LINE_SEPARATOR_PATTERN = "\r\n|[\n\r\u2028\u2029\u0085]"

Related

error : cannot find symbol with Javac shows up when everything seems to be right

I'm writing a very basic program in java but I get the error "error: cannot find symbol".
It's driving me crazy because I really don't know what I did wrong this time.
Here is the code of the main class
package glaces.tests;
import geometrie.Point ;
import glaces.Iceberg2D;
public class TestIceberg2D {
public static void main (String[] args){
Iceberg2D i1 = new Iceberg2D(new Point(2,3), new Point(6,7));
Iceberg2D i2 = new Iceberg2D(new Point(3,7), new Point(5,9));
Iceberg2D i3 = new Iceberg2D(i1,i2);
System.out.println(i3.toString());
}
}
Here is the class Iceberg2D :
package glaces;
import geometrie.Point;
import java.lang.Math;
/**
* Un iceberg rectangulaire
* #author Martine Gautier, Université de Lorraine
*/
public class Iceberg2D {
private Point enBasAGauche ;
private Point enHautADroite ;
/**
* Construction
* #param g le coin en bas à gauche
* #param d le coin en haut à droite
* uniquement en coordonnées positives
*/
public Iceberg2D(Point g, Point d) {
this.enBasAGauche = g;
this.enHautADroite = d;
}
/**
* Construction par fusion de deux icebergs qui se touchent
* #param i1 premier iceberg à fusionner
* #param i2 deuxième iceberg à fusionner
*/
public Iceberg2D(Iceberg2D i1, Iceberg2D i2) {
this.enBasAGauche = new Point (Math.min(i1.coinEnBasAGauche().getAbscisse(),i2.coinEnBasAGauche().getAbscisse()),Math.min(i1.coinEnBasAGauche().getOrdonnee(),i2.coinEnBasAGauche().getOrdonnee()));
this.enHautADroite = new Point (Math.max(i1.coinEnHautADroite().getAbscisse(),i2.coinEnHautADroite().getAbscisse()),Math.max(i1.coinEnHautADroite().getOrdonnee(),i2.coinEnHautADroite().getOrdonnee()));
}
/**
* Retourne le coin en bas à gauche
* #return le coin en bas à gauche
*/
public Point coinEnBasAGauche() {
return this.enBasAGauche ;
}
/**
* Retourne le coin en haut à droite
* #return le coin en haut à droite
*/
public Point coinEnHautADroite() {
return this.enHautADroite ;
}
/**
* Retourne la hauteur
* #return hauteur
*/
public double hauteur() {
return Math.abs(enHautADroite.getOrdonnee()-enBasAGauche.getOrdonnee());
}
/**
* Retourne la largeur
* #return largeur
*/
public double largeur() {
return Math.abs(enHautADroite.getAbscisse()-enBasAGauche.getAbscisse());
}
/**
* Retourne la surface totale
* #return surface totale
*/
public double surface() {
return hauteur()*largeur() ;
}
/**
* Retourne vrai si il y a une collision entre les deux icebergs
* #param i iceberg potentiellement en collision
* #return vrai si collision entre les deux icebergs
*/
public boolean collision(Iceberg2D i) {
if(this.enBasAGauche.getAbscisse() + largeur() == i.enBasAGauche.getAbscisse() || i.enBasAGauche.getAbscisse() + i.largeur() == this.enBasAGauche.getAbscisse()){
if(this.enBasAGauche.getOrdonnee() + hauteur() >= i.coinEnBasAGauche().getOrdonnee() && i.enBasAGauche.getOrdonnee() + i.hauteur() >= this.coinEnBasAGauche().getOrdonnee()){
return true;
}}
if(this.enBasAGauche.getOrdonnee() + hauteur() == i.enBasAGauche.getOrdonnee() || i.enBasAGauche.getOrdonnee() + i.hauteur() == this.enBasAGauche.getOrdonnee()){
if(this.enBasAGauche.getAbscisse() + largeur() >= i.coinEnBasAGauche().getAbscisse() && i.enBasAGauche.getAbscisse() + i.largeur() >= this.coinEnBasAGauche().getAbscisse()){
return true;
}
}
return false ;
}
/**
* Retourne vrai si this est plus volumineux que i
* #param i iceberg à comparer
* #return vrai si this est plus volumineux que i
*/
public boolean estPlusGrosQue(Iceberg2D i) {
return this.surface() > i.surface();
}
public String toString() {
return "Point bas gauche : "+enBasAGauche.toString()+" / Point haut droite : "+enHautADroite.toString() ;
}
/**
* Retourne le point au centre
* #return le point au centre de l'iceberg
*/
public Point centre() {
return new Point((enBasAGauche.getAbscisse()+enHautADroite.getAbscisse())/2,(enBasAGauche.getOrdonnee() +enHautADroite.getOrdonnee())/2);
}
/**
* Réduction dans les quatre directions ; le centre ne bouge pas
* #param fr dans ]0..1[ facteur de réduction
*/
public void fondre(double fr) {
fr = fr/2;
double hauteur = hauteur();
double largeur = largeur();
enBasAGauche.deplacer(largeur*fr,hauteur*fr);
enHautADroite.deplacer(-largeur*fr,-hauteur*fr);
}
/**
* Casser une partie à droite
* #param fr dans ]0..1[ facteur de réduction
*/
public void casserDroite(double fr) {
fr = fr/2;
enHautADroite.deplacer(-largeur()*fr,0);
}
/**
* Casser une partie à gauche
* #param fr dans ]0..1[ facteur de réduction
*/
public void casserGauche(double fr) {
fr = fr/2;
enBasAGauche.deplacer(largeur()*fr,0);
}
/**
* Casser une partie en haut
* #param fr dans ]0..1[ facteur de réduction
*/
public void casserHaut(double fr) {
fr = fr/2;
enHautADroite.deplacer(0,-hauteur()*fr);
}
/**
* Casser une partie en bas
* #param fr dans ]0..1[ : définit le pourcentage supprimé
*/
public void casserBas(double fr) {
fr = fr/2;
enBasAGauche.deplacer(0,hauteur()*fr);
}
}
here is the error I get :
javac -classpath ../ressourcesBPO/geometrie.jar -encoding "iso-8859-1" glaces/tests/TestIceberg2D.java
glaces/tests/TestIceberg2D.java:3: error: cannot find symbol
import glaces.Iceberg2D;
^
symbol: class Iceberg2D
location: package glaces
glaces/tests/TestIceberg2D.java:9: error: cannot find symbol
Iceberg2D i1 = new Iceberg2D(new Point(2,3), new Point(6,7));
^
symbol: class Iceberg2D
location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:9: error: cannot find symbol
Iceberg2D i1 = new Iceberg2D(new Point(2,3), new Point(6,7));
^
symbol: class Iceberg2D
location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:10: error: cannot find symbol
Iceberg2D i2 = new Iceberg2D(new Point(3,7), new Point(5,9));
^
symbol: class Iceberg2D
location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:10: error: cannot find symbol
Iceberg2D i2 = new Iceberg2D(new Point(3,7), new Point(5,9));
^
symbol: class Iceberg2D
location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:58: error: cannot find symbol
Iceberg2D i3 = new Iceberg2D(i1,i2);
^
symbol: class Iceberg2D
location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:58: error: cannot find symbol
Iceberg2D i3 = new Iceberg2D(i1,i2);
^
symbol: class Iceberg2D
location: class TestIceberg2D
7 errors
This is my folder tree view
thanks in advance !!!
Ok so I found the solution but I have absolutely no idea why it works.
Here is what I typed in the terminal :
javac -classpath ../ressourcesBPO/geometrie.jar:. -encoding "iso-8859-1" glaces/tests/TestIceberg2D.java
I added :. after the .jar
Why it worked ? I have absolutely no idea so, if someone wants to explain it would be much appreciated. Thanks a lot btw for trying to help !
Since your test is using the Iceberg2D class, you will first need to compile that and then point javac to its location as part of the classpath, or compile both sources at the same time.
Based on your directory structure, the first would be (assuming you’re in the top-level java directory):
javac -cp ../ressourcesBPO/geometrie.jar -d java java/glaces/Iceberg2D.java
javac -cp ../ressourcesBPO/geometrie.jar:. -d java java/glaces/test/TestIceberg2D.java
The point is that you need to add the top-level path of your *.class files to the classpath (i.e. where Java would search for the compiled file glaces/Iceberg2d.class) — and that is the current directory (i.e. .).
However, that’s ending up mixing compiled and source files, and makes everything more complicated than necessary. A more conventional Java project structure would have this outline:
projectname/
╰── build/
╰── classes/
   ├── main/
   ╰── test/
├── lib/
│   ╰── geometrie.jar
╰── src/
├── main/
│   ╰── java/
│   ╰── glaces/
│   ╰── Iceberg2D.java
╰── test/
╰── java/
╰── glaces/
╰── TestIceberg2D.java
This simplifies the build command somewhat, and prevents cluttering your source tree:
shopt extglob # requires Bash to run!
javac -d build/classes/main -cp lib/geometrie.jar src/main/java/**/*.java
javac -d build/classes/test -cp lib/geometrie.jar:build/classes/main src/test/java/**/*.java
Furthermore, this is also the directory structure used by modern Java build systems such as Gradle. Using the latter, you could create a minimal build configuration (using gradle init) and then run gradle test to build your entire main source tree, the test source tree, and then run the tests.
A final note, I know it’s extremely common to program “in French” at University in France (been there, done the same) but I strongly recommend consistently using English when writing code. Names matter for code comprehension and mixing different languages when working with other libraries makes everything confusing. It also means that only French-speaking people can read or use your code. This means asking questions (e.g. here on Stack Overflow) gets harder, but also that you can’t usefully distribute your code once you write something cool and want to share it.

MPEG 2 TS ISOMEDIA complaint

I record audio using android AudioRecord class with success and to make it play with all players i add AAC ADTS headers with success and concatenated all in MPEG 2 TS (.ts) file and it's able to play with all players (native an VLC).
But unfortunately this file is not ISOMEDIA compliant to dashing with GPAC. I try to look at a solution and i found that i need to add PES headers to my ES stream.
please can't someone know how to add PES headers on top of ADTS headers in MPEG 2 TS using java ? on code below ?
enter code here
private void addADTStoPacket(byte[] packet, int packetLen) {
int profile = 2; //AAC LC //39=MediaCodecInfo.CodecProfileLevel.AACObjectELD;
int freqIdx = 4; //44.1KHz
int chanCfg = 2; //CPE
// fill in ADTS data
packet[0] = (byte)0xFF; // conversion hexadecimal a decimal - il y a seize unités de 0 à F, on parle donc d'hexadécimal.
packet[1] = (byte)0xF1; // installe l'entete ADTS dans MPEG-2 (0xF1) au lieu de MPEG-4 (0xF9)
packet[2] = (byte)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
packet[3] = (byte)(((chanCfg&3)<<6) + (packetLen>>11));
packet[4] = (byte)((packetLen&0x7FF) >> 3);
packet[5] = (byte)(((packetLen&7)<<5) + 0x1F);
packet[6] = (byte)0xFC; // 0xFC est également correct si vous ne connaissez pas la valeur de la plénitude du tampon
}

Java scanner.nextDouble()

Hi I am just learning java with a book; in this, there is an excersise called loan; I am following the sample, but when I want to test the program after I type the loan amount named by me as montoPrestamo (in Spanish) but when I type any value, netBeans does not nothing, no error, no exception, no next prompt, nothing.
I dont know what is going on or where is the mistake.
thanks
here is my code:
public static void main(String[] args) {
double montoPrestamo, interesAnual,pagoMensual,pagoTotal;
int tiempoPrestamo;
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter(System.getProperty("line.separator"));
System.out.print("Monto del prestamo (Pesos y Centavos): ");
montoPrestamo = scanner.nextDouble();
System.out.print("el valor ingresado es " + montoPrestamo);
System.out.print("Tasa de Interes anual (ejemplo, 9,5): ");
interesAnual=scanner.nextDouble();
System.out.print("Tiempo de periodo del prestamo en años : ");
tiempoPrestamo=scanner.nextInt();
System.out.println("");
System.out.println("Cantidad solicitada $"+montoPrestamo);
System.out.println("la tasa de interes de su prestamo:"+interesAnual+"%");
System.out.println("Tiempo del prestamo en años"+tiempoPrestamo);
System.out.println("\n");
System.out.println("Pago mensual"+pagoMensual);
System.out.println("Pago total"+pagoTotal);
}
It works after removing this line:
scanner.useDelimiter(System.getProperty("line.separator"));
Probably this does not work from within an IDE because it uses an own terminal which might use another linefeed separator than your operating system.

How can I reset a if-command? Or jump back to the point before the if-command was executed?

So yesterday I asked a question about some GUI-ing. I completly threw that over, since I found it a little to complicated for me to actually deal with it.
Now I am reworking the thing in the console.
And I got myself stuck again. My problem this time: How can I jump back to a point before a if-command was executed?
Direct example:
import java.util.Scanner;
import java.io.*;
public class HBA {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Herzlichen Glückwunsch Anna! Und viel Spaß mit deinem Geschenk!") ;
System.out.println("Neben diesem Programm befindet sich eine Passwort gesicherte Datei, die einen weiteren Teil deines Geschenks enthällt."
+ "Um an das Passwort zu gelangen wirst du jedoch ein paar ganz besonders schwierige Fragen beantworten müssen!"
+ "Wenn du bereit für das Quiz bist, gib in die Eingabe: 'ok' ein.");
String OK, Q1, Q2, Q3, Q4, Q5, Q6, Q7;
BufferedReader repo = null;
OK = scan.next();
if (OK == "ok") {
System.out.println("Alles gut, fangen wir mit etwas leichtem an!");
}
else {
System.out.println("Wie... bei so einer einfachen Sache versagst du schon? Versuchs nochmal!");
}
System.out.println("Frage 1: Wer ist Supergeil? \n A: Erik \n B: Anna \n C: The J \n D: Friedrich Liechtenstein");
mark(0);
Q1 = scan.next();
if (Q1 == "D") {
System.out.println("Richtig! Der erste Buchstabe lautet: S");
}
else {
System.out.println("Leider falsch. Versuch es nochmal.");
reset();
}
}
}
The scripted works as expected, besides: If you type something wrong in the last part:
System.out.println("Frage 1: Wer ist Supergeil? \n A: Erik \n B: Anna \n C: The J \n D: Friedrich Liechtenstein");
mark(0);
Q1 = scan.next();
if (Q1 == "D") {
System.out.println("Richtig! Der erste Buchstabe lautet: S");
}
else {
System.out.println("Leider falsch. Versuch es nochmal.");
reset();
}
}
}
It just ends the script. Instead it should jump back to the beginning of the if-command.
Means: The answer to the question in the System.out.printLn (it is a question) is D and you typ A instead, it should reset the whole thing that you can try it again and answer something different. How can I achieve that? I read that BufferedReader have a mark() and reset() function, but I don't know if they work the way I expect them to or how I would have to integrate them.
I also thought about using a while or a do command. But I haven't found a way for that yet.
Can someone pls enlighten me?
Thanks!

Why do I get class, enum, interface expected?

/**
* #(#)b.java
*
*
* #author
* #version 1.00 2012/5/4
*/
import java.util.*;
import java.io.*;
import java.*;
public class b {
static void lireBddParcs(String nomFichier) throws IOException
{
LinkedHashMap parcMap = new LinkedHashMap<Parc,Collection<Manege>> ();
boolean existeFichier = true;
FileReader fr = null;
try
{
fr = new FileReader (nomFichier);
}
catch(java.io.FileNotFoundException erreur)
{
System.out.println("Probleme rencontree a l'ouverture du fichier" + nomFichier);
existeFichier = false;
}
if (existeFichier)
{
Scanner scan = new Scanner(new File(nomFichier));
while (scan.hasNextLine())
{
String[] line = scan.nextLine().split("\t");
Parc p = new Parc(line[0], line[1], line[2]);
parcMap.put(p, null);
}
}
scan.close();
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException
{
lireBddParcs("parcs.txt");
}
}
parc.txt contains:
Great America Chicago Illinois
Magic mountain Los Ageles Californie
Six Flags over Georgia Atlanta Georgie
Darien Lake Buffalo New York
La Ronde Montreal Quebec
The Great Escape Lake Georges New York
Six Flags New Orleans New Orleans Louisiane
Elitch Gardens Denver Colorado
Six Flags over Texas Arlington Texas
Six Flags New England Springfield Massachusetts
Six Flags America Washington D.C.
Great Adventure Jackson New Jersey
error: class, interface, or enum expected line 94
error: class, interface, or enum expected line 99
I decided to change my code, because something didn't work out as expected, but now I am getting this. Can't get through the compilation. Any idea why it doesn't work? I am a complete noob about to abandon my java course.
Although indentation is confusing, the main method is outside of the class while it should be inside of it.
It also make the line scan.close(); invalid, as scan is not defined there. remove the } before scan.close();.
It's just because there's an extraneous closing brace in your first method here:
}
scan.close();
If you use an IDE such as eclipse or netbeans to edit your source files, it will help a lot with automatic brace matching and highlighting these kinds of errors.

Categories

Resources