Defining A Class In Source Code File?
Oct 9, 2014
"If you define a public class or an interface in your class, then it should match the name of the java source code file."
Does the statement above mean the following should compile fine ?
public class Animal{
public class Animal{ }
}
The author gives an example saying that if you have a class Multiple.java and in that class, you have:
public class Multiple{}, then it should compile.
But this does not compile. I tried it.
View Replies
ADVERTISEMENT
Mar 30, 2014
To be specific, how can I use enumeration of one source-code file in another.
View Replies
View Related
Feb 3, 2014
Why is this not valid in java:
Both uses public with the same class/interface name.
Test.java:
public class Test implements Test{
/// Some codes
}
public interface Test {
///Some methods
}
View Replies
View Related
Oct 31, 2014
I am trying to complete this question. I understand the most of it but I haven't go a clue to read in the file name.
Full question: Implement a program that reads in a Java source code file and checks to see if it has balanced {}brackets. Your program should use a stack, implemented as a linked list, to check the brackets.
NOTE: you can use a reference called top which points to the head of the list. Your program should run as a command line program and should take a filename as an argument and print one of BALANCED or NOT BALANCED.
For example: c:> java checkBalanced "myProgram.java" BALANCED
View Replies
View Related
Nov 3, 2014
I am having hard time to grasp the concept of java as i am beginner. according to different sources found in internet, only one class is written in one source file. and all those class can be accessed through the main class.
my question is
1.can we access one class present in one source file, through another class present in another source file [not through the class containing main method]?
2.can we create more than 1 class in same source file?is there special way to do it? i do get error always when i try to do so
3.can multiple classes contain main method? or should there be only single class containing it?
View Replies
View Related
Oct 20, 2014
I have a question about the following snippet concerning the steps the javac compiler follows to compile a program:
[...]at first, searching a class within a package is discussed if the latter doesn't contain a full package name[...]
It is a compile-time error if more than one class is found. (Classes must be unique, so the order of the import statements doesn't matter.)
The compiler goes one step further. It looks at the source files to see if the source is newer than the class file. If so, the source file is recompiled automatically. Recall that you can import only public classes from other packages. A source file can only contain one public class, and the names of the file and the public class must match. Therefore, the compiler can easily locate source files for public classes.
However, you can import nonpublic classes from the current package. These classes may be defined in source files with different names. If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class. I don't quite understand the red fragment. I wondered if the word "import" nonpublic classes from the current package weren't a synonym for the word "use", since why would we want to import a class from the same package when compiler searches the current package automatically anyway?
However I wanted to test nonpublic classes that are contained in source file which name doesn't match the class name:
NonpublicClass.java:
Java Code:
package com.work.company;
class NonpublicClass
{
public void description() {
System.out.println("Working!");
}
} mh_sh_highlight_all('java');
[Code] ....
Everything's fine when the source file names are the same as above. However, when I change NonpublicClass.java to a different name, there's an error "cannot find symbol" in:
Java Code: NonpublicClass v = new NonpublicClass(); mh_sh_highlight_all('java');
I noticed that the class file for NonpublicClass isn't even generated so that's probably the cause. If I change to the directory of the package the NonpublicClass is contained in and compile it directly, i.e. issue for example:
Java Code: javac NonpublicClass_different_name.java mh_sh_highlight_all('java');
Then a proper class file is generated with the class name, that is NonpublicClass.class, and afterwards I can compile the main program in Start.java.
So the question is what am I missing regarding the cited quote that reads:
If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class.
? Because adding such an import to CompanyClass.java:
import com.work.company.NonpublicClass;
didn't work either...
View Replies
View Related
Jul 11, 2014
1.Is there any way by which i can see what is happening inside pre defined methods.
i.e. can i see there source code?
2.Somewhere i learned how to see the content of a pre defined class i.e. by using:
javap java.io.classname
but i dont know what javap means!
View Replies
View Related
Oct 19, 2014
I'm doing homework and as far as input, my assignment reads: "Your program must take as input the name of a Java source code file such as the source file containing the source code of this assignment." So my question is, how do i do that without linking the directory directly (i.e. C:/users/...)? I'm using FileReader as shown below...
public static void main(String[] args) throws Exception {
FileReader file = new FileReader (WHAT GOES HERE???);
BufferedReader reader = new BufferedReader(file);
String s = "";
String line = reader.readLine();
[Code] .....
View Replies
View Related
Apr 8, 2015
I want to extract a specific String from HTML, specifically, I want to extract a String from in between <...>
So far, I've got this
package main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class HTMLGrabber {
static String allOneString = "";
[code]....
The problem I have is when I change the last parameter in this line:
System.out.println("And the Keywords are:
" +allOneString.substring(allOneString.lastIndexOf("meta name="keywords" content=") + 30, allOneString.indexOf("Fictional History">")+17));
to
System.out.println("And the Keywords are:
" +allOneString.substring(allOneString.lastIndexOf("meta name="keywords" content=") + 30, allOneString.indexOf("">")));
i.e. the generic alternative, I get this error message:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -366
at java.lang.String.substring(Unknown Source)
at main.HTMLGrabber.main(HTMLGrabber.java:45)
Is there a better and simple way to extract a substring?
View Replies
View Related
Oct 7, 2014
Lately I've been working with JDBC and writing queries in some Java programs. I've noticed that I keep experimenting with where I place my blocks of querying code. I've tried creating a private method in the class where the query is used, I've created utility classes where I can call the query when needed, and sometimes I've just put it in a localized block of code where I need to. Also, most often these queries are one-shot queries where I need to pull the data to populate a JTable.
In short, this has made me realize that I don't have a solid practice for organizing my queries in Java. So my question is "Is there a practice for organizing query code?".
View Replies
View Related
Sep 5, 2014
I am developing an inhouse project in my organization. I unable to implement the pdf uploading, downloading, report generation logic as i am totally new to this implementation.
View Replies
View Related
Feb 19, 2014
I have been given a task to scan the contents of a websites source code, and use delimiters to extract all hyperlinks from the site and display them. We havent been told anything about how to do this so after some looking around online this is what I have so far:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
[Code] ....
So my program can extract each line from the source code of a website and display it, but realistically I want it to extract each WORD as such from the source code rather than every line. I've looked around online but I don't really know how it's done because I keep getting errors when I use input.read();
How to make it extract each word from the source code?
View Replies
View Related
Feb 21, 2015
How to deploy Java Swing application as windows software. I have tried the following to do this :
I have created jar file for my project after that created exe file using Launch4j and Advanced installer, exe file produced by both software are working fine but those are placing the jar file inside that exe file, My problem is any one who has that exe file can extract and view the source code by decompiling java class file. Is there any way to deploy java application without decompiling the code.
View Replies
View Related
Apr 29, 2015
I am operating java from the command line (using Terminal on Mac OSX 10.9.5).The current directory within Terminal is called "orange" and the following sourcecode file is in the orange directory :
Test1.java
package orange;
public class Test1 {
public static void main(String[] args) {
System.out.println("Test1 works");
}
}
This complies to Test1.class (visible in the orange directory) but when I try to run it in Terminal (simply using "java Test1" from within the orange directory). I get the following error message (which I don't get if I simply comment out the line //package orange;)
Exception in thread "main" java.lang.NoClassDefFoundError: Test1 (wrong name: orange/Test1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
[code]...
View Replies
View Related
Apr 3, 2014
The problem is what to do when the source file for a class gets too big ( a judgment call for sure). Is it possible for one class to be defined in multiple files and if so how do you do it, and is it good practice?
I do see ways to refactor into a base class or move code into a helper class but sometimes the abstraction is cleaner as a single class that does a lot of stuff. My problem is how to organize a BIG class?
View Replies
View Related
Jun 13, 2014
This is a snippet code that im developing. (no source of it) . I am facing a trouble to extract file in the same directory as the source file (example: extract here option)
public class HelperAction {
public static void unzip(String zipFilePath)throws IOException {
File dir = new File(zipFilePath.substring(0,zipFilePath.length()));
// create output directory if it doesn't exist
if(!dir.exists()) dir.mkdirs();
FileInputStream fis;
[Code] ....
View Replies
View Related
Jul 5, 2014
I have a file(source.txt) that contains some texts. Now, the java code is to run through each line in the source file and check if that line begins with a number and if true, copy that line to the destination file(destination.txt). I have the code but I am not sure why I can't loop through each line in the source.txt file. Below is my java code:
public static void copyFile(File source, File destination) {
BufferedReader br;
try {
FileWriter fw = new FileWriter(destination.getAbsoluteFile());
PrintWriter bw = new PrintWriter(fw);
br = new BufferedReader(new FileReader(source));
String line = new String();
[Code] ....
As shown in the source.txt file, there are 4 lines in the file but when the execution gets to while ((line = br.readLine()) != null) , it executes to false.. Why this is so?
below is the source.txt file
12 is a number
Green is bad
5 is not so cool
you are right
View Replies
View Related
May 27, 2014
I want to define this data structure , I show you an example
"first street",{{1,40},{43,45},{34,26}},
"second street",{{14,41},{42,5},{3,46}},
............
I wrote in this way but it doesnt work :
Map<String,ArrayList<int[]>> Street=new HashMap<String, ArrayList<int[2]>>();
View Replies
View Related
Aug 30, 2014
Have the following .bat file that compiles the ReadMindWave.java source with jna and jfreechart. I get a javac error saying 'no source' when the .bat is run, as the .java source needs to be included in the .bat file. Where it goes in the following .bat file?
@echo off
echo Compiling %1 with JNA and JFreeChart...
javac -Xlint:deprecation -cp "C:jna-master*;C:jfreechart-1.0.19libjfreechart-1.0.19.jar;C:jfreechart-1.0.19libjcommon-1.0.23.jar;."%*
The file to compile is in C:MindWaveTestsReadMindWave.java
View Replies
View Related
Apr 13, 2015
I am making a gui in which i read values from text file to JTable. But i want to define the column header as A,B,C,D,E,F,G,H,I. Now when i read, the values from text file are getting displayed in Jtable headers. How can i achieve the column header as A,B,C,D,E,F,G,H,I.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class Bb extends JFrame {
[Code] ......
Text file from where it reads values: bbb.txt
1 2 1 1 2 1 1.83e-02 -1.0 3
1 1 1 1 1 1 1.89e-02 -1.0 2
0 0 0 0 0 0 0 0 0
1 2 1 1 2 1 1.83e-02 -1.0 3
1 1 1 1 1 1 1.83e-02 -1.0 2
View Replies
View Related
Mar 23, 2014
I am building a room management system in Java (Netbeans) and Ms Access. The jasper report is only showing code (source view) instead of design view. The GUI JFrame and Login JFrame are not displaying records in the database and the buttons are not working. Also the labels and text fields are not neatly arranged. The connection to the database is not displaying records.
Connection to the database. COIRMS.java
package Login;
import java.sql.*;
public class COIRMS {
Connection con;
Statement st;
ResultSet rs;
public COIRMS ()
[Code] ....
View Replies
View Related
Apr 23, 2015
I'm having another issue. I have 2 java source files(see below). They are place in the same directory. How do I compile them using classpath?
I have already tried 1st attempt :
javac -cp com.companyname.interview.DuplicateReplace.java DuplicateReplaceTest.java [did not work!]
2nd attempt: javac -cp DuplicateReplace.java DuplicateReplaceTest.java [again, did not work!]
package com.companyname.interview;
public class DuplicateReplace { /* code */}
[Code] ....
View Replies
View Related
Jun 8, 2014
how to add an error message when my program can't find the file the user inputs.
For example:
Enter your source code:
D:Data StructsProjectssrcHelloWorld (I didn't put .java on purpose)
Exception in thread "main" java.io.FileNotFoundException: D:Data StructsProjectsAssignment 3srcHelloWorld (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at HomeworkDriverFour.main(HomeworkDriverFour.java:14)
//Can I get rid of the exception above and replace it with "File not found!"
Enter your source code:This is what I have so far:
import java.io.*;
import java.util.*;
public class HomeworkDriverFour {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
[code]...
View Replies
View Related
Jan 22, 2015
when i try to compile following source file, there is a error which states "MovieTestDrive is public, so must be declared in a file MovieTestDrive.java" i didnot understand. i am beginner in java;
class Movie
{
String title;
String genre;
int rating;
void playit()
{
System.out.println("playing the movie");
}
}
public class MovieTestDrive
[code]....
View Replies
View Related
Nov 6, 2014
My goals:
1) Have some source file be read in
2) Specify what arithmetic operators to swap (+, -, /, *)
2) If an arithmetic operator is read (like a + sign etc) then we swap it with its opposite (- for example)
3) Once the swap is complete, the rest of the file stays the same even if more operators are in the file...it is then output to a file (I am going with 1mutation.java)
4) This is where it gets tricky....it then picks up where it left off to finish reading the + operators (or whatever was specified) and repeats steps 2-3 (but the operator that is already swapped gets left as it was / skipped) and the output is saved as 2mutation.java.
The most I have been able to manage is having it changed 1 operator or all of them at once. I deleted a lot of my work to start fresh / master one operator for the time being. Here is what I have:
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
public class OperatorSub {
[Code] ....
This is the data file I am using. (see attached) The file should be .java or .cpp but I stuck with .txt for now. How to tackle this? Am I on the right track?
View Replies
View Related
Jan 25, 2014
My problem is that I want to load an Image from the source directory in Canavas' paint() method.
My code is currently (in a try/catch)
g.drawImage(ImageIO.read(new File(imageLocation)), imageX, imageY, null);
Where imageLocation leads to an image on my HDD ("C:UsersVladdeDocumentsFolders-Filesfolder.png")
View Replies
View Related