Java programming

Discuss software development issues here.

Java programming

Postby relet » 29 Apr 2015, 07:48

If someone can help,
I cannot pass this simple piece in javac:

import java.awt.*;
import java.awt.event.*;
public class Welcome{
public static void main(String args[]){
System.out.printf("%s\n%s\n", "Welcome to Java Programming", " and this is my first program.");
}//end; {main}.
}//end.

javac Welcome.java
----------
1. ERROR in Welcome.java (at line 6)
System.out.printf("%s\n%s\n", "Welcome to Java Programming", " and this is my first program.");
^^^^^^
The method printf(String, String, String) is undefined for the type PrintStream

However, I passed the compiler and java the following more complex piece smoothly, strange?

import java.awt.*;
import java.awt.event.*;
public class RedRect extends Frame{
public static void main(String[] args){new RedRect();}
RedRect(){
super("RedRect");
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
}
);
setSize(200, 100);
add("Center", new CvRedRect());
show();
}//end; {RedRect}.
}//end; {main}.
class CvRedRect extends Canvas{
public void paint(Graphics g){
Dimension d=getSize();
int maxX=d.width-1, maxY=d.height-1;
g.drawString("d.width="+d.width, 10, 30);
g.drawString("d.height="+d.height, 10, 60);
g.setColor(Color.red);
g.drawRect(0, 0, maxX, maxY);
}//end; {paint}.
}//end.

I am using YDL6.2 on PS3, thanks.
relet
ydl beginner
ydl beginner
 
Posts: 29
Joined: 13 Dec 2011, 00:24

Re: Java programming

Postby ppietro » 08 Sep 2015, 22:45

relet wrote:If someone can help,
I cannot pass this simple piece in javac:

import java.awt.*;
import java.awt.event.*;
public class Welcome{
public static void main(String args[]){
System.out.printf("%s\n%s\n", "Welcome to Java Programming", " and this is my first program.");
}//end; {main}.
}//end.

javac Welcome.java
----------
1. ERROR in Welcome.java (at line 6)
System.out.printf("%s\n%s\n", "Welcome to Java Programming", " and this is my first program.");
^^^^^^
The method printf(String, String, String) is undefined for the type PrintStream


My guess is it's your Java compiler and/or the Java base libraries installed on your system. I tried this exact code snippet in Sun's Java and it worked fine.

Code: Select all
C:\Documents and Settings\ppietro\My Documents\javatest2>type Welcome.java

import  java.awt.*;
import  java.awt.event.*;
public  class   Welcome{
public  static  void    main(String args[]){
    System.out.printf("%s\n%s\n", "Welcome to Java Programming", "and this is my first program.");
}//end; {main}.
}//end.

C:\Documents and Settings\ppietro\My Documents\javatest2>javac Welcome.java

C:\Documents and Settings\ppietro\My Documents\javatest2>java Welcome
Welcome to Java Programming
and this is my first program.


If I remember correctly, Java on the PS3 uses GCJ (GNU Java) by default. That - frankly - isn't a very good Java.

If it's still available anywhere on the net, you'll want to use IBM's Java. They licensed it directly from Sun and it's 100% compatible.

There were instructions here:
http://www.fixstars.com/en/technologies ... efox-java/

and we wrote some up here:
viewtopic.php?f=5&t=8237
and here:
viewtopic.php?t=2935

NOTE: Just to be clear - I did not try the code snippet with IBM's Java on the PS3 - I'm using Sun's Java on a PC which should be identical.

Good luck!

Cheers,
Paul
User avatar
ppietro
Site Admin
Site Admin
 
Posts: 4965
Joined: 13 Sep 2007, 22:18

Re: Java programming

Postby ppietro » 08 Sep 2015, 23:48

By the way - as long as we're talking formatting, this version of RedRect makes more sense to me:

Code: Select all
/*
 * RedRect.java
 *
 * Created on September 8, 2015, 3:14 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import java.awt.*;
import java.awt.event.*;

public class RedRect extends Frame {
   
    /** Creates a new instance of RedRect */
    RedRect() {
        super("RedRect");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        } );
        setSize(200, 100);
        add("Center", new CvRedRect());
        show();
    }
   
    public static void main(String[] args) {
        new RedRect();
    }
   
}

class CvRedRect extends Canvas {
   
    public void paint(Graphics g){
        Dimension d=getSize();
        int maxX=d.width-1, maxY=d.height-1;
        g.drawString("d.width="+d.width, 10, 30);
        g.drawString("d.height="+d.height, 10, 60);
        g.setColor(Color.red);
        g.drawRect(0, 0, maxX, maxY);
    }
   
}


It should be identical, but it "looks" more like the Java I'm used to. :D

I like seeing the constructors first in a class, followed by the methods. But - that's probably just me. :)

Cheers,
Paul
User avatar
ppietro
Site Admin
Site Admin
 
Posts: 4965
Joined: 13 Sep 2007, 22:18


Return to Software Development

Who is online

Users browsing this forum: No registered users and 1 guest