May 02, 2024, 09:28:22 PM

1,531,645 Posts in 46,729 Topics by 1,523 Members
› View the most recent posts on the forum.


This is why I hate Java

Started by Daddy, October 01, 2009, 10:32:39 PM

previous topic - next topic

0 Members and 1 Guest are viewing this topic.

Go Down

Daddy

[spoiler]
Code Select
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class LineNum extends JFrame implements ActionListener{
public final int frameWidth = 400;
public final int frameHeight = 200;
private JTextField file = new JTextField(20);
private JTextField data = new JTextField(10);
private JButton quit = new JButton("Quit");
private JButton makeFile = new JButton("Create File");
private boolean fileOpen = false;
private FileOutputStream out;
private PrintWriter output;

public static void main(String [] args){
LineNum gui = new LineNum();
}
//Constructor
public LineNum(){
setSize(frameWidth, frameHeight);
Container window = getContentPane();
window.setLayout(new GridLayout(4,2));
window.add(new JLabel("Filename:"));
window.add(file);
window.add(new JLabel("Number of Lines:"));
window.add(data);
window.add(new JLabel("   "));
window.add(new JLabel("   "));
window.add(quit);
window.add(makeFile);
setVisible(true);
makeFile.addActionListener(this);
quit.addActionListener(this);
file.addActionListener(this);
data.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == quit){
if(fileOpen){output.close();}
System.exit(0);
}
else if(event.getSource()==makeFile){
if(fileOpen){
output.close();
}
try {
out = new FileOutputStream(file.getText());
}catch(FileNotFoundException exception){
System.out.println("Could not open file.");
System.exit(1);
}
fileOpen = true;
output = new PrintWriter(out);
int largeDiv = 1;
for(int i=1;i<Integer.parseInt(data.getText())+1;i++){
for(int j=1;j<i;j++){
if(i%j==0){
// output.printf("%d   %d\n", i, j);
largeDiv=j;
}
}
output.printf("%d   %d\n", i, largeDiv);
}output.close();

}
}
}
[/spoiler]


Here is the same code(sans a popup window) in python:
[spoiler]
Code Select
#!/usr/bin/python
#Because python is far less verbose than Java
strYo = ''
largeDiv = 1
for i in range(1, 101):
for j in range(1, i):
if not i%j: largeDiv = j
strYo+=str(i) + "    " + str(largeDiv)+"\n"
open('/Users/jamesvalente/Desktop/pythonisbetter', 'w').write(strYo) 
print strYo
[/spoiler]

Thyme


FAMY2


the shortest route to the sea

Um, note: code text box is borked?

Or is that the point of the thread n_u

Quote from: Socks on January 03, 2011, 09:56:24 PM
pompous talk for my eyes water and quiver with a twitch like a little bitch

Dullahan

Yeah, I hate Java too.

What exactly are you trying to code?

Daddy

Quote from: The Tricker on October 04, 2009, 04:22:59 AM
Yeah, I hate Java too.

What exactly are you trying to code?
It takesssssssssssssssss aaaaaa lisssssstttt annddddd finddddddssssssssss thhhheeee laaaaaarrrrrggggeeeessssssttt eeeeeevvvvvvvveeeeeeeeeeennnnnnn diiiiiivvvisssssssorrrrr

Dullahan

Quote from: Ensign Halibar on October 04, 2009, 05:00:01 AM
It takesssssssssssssssss aaaaaa lisssssstttt annddddd finddddddssssssssss thhhheeee laaaaaarrrrrggggeeeessssssttt eeeeeevvvvvvvveeeeeeeeeeennnnnnn diiiiiivvvisssssssorrrrr
just lettin you know i'm a terrible programmer

Daddy

Quote from: The Tricker on October 04, 2009, 06:44:10 AM
just lettin you know i'm a terrible programmer
I didn't really use comments so it's not really your fault.

Go Up