tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

You're taking input as a string: scanf("%s",&array[i]);.

tux4life 2,072 Postaholic

you can download dos-box turbo c++ from internet. it will work on all versions of window including win 7 and win 8 also

try it now

Why doing so many effort for something that is worse?

tux4life 2,072 Postaholic

i'm wanting to do separate programs for each (aka one program for adding one for subtracting) is this possible?

Yes.

tux4life 2,072 Postaholic

Read this and figure out why you must not use void main() but int main(void). "A-void main()" (Read as: avoid void main()) Didn't look further though.

Mayukh_1 commented: I rather saw conio.h & stopped +1
tux4life 2,072 Postaholic

There's no need to write your own logger. There are frameworks available for that.

If you're interested, you might want to take a look at these links:

tux4life 2,072 Postaholic

Where do you have problems with in specific?
I'd suggest you to perform a Google search on 'Sokoban', look at existing implementations, play a couple of them and see how it works.
Familiarize yourself with the rules of the game. Start coding, and when you encounter problems come back and ask for help.

tux4life 2,072 Postaholic

does he mean incorporating arrays and such?

Why are you asking us and not your teacher?

As it stands now it's a bit of a mess...

  • You're using unportable stuff like conio and system("cls").
  • Try to replace the recursion in your input routines by iterative constructs.
  • Remove useless comments like "This is the Returns function", every C programmer will be able to figure that out when seeing void Returns().
  • Try incorporating more error handling and safety: things like scanf ("%s",&fname); aren't safe.
  • Concisely choose your variable names.
  • Avoid the use of global variables.

There's probably more, but this is what I noticed from a quick skim through your code.

tux4life 2,072 Postaholic

Of course we are able to help you. Could you show us your code and point out what issue you are having?

tux4life 2,072 Postaholic

Wondering... how many time did you spend reading my post?

tux4life 2,072 Postaholic

You're using Eclipse for Java EE Developers right?
Window > Preferences > Server > Runtime Environments > Add > Download additional server adapters (link)

tux4life 2,072 Postaholic
 @Override
 public void actionPerformed(ActionEvent ae) {
    double p = Double.parseDouble(pricefield.getText());
    double d = Double.parseDouble(discountfield.getText());
    double sum = 0;

    while (p < sum && d < sum) {
        sum = p * d;
        calc.setText(sum);   
    }
}

You initialize sum to 0, hence if you enter a value bigger than zero in your pricefield or discountfield, then the condition p < sum evaluates to false or d < sum evaluates to false, or both.
If at least one of the operands in a logical AND operation is false, then the condition evaluates to false, hence the statements in your loop won't be executed.
However, if you only need to display the product of both numbers, then you don't need a loop here at all, since p and d don't change, their product won't change either, so no point in looping.
If you need a check, then use an if statement. This loop will loop infinitely if you enter two negative numbers.

tux4life 2,072 Postaholic

Here, here and here.

tux4life 2,072 Postaholic

Here and here.

Pobunjenik commented: Nice one. +0
tux4life 2,072 Postaholic

I took the time trying to reproduce the behaviour you describe in your post, but I don't seem to be able to.
I was getting other errors, most likely because I was guessing at the file structure.
Is this the same code as you were using? Could you provide us with the exact same contents of the input file that you were using at the time you encountered the problem?

On a side note:

Code formatting can be done effortlessly in any decent IDE.
From lines 1-4 I can derive that you're probably using NetBeans.
You can let NetBeans format code for you: Right click in your code editor and choose Format, or use the keyboard shortcut ALT+SHIFT+F.

tux4life 2,072 Postaholic

Golden advice from a quick skim through your code:

Line 122-123: NEVER EVER leave your catch blocks empty! [1] It will hide away every potential error and problem at the time one occurs.
Whenever you feel tempted to leave a catch block empty, always at least insert a call to printStackTrace(), like this:

...
catch (Exception ex)
{
    ex.printStackTrace();
}
...

[1] This is also commonly referred to as "eating the exception". Well, trust me: exceptions taste bad, you don't want to ever eat them :P

tux4life 2,072 Postaholic

I want it to display automatically when I run the programme without the need to press any buttons

Set the text field's content in your constructor.

tux4life 2,072 Postaholic

You can use filters and sorters on a JTable, if all the data is already in your table model.

tux4life 2,072 Postaholic

