Many years ago when I was at uni they said to put a capital i (I) in front of interfaces. Is this still a convention because I see many interfaces that do not follow this.
I'm trying to get one line of source from a website and then I'm returning that line back to main. I keep on getting an error at the line where I define InputStream in. Why am I getting an error at this line?
public class MP3LinkRetriever
{
private static String line;
public static void main(String[] args)
{
String link = "www.google.com";
String line = "";
while (link != "")
{
link = JOptionPane.showInputDialog("Please enter the link");
try
{
line = Connect(link);
}
catch(Exception e)
{
}
JOptionPane.showMessageDialog(null, "MP3 Link: " + parseLine(line));
String text = line;
Toolkit.getDefaultToolkit( ).getSystemClipboard()
.setContents(new StringSelection(text), new ClipboardOwner()
{
public void lostOwnership(Clipboard c, Transferable t) { }
});
JOptionPane.showMessageDialog(null, "Link copied to your clipboard");
}
}
public static String Connect(String link) throws Exception {
String strLine = null;
InputStream in = null;
try {
URL url = new URL(link);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(uc.getInputStream());
Reader re = new InputStreamReader(in);
BufferedReader r = new BufferedReader(re);
int index = -1;
while ((strLine = r.readLine()) != null && index == -1) {
index = strLine.indexOf("<source src");
}
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return strLine;
}
public static String parseLine(String line)
{
line = line.replace("<source", "");
line = line.replace(" src=", "");
line = line.replace("\"", "");
line = line.replace("type=", "");
line = line.replace("audio/mpeg", "");
line = line.replace(">", "");
return line;
}
}
So I have a string, and I want to strip out some parts of it using, for example, the firt and last characters of the "interesting" part.
String dirty = "$!$!%!%$something interesting&!!$!%$something interesting2";
And the output something like:
String clean = "something interesting:something interesting2";
Note:
The code needs to work without knowing the random part, changing everytime the program runs.
I researched and only found code that does it, but only knowing the random segment.
Hello Internet !
I'm having trouble with doubling up on my code for no reason other than my own lack of ability to do it more efficiently...
`for (Method curr: all){
if (curr.isAnnotationPresent(anno)){
if (anno == Pre.class){
for (String str : curr.getAnnotation(Pre.class).value()){
if (str.equals(method.getName()) && curr.getReturnType() == boolean.class && curr.getParameterTypes().length == 0){
toRun.add(curr);
}
}
} if (anno == Post.class) {
for (String str : curr.getAnnotation(Post.class).value()){
if (str.equals(method.getName()) && curr.getReturnType() == boolean.class && curr.getParameterTypes().length == 0){
toRun.add(curr);
}
}
}
}
}`
anno is a parameter - Class, and Pre and Post are my annotations, both have a value() which is an array of strings.
Of course, this is all due to the fact that i let Eclipse auto fill code that i don't understand yet.
Hi
I'm not too sure how to go about getting the external IP address of the machine as a computer outside of a network would see it. My following IPAddress class only gets the local IP address of the machine.
Any help would be appreciated.
Thanks.
public class IPAddress {
private InetAddress thisIp;
private String thisIpAddress;
private void setIpAdd(){
try{
InetAddress thisIp = InetAddress.getLocalHost();
thisIpAddress = thisIp.getHostAddress().toString();
}
catch(Exception e){}
}
protected String getIpAddress(){
setIpAdd();
return thisIpAddress;
}
}
I'm trying to declare a method for my program that takes only a 5 digit integer and for each digit of the integer, reads a value from the program and prints it out. I understand this isn't very clear but im having trouble relaying what I mean. I understand it will be some sort of for loop to read each digit of the integer individually until something reaches 5. Something like the charAt() string method but works for digits.
Can anyone explain:
Why the two patterns used below give different results? (answered below)
Why the 2nd example gives a group count of 1 but says the start
and end of group 1 is -1?
public void testGroups() throws Exception
{
String TEST_STRING = "After Yes is group 1 End";
{
Pattern p;
Matcher m;
String pattern="(?:Yes|No)(.*)End";
p=Pattern.compile(pattern);
m=p.matcher(TEST_STRING);
boolean f=m.find();
int count=m.groupCount();
int start=m.start(1);
int end=m.end(1);
System.out.println("Pattern=" + pattern + "\t Found=" + f + " Group count=" + count +
" Start of group 1=" + start + " End of group 1=" + end );
}
{
Pattern p;
Matcher m;
String pattern="(?:Yes)|(?:No)(.*)End";
p=Pattern.compile(pattern);
m=p.matcher(TEST_STRING);
boolean f=m.find();
int count=m.groupCount();
int start=m.start(1);
int end=m.end(1);
System.out.println("Pattern=" + pattern + "\t Found=" + f + " Group count=" + count +
" Start of group 1=" + start + " End of group 1=" + end );
}
}
Which gives the following output:
Pattern=(?:Yes|No)(.*)End Found=true Group count=1 Start of group 1=9 End of group 1=21
Pattern=(?:Yes)|(?:No)(.*)End Found=true Group count=1 Start of group 1=-1 End of group 1=-1
What is the elegant way to convert JSONObject to URL parameters.
For example, JSONObject:
{stat: {123456: {x: 1, y: 2}, 123457: {z: 5, y: 2}}}}
this should be like:
stat[123456][x]=1&stat[123456][y]=2&stat[123457][z]=5&stat[123457][y]=2
of course with escaped symbols, and of course JSON object could be more complicated..
Maybe there already exist some mechanisms for that?
Thanks,
For a project, I have to convert a binary string into (an array of) bytes and write it out to a file in binary.
Say that I have a sentence converted into a code string using a huffman encoding. For example, if the sentence was: "hello" h = 00 e = 01, l = 10, o = 11
Then the string representation would be 0001101011.
How would I convert that into a byte? <-- If that question doesn't make sense it's because I know little about bits/byte bitwise shifting and all that has to do with manipulating 1's and 0's.
Can anyone help in architecture design of one of my complex application.
Requirement :
In web based application, we need to generate Excel kind of report as HTML page
and after that we need to perform different kinds of operations like
Add manual rows
Delete rows
Edit rows
adding comments based on each cell
viewing the added comments.
attaching the file based on each cell
viewing the attached file.
Collapsible functionality for some of rows
In the process of design we have come up with DB design and application framework is Spring.
and for Web not yet finalized.
what is the best approach to implement this kind of UI?
--JSF?(keep in mind we need to Excel operations like above mentioned operations)
-- Any reporting tool which will provide editing functionality?
Please suggest me How can we do it? and what is the best technology for it? or is there any reporting tools?
I came across the following program and it behaving in unexpected manner.
public class ShiftProgram
{
public static void main(String[] args)
{
int i = 0;
while(-1 << i != 0)
i++;
System.out.println(i);
}
}
If we think about this program output, when it reaches 32 while loop condition should return false and terminate and it should print 32.
If you ran this program, it does not print anything but goes into an infinite loop. Any idea whats going on? Thank you in advance.
Hello,
I configure my web application to use SSL using my own self signed certificate. Everything is working fine but here my whole site is https now as i used :-
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
However, i only want my login page to use SSL and not complete site. What changes do i need to make in my application?
Thanks in advance :)
Creating a JApplet I have 2 Text Fields, a button and a Text Area.
private JPanel addressEntryPanel = new JPanel(new GridLayout(1,3));
private JPanel outputPanel = new JPanel(new GridLayout(1,1));
private JTextField serverTf = new JTextField("");
private JTextField pageTf = new JTextField("");
private JTextArea outputTa = new JTextArea();
private JButton connectBt = new JButton("Connect");
private JScrollPane outputSp = new JScrollPane(outputTa);
public void init()
{
setSize(500,500);
setLayout(new GridLayout(3,1));
add(addressEntryPanel);
addressEntryPanel.add(serverTf);
addressEntryPanel.add(pageTf);
addressEntryPanel.add(connectBt);
addressEntryPanel.setPreferredSize(new Dimension(50,50));
addressEntryPanel.setMaximumSize(addressEntryPanel.getPreferredSize());
addressEntryPanel.setMinimumSize(addressEntryPanel.getPreferredSize());
add(outputPanel);
outputPanel.add(outputSp);
outputTa.setLineWrap(true);
connectBt.addActionListener(this);
The problem is when debugging and putting it in a page the components / panels resize depending on the applet size. I don't want this. I want the textfields to be a certain size, and the text area to be a certain size. I've put stuff in there to set the size of them but they aren't working. How do I go about actually setting a strict size for either the components or the JPanel.
Hi,
Is there any keyword or design pattern for doing this?
public abstract class Root
{
public abstract void foo();
}
public abstract class SubClass extends Root
{
public void foo()
{
// Do something
}
}
public class SubberClass extends SubClass
{
// Here is it not necessary to override foo()
// So is there a way to make this necessary?
// A way to obligate the developer make again the override
}
Thanks
Assign the following 25 scores to a one dimensional int array called "temp"
34,24,78,65,45,100,90,97,56,89,78,98,74,90,98,24,45,76,89,54,12,20,22,55,66
Move the scores to a 2 dimensional int array called "scores" row wise
-- meaning the first 5 scores go into row 0 etc
I am writing a genetic algorithm that approximates an image with a polygon. While going through the different generations, I'd like to output the progress to a JFrame. However, it seems like the JFrame waits until the GA's while loop finishes to display something. I don't believe it's a problem like repainting, since it eventually does display everything once the while loop exits. I want to GUI to update dynamically even when the while loop is running.
Here is my code:
while (some conditions) {
//do some other stuff
gui.displayPolygon(best);
gui.displayFitness(fitness);
gui.setVisible(true);
}
public void displayPolygon(Polygon poly) {
BufferedImage bpoly = ImageProcessor.createImageFromPoly(poly);
ImageProcessor.displayImage(bpoly, polyPanel);
this.setVisible(true);
}
public static void displayImage(BufferedImage bimg, JPanel panel) {
panel.removeAll();
panel.setBounds(0, 0, bimg.getWidth(), bimg.getHeight());
JImagePanel innerPanel = new JImagePanel(bimg, 25, 25);
panel.add(innerPanel);
innerPanel.setLocation(25, 25);
innerPanel.setVisible(true);
panel.setVisible(true);
}
I am looking for something very close to an application server with these features:
it should handle a series of threads/daemons, allowing the user to start-stop-reload each one without affecting the others
it should keep libraries separated between different threads/daemons
it should allow to share some libraries
Currently we have some legacy code reinventing the wheel... and not a perflectly round-shaped one at that!
I thought to use Tomcat, but I don't need a web server, except maybe for the simple backoffice user interface (/manager/html).
Any suggestion? Is there a non-web application server, or is there a better alternative to Tomcat (more lightweight, for example, or easier to configure)? Thanks in advance.
Hi,
I need to decide which configuration framework to use. At the moment I am thinking between using properties files and XML files. My configuration needs to have some primitive grouping, e.g. in XML format would be something like:
<configuration>
<group name="abc">
<param1>value1</param1>
<param2>value2</param2>
</group>
<group name="def">
<param3>value3</param3>
<param4>value4</param4>
</group>
</configuration>
or a properties file (something similar to log4j.properties):
group.abc.param1 = value1
group.abc.param2 = value2
group.def.param3 = value3
group.def.param4 = value4
I need bi-directional (read and write) configuration library/framework. Nice feature would be - that I could read out somehow different configuration groups as different objects, so I could later pass them to different places, e.g. - reading everything what belongs to group "abc" as one object and "def" as another. If that is not possible I can always split single configuration object into smaller ones myself in the application initialization part of course.
Which framework would best fit for me?
private static char[] quicksort (char[] array , int left , int right) {
if (left < right) {
int p = partition(array , left, right);
quicksort(array, left, p - 1 );
quicksort(array, p + 1 , right);
}
for (char i : array)
System.out.print(i + ” ”);
System.out.println();
return array;
}
private static int partition(char[] a, int left, int right) {
char p = a[left];
int l = left + 1, r = right;
while (l < r) {
while (l < right && a[l] < p) l++;
while (r > left && a[r] >= p) r--;
if (l < r) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
}
a[left] = a[r];
a[r] = p;
return r;
}
}
hi guys just a quick question regarding the above coding, i know that the above coding returns the following
B I G C O M P U T E R
B C E G I M P U T O R
B C E G I M P U T O R
B C E G I M P U T O R
B C E G I M P U T O R
B C E G I M O P T U R
B C E G I M O P R T U
B C E G I M O P R T U
B C E G I M O P R T U
B C E G I M O P R T U
B C E G I M O P R T U
B C E G I M O P R T U
B C E G I M O P R T U
when the sequence BIGCOMPUTER is used but my question is can someone explain to me what is happening in the code and how?
i know abit about the quick-sort algorithm but it doesnt seem to be the same in the above example.
See following code with resides in fillowing directory
mypack.pack1
package mypack.pack1;
public class myclass
{
public static void main(String args[])
{
System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
}
}
See following screen shot. that is giving error.
And i dont want to add anything in class path because i am in particular directory and it has to work.
why it is not working.??????
I want to add a JComboBox in Swing that is simple but I want to assign the values for each items in combo. I have the following code
JComboBox jc1= new JComboBox();
jc1.addItem("a");
jc1.addItem("b");
jc1.addItem("c");
Now what I want is that when click on combo box it should return 1, 2 and 3 correspondingly
instead of a ,b, c.
Is there any way to assign the key values for each items in combo box?
Question 2. USE THE FOR LOOP.
Design and write an algorithm that will read a single positive number from the keyboard and will then print a pyramid out on the screen. The pyramid will need to be of a height equal in lines to the number inputted by the operator. Your program is not to test for negative numbers, nor is it to cater for them. For your test, use the number 7. If you would like to take the problem further, try 18 and watch what happens.
Example input:
4
Example output:
1
121
12321
1234321
My current code needs to read foreign characters from the web, currently my solution works but it is very slow, since it read char by char using InputStreamReader. Is there anyway to speed it up and also get the job done?
// Pull content stream from response
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
StringBuilder contents = new StringBuilder();
int ch;
InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
// FileInputStream file = new InputStream(is);
while( (ch = isr.read()) != -1)
contents.append((char)ch);
String encode = isr.getEncoding();
return contents.toString();