So Far I Havent Started to Code On My 'OWN'

What are you waiting for? Get started! Cheapest way, and you learn something too ;-)

tux4life 2,072 Postaholic

Open your file in append mode:

PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

Implement your own AbstractTableModel and let it interact with the database.
You'll have to provide your own remove method there, and make use of the fireTableRowsDeleted method to notify the JTable that a row was deleted.

Here are two links that should provide you with the information you need (including examples):

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
http://www.informit.com/articles/article.aspx?p=332278

tux4life 2,072 Postaholic

If anything needs explaining then just send me a PM

If anything needs explaining he better replies with a post, which - on a forum - is a far more superior way to get help than by a PM.

tux4life 2,072 Postaholic

I'm going to illustrate two approaches using a small amount of code.
I'm just illustrating the concept, writing a robust implementation is up to you.

HashMap approach:

HashMap<String, /* insert type of your control */> board;
board.put("a1", /* reference to your control */);
board.put("a2", /* reference to your control */);
// etc.

You can write it shorter using loops, but you get the idea.

// get a control
control = board.get(pos.getText().toLowerCase());

Array approach:

Put your board controls in a 1D-array and compute the appropriate index from the user input.

String s = pos.getText();
// compute index in array
int loc = (Character.toLowerCase(s.charAt(0)) - 'a') * 3 + (s.charAt(1) - '1');

// get a control
control = board[loc];
tux4life 2,072 Postaholic

Is this possible with Dialog?

As mKorbel said in this post, there are two proper ways:

  • CardLayout
  • JDialog

So yes, it is possible with a dialog.

Or JFrame is the correct way?

Typically you need at most one JFrame per application.
Let's take Microsoft Office Word as an example: you have one main window (this is analog to having one JFrame), and for some functionality there are separate small windows (these are called dialogs, and are analog to JDialog).

tux4life 2,072 Postaholic

Probably because the intent is to write your own answer.

tux4life 2,072 Postaholic

First off, if you don't need the synchronization provided by a StringBuffer, then prefer using a StringBuilder.
Here's what the Java API states about StringBuilder:

This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

  1. Provides an API compatible with StringBuffer;
  2. Designed for use as a drop-in replacement for StringBuffer;
  3. It is recommended that this class be used in preference to StringBuffer;
  4. Will be faster under most implementations;

Second, the following piece of code can be improved upon from a performance perspective (without writing unreadable code, but by merely applying the concept of avoiding unnecessary String instantiation):

if (buffer.toString().contains(",") || buffer.toString().contains(".") || buffer.toString().contains("!"))
{
    element = buffer.reverse().toString();
    element = element.substring(1, element.length()) + element.charAt(0);
}
else
{
    element = buffer.reverse().toString();
}

There's a possibility that buffer.toString() will be called multiple times in a row, while yielding the same result every time. You have to be careful about this. Every call to a StringBuilder's [1] toString() method will instantiate a new String object! And after all that contradicts with why we initially want to use a StringBuilder: avoid the unnecessary instantiation of String objects.
A more performant …

tux4life 2,072 Postaholic

A small correction on my previous post: change "class variables" to "class variables and instance variables".

tux4life 2,072 Postaholic

The error I am getting from this is, "Non-static variable panel, cannot be referenced from a static context".

Let's analyze what the compiler is trying to tell us:

  • Non-static variable panel, let's see... variable panel is declared on line 7, and there's no static keyword there, so it is non-static.

  • cannot be referenced, this means: "you cannot use it".

  • from a static context, the "static context" the compiler is talking about is (in this case) your main method (starting at line 9). Your main method is a static context because it is a static method (the static keyword is in its signature, you can check yourself: line 9: public static void main(String[] args))

To conclude: you cannot use variable panel in your main method because main is static and your variable is not.

There's a good reason for why this isn't possible (you can skip to the end of the post if you're not interested in the reason):
First we have to differentiate between class variables and instance variables.
A good explanation is given here, but it boils down to class variable => static, instance variable => non-static.

-- From here I assume that you've read the page I linked to. --

public class A
{
    private static int a; // class variable
    private long b; // instance variable
    private double c; // instance variable
    private static boolean d; // class variable

    // compiles
    public static void aStaticMethodOperatingOnAClassVariable()
    {
        // There's only …
tux4life 2,072 Postaholic

Line 45 in the code you posted: public static Void Main(String[]args) correct it to:
public static void main(String[]args) (main without capital and void without capital)

(Even though it will compile with Main as identifier, it won't run because there's no main defined)

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

Let's take a look at createTree():

private final Tree createTree(Scanner in)
{
    Tree tree = new Tree(); // don't do this [1]
    tree.setId(sc.nextInt());
    tree.setX(sc.nextInt());
    tree.setY(sc.nextInt());
    tree.setColor(sc.Next());

    return tree;
}

[1] Don't create an instance of the superclass here. We all know how an AppleTree, LemonTree looks. But how is a general Tree supposed to look? When you think about it, it doesn't make alot of sense to be able to construct an object from class Tree at all. You can in fact protect yourself (and others) from instantiating an object from Tree by marking your Tree class as abstract, this means that writing new Tree(); will generate a compiler error. It is very simple to mark a class as abstract, you only have to put the word abstract in front of the class keyword. E.g. public class Tree becomes public abstract class Tree.

And I thought maybe the createTree method is where I should decide whether it's an apple tree or something else

Yup, you should do that.

but then how do I return that?

Declare a Tree reference outside your if block:

// Pseudocode / Skeleton
Tree tree = null;

if (appleTree)
{
    tree = new AppleTree();
    // if there are things specific to an apple tree,
    // then you should set them here
}
else if (lemonTree)
{
    tree = new LemonTree();
    // if there are things specific to a lemon tree,
    // then you should set them here
}

// …
tux4life 2,072 Postaholic

Player player; creates a reference variable that can refer to a Player object, and is initially null.

This is only true for class variables. This does not apply to local method variables (these are not initialized to a default value when no value is specified).

The compiler will issue a warning if you try to use an uninitialized variable.

The following does not compile:

public class Test
{
    public static void main(String[] args)
    {
        String s; // local variable, not initialized automatically
        // accessed
        if (s == null)
            System.out.println("s == null => true");
    }
}

/** Compiler output:
    Test.java:6: error: variable s might not have been initialized
            if (s == null)
                ^
    1 error
*/

The following compiles though:

public class Test
{
    public static void main(String[] args)
    {
        String s; // local variable, not initialized automatically
        // not accessed
    }
}

The following compiles:

public class Test
{
    String s; // class variable
    // will be initialized automatically
    // to a default value if no value is
    // specified, default value in this
    // context means: null.

    public static void main(String[] args)
    {
        Test t = new Test();
        if (t.s == null)
            System.out.println("s == null => true");
    }
}

/** Output:

    s == null => true

*/
tux4life 2,072 Postaholic

He better learns this first.

tux4life 2,072 Postaholic

My problem is with the constructor: where does it get called? I would have thought in this line
Player player; but shouldn't this be Player player = new Player();?

You are right in saying that the line Player player; does not call the constructor, however, if you look a bit further you see that in the for loop on line 17 (TeamFrame.java) there's a constructor call ;-)

Also notice that Player player = new Player(); would be invalid here anyhow since there's no such constructor defined in the code you posted.

tux4life 2,072 Postaholic

For re-usability it would be better to return the long value, rather than set a fixed named variable that's declared elsewhere?

In addition it would be a good idea then to also rename the method to getUniqueIdentifier().

tux4life 2,072 Postaholic

i think you have to use

pos.getX(),pos.getY()
methods for the where it is located
(or)

pos.getLocation()
check it once for your desired outout
and let me know the status after changing your code

The OP doesn't want to get the location/position of the textfield. He wants to create a TicTacToe game, every move the user needs to enter the board location where wants to place his/her marker. That board location is entered in a textfield.

tux4life 2,072 Postaholic
  1. Your attribute declarations (lines 14-23) should be outside any method (now they are in your main method, move them out).
  2. You've put your applet() method (lines 28-43) inside your main method, move it outside of your main.
  3. You miss a curly ending brace for your main and applet method.
tux4life 2,072 Postaholic

how do i get a1 as in its control name in swing and not the actual string

http://stackoverflow.com/questions/4958600/get-a-swing-component-by-name

You could also put the components that represent your board squares in an array and translate the input from your textfield to the corresponding array index.

tux4life 2,072 Postaholic

By "excel file" you mean perhaps a CSV file, that - when opened/imported in Excel - produces output like in the example you provided us with?

tux4life 2,072 Postaholic

i want to read the data from text file and write to excel file.

You'll need to be more specific on how the data is formatted in these files, perhaps include an example of how your text file looks, and how your "excel file" should look.

tux4life 2,072 Postaholic

CAN ANYBODY PLEASE TELL HOW TO LEARN C++ Coding

Read books, write alot of code, be prepared to fail alot...

IS THAT THEROY IMPORTANT

Pretty much.

PLEASE HELP ME TO MAKE MY CARRIR . . . MY AIM IS TO BECOME A SOFTWARE ENGINEER

Read books, write alot of code, be prepared to fail alot...

PLZ PLZPLZ HELP ME N SUPPORT ME PLZ

PLZ USE DECENT ENGRISH N STOP WRITNG ALL CAPS...

tux4life 2,072 Postaholic

Yes, it will work with JOptionPane, as long as you use String.format() to format it into a String. After that you can output the String using a JOptionPane.
I am aware that I linked to a page using System.out.printf() but it is analog for String.format().

tux4life 2,072 Postaholic

Take a look at: http://www.homeandlearn.co.uk/java/java_formatted_strings.html .
Use String.format() instead of System.out.printf().

tux4life 2,072 Postaholic

Try a GridLayout.
FYI: It is also possible to do this entirely without buttons, it would be more complicated though
(since you'd need to compute the clicked square from the coordinates of the mouse click).

tux4life 2,072 Postaholic

You should be more specific as to what you want to do with your truncated number.
Do you want to put it in a String? Do you want to print it to the screen and keep the exact value in your variable? Do you want to truncate the result in your double variable?

  1. Only printing to the screen: System.printf("%.1f", number);
  2. Truncated number as a String: String.format("%.1f", number); and assign it to a String reference variable.
  3. Truncate the result in your double variable: number = (int) (number * 10) / 10.0;
deceptikon commented: Yup, intended usage is important. +12
tux4life 2,072 Postaholic

You might be interested in the following case study of Deitel's Java How To Program: http://www.deitel.com/books/jhtp7/DeitelMessenger.pdf

tux4life 2,072 Postaholic

Read this first. Have fun with it ;-)

tux4life 2,072 Postaholic

does not move past the accept method

In fact it does move past the accept method.
The reason why your "Client connected" message is not displayed is because you add it to a String, but you don't append it to your JTextArea.

What could be creating the problem that I am having?

private void chatProgram()
{
    while (clientSock.isConnected()) // [1]
    {
        read(); // [2]
        conversation.setText(text);
    }

    closeSockets();
}

private void read()
{
    try
    {
        String r = read.readLine(); // [3]

        if (r != null)
        {
            text += r + nl;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

Even though you have the check clientSock.isConnected() in [1], this check will execute before the call read.readLine(); in [3] (via [2]) blocks - waiting for input.
When the client closes, the connection is reset and an exception is thrown from the underlying client socket input stream at the server-side.
This IOException is propagated from [3] and caught by the catch block in your read() method.

On Client line 69: write.write(t); you need to write a newline here too.
You can do this by writing: write.println(t);.

tux4life 2,072 Postaholic

All the common things (id, color, position) go in the superclass since they are the same for every Tree. Like you said: the only thing that differs is the the appearance.
So how should this superclass look?

// Our superclass
public abstract class Tree
{
    private int id;
    private int x;
    private int y;
    private String color;

    public Tree(int id, int x, int y, String color)
    {
        this.id = id;
        this.x = x;
        this.y = y;
        this.color = color;
    }

    // We want each subclass to have a paint method.
    // Since the appearance of each Tree is different,
    // we leave it up to the subclass to implement the
    // drawing behaviour.
    public abstract void paint(Graphics g);

    // getters (public)
    // You need to provide these yourself!
    // ...
}

I omitted the use of setters here for the sake of simplicity, and to keep the example short.

How might we implement an AppleTree?

// A concrete Tree subclass
public class AppleTree extends Tree
{
    public AppleTree(int id, int x, int y, String color)
    {
        // The fields for id, x, y and color are declared as private
        // in the superclass, they are still inherited, but they aren't
        // visible in subclasses.
        // Call the constructor of the superclass and let it handle setting
        // these fields for us.
        super(id, x, y, color);
    }

    @Override
    public void paint(Graphics g)
    {
        // Implement your drawing algorithm here.

        // First problem: we need the x, y …