BestJewSinceJC 700 Posting Maven

An obvious way to generate permutations of n is to generate values for the Lehmer code (possibly using the factorial number system representation of integers up to n!), and convert those into the corresponding permutations. However the latter step, while straightforward, is hard to implement efficiently, because it requires n operations each of selection from a sequence and deletion from it, at an arbitrary position; of the obvious representations of the sequence as an array or a linked list, both require (for different reasons) about n2/4 operations to perform the conversion. With n likely to be rather small (especially if generation of all permutations is needed) that is not too much of a problem, but it turns out that both for random and for systematic generation there are simple alternatives that do considerably better. For this reason it does not seem useful, although certainly possible, to employ a special data structure that would allow performing the conversion from Lehmer code to permutation in O(n logn) time.

You could try to do some research on what I found for you above and try to generate all possible permutations of your String. Then, you could use a dictionary as your text file and look up each permutation using a binary search. Binary search works basically like you'd think it would for searching a dictionary: start in the middle, if your word is higher than the word in the middle, throw out the bottom half of the dictionary. Then go to …

BestJewSinceJC 700 Posting Maven

The error message would be nice.

BestJewSinceJC 700 Posting Maven

You can easily write this with either a for loop or a while loop. You don't need both. Use the % (modulus) operator to tell if a number is even or not. If number % 2 == 0 then it is even.

BestJewSinceJC 700 Posting Maven

What is your mechanism for allowing the user to draw on the JPanel to begin with? You might have to save the way the user drew the image and duplicate it when the program is re-run.

BestJewSinceJC 700 Posting Maven

Nah. I think I'll pass.

BestJewSinceJC 700 Posting Maven

right click on the desktop
click personalize
in the bottom left, under "see also" it says "taskbar and start menu"

I think you'll be able to get it from there.

BestJewSinceJC 700 Posting Maven

You have most of the correct code.

import java.util.Scanner;
import java.io.*;

/**
   
*/

public class ChargeAccountValidation
{
   public static void main(String[] args) throws IOException
   {
      final int SIZE = 50;
      int[] numbers = new int[SIZE];
      int result, searchValue;
      
      int index = 0;
      
      // Open the file.
      File file = new File("accounts.txt");
      Scanner inputFile = new Scanner(file);

         while (inputFile.hasNextInt() && index < numbers.length)
         {
            numbers[index] = inputFile.nextInt();
            System.out.println(numbers[index]);
            index++;
         }

      }
      // Close the file.
      inputFile.close();
   }
}

I only changed a few things: removed the for loop, initialized the index to 0, and changed the while loop's condition from hasNext() which will match basically anything to hasNextInt() which will match only an int. Alternatively you could use a for loop to do this, but combining a while & a for makes no sense:

for (int index = 0; index < numbers.length; index++){
if (inputFile.hasNextInt()) numbers[index] = inputFile.nextInt();
}
BestJewSinceJC 700 Posting Maven

I agree with you, mainly, but what did that 'hijack' thread have to do with people who don't show effort on homework assignments? I thought the whole 'hijack' thing was about threads that had gotten split or something like that. Guess I'm out of the loop...

BestJewSinceJC 700 Posting Maven

Well, you could have them do something like this:

String choice = JOptionPane.showMessageDialog(null, "Enter 1 for Small, 2 for Medium, 3 for Large, 4 for X");
int userChoice = Integer.parseInt(choice);
System.out.println("Your pizza price is " + sizePrice[userChoice-1]);

Honestly though, the other Dialog that I showed you before would be a much more reasonable dialog in this case, because it forces the user to pick from a list of choices, and the user can't pick anything that you don't want them to pick. The other dialog that I showed you returns an int which would represent '0' for small, '1' for medium, etc. So if you use that dialog, you could directly use sizePrice, whereas if you use the dialog that I showed you above, you have to do a bunch of extra work to make sure the user actually entered something between 1-4.

By the way, the code you posted above would work, except you cannot use '==' to compare two Objects for equality. Using '==' only works to compare primitive types (int, double, etc) for equality. To compare Objects for equality, you have to use a method - the equals() method. The equals() method is supposed to return true if the Objects are equal and false otherwise. So change this:


if (size == whatSize[s])
//Should be
if (size.equals(whatSize[s]))
BestJewSinceJC 700 Posting Maven

Ok, I see. This link might be helpful. It'll use a dialog to drop down a box from which the user is forced to choose their option. So you could use the drop down box to force them to choose between S, M, L, X. They already have a code example there.

http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html#input

BestJewSinceJC 700 Posting Maven

Not really man. Not trying to be rude but punctuation and clear explanations go a long way. I have no idea what 'pizza size' or anything else you mentioned has to do with your problem.

BestJewSinceJC 700 Posting Maven

Your explanation makes no sense. Do you want to know how to use a JOptionPane?

JOptionPane.showMessageDialog(null, "Here is some text!");
BestJewSinceJC 700 Posting Maven

No problem. You might want to check out the contains() methods of the Rectangle class. They make it easier to detect which braille character you are clicking on.

BestJewSinceJC 700 Posting Maven

No problem, mark the thread as solved

BestJewSinceJC 700 Posting Maven

Yeah; after you add the new widget to the JFrame, call myFrame.validate();

BestJewSinceJC 700 Posting Maven

I suppose you could try to draw different sized rectangles and other shapes then. But that's where you're on your own as far as me helping you, because I've only drawn on JPanels before.

If drawing on the buttons proves to be too hard, you could also draw your grid on a JPanel, implement MouseListener, detect the user's button clicks (using MouseListener) and detect whether or not you clicked on a particular button. I wrote code once that did exactly that, but the project is on my other laptop. If it comes to it, I can grab the project and give you an example.

BestJewSinceJC 700 Posting Maven

I don't see why not. But don't do that. Just add an image, it is much easier.

BestJewSinceJC 700 Posting Maven

You have to declare each variable separately. Putting = new JPanel() at the end only declares the last variable, not all of them. Therefore your pnlUser variable has never been initialized with a "new" statement and is giving a NullPointerException.

BestJewSinceJC 700 Posting Maven

No problem, glad you got it working! Mark the thread as solved

BestJewSinceJC 700 Posting Maven

http://java.sun.com/docs/books/tutorial/uiswing/components/button.html

You can make JButtons with images, text, or both images and text on them. You can add these to a JPanel that uses the GridLayout layout manager. For example

//Creates a panel with a layout that has 10 rows and 10 columns.
JPanel panel = new JPanel(new GridLayout(10, 10));
BestJewSinceJC 700 Posting Maven

If you wanted to show all of the results you'd have to do

showResult= showResult + "Found Names: "+found+ "Similarity: " +compare+"Path: "+file.getName();

or, equivalently,

showResult +="Found Names: "+found+ "Similarity: " +compare+"Path: "+file.getName();
BestJewSinceJC 700 Posting Maven

Ok, here is a quick example for you. As you can see, I created a new SwingComponent (which adds a Swing JButton into the SWT Composite) from within the ViewPart class. But you can call new SwingComponent from anywhere and it will work the same. Again, keep in mind the things I've told you earlier in the thread though: if the View isn't currently showing, you will have to call showView in order to see anything.

package testpluginproj;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class ViewPart1 extends ViewPart {
static Composite parent = null;
	public ViewPart1() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stu
		Button ok = new Button (parent, SWT.PUSH);
		this.parent = parent;
		ok.setText ("Push to show the view!");
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				new SwingComponent();
			}
		});
		
		parent.layout(true);

	}

	
	public static Composite getComposite(){
		return parent;
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
package testpluginproj;

import java.awt.Frame;

import javax.swing.JButton;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;

public class SwingComponent {

	public SwingComponent(){
		Composite parent = ViewPart1.getComposite();
		Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
		Frame frame = SWT_AWT.new_Frame(composite);
		frame.add(new JButton("STUFF"));
		frame.validate();
		parent.layout(true);
	}

}
BestJewSinceJC 700 Posting Maven

Even if I create a separate class like the one you have written and call the createPartControl(parent) method from outside the function what should the argument "parent" be?

You should never call createPartControl. Eclipse handles calling that method for you. If you want to make modifications to the View, like I have already suggested, make a getter method in your ViewPart class.

public Composite getComposite(){
return parent; //where parent is what I have in my prev. ex.
}

Then you can add your Swing components to the Composite as is detailed in the article I just linked you to. You can then call the layout(true) method as I told you earlier, causing your changes to be visible. If you give me a few minutes, I will give you a full example if this doesn't make sense.

BestJewSinceJC 700 Posting Maven

I find it hard to believe that none of my suggestions thus far have helped, but additionally, you can check out this article on Swing/SWT Interactions. However, if as you said you want to "show the Swing table in the Eclipse window" you're still going to need to use the suggestions I gave you about how to find the view and how to show the view.

BestJewSinceJC 700 Posting Maven

If you want to show something on an Eclipse Workbench Window like you said a bunch of posts ago, then that something will have to be shown in a View, which in turn, will have to extend ViewPart.

Oh, and if you use the showView method by just replacing that code I showed you earlier by changing findView to showView, then it will make the view appear. Isn't that what you wanted to do this whole time?

BestJewSinceJC 700 Posting Maven

Sorry, I was a lot more rusty with commands than I thought I was. Nevertheless the code I gave you before works. I used this class and it works fine for me. (Excuse the goofiness/bad programming practice with making the Composite a static member... )

package testpluginproj;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class ViewPart1 extends ViewPart {
static Composite parent = null;
	public ViewPart1() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stu
		Button ok = new Button (parent, SWT.PUSH);
		this.parent = parent;
		ok.setText ("Push to show the view!");
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ViewPart yourView = (ViewPart)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("TestPluginProj.myview");  
				Label label = new Label(ViewPart1.parent,SWT.NONE);
				label.setText(yourView.getPartName());
				ViewPart1.parent.layout(true);
			}
		});
		
		parent.layout(true);

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}

Anyway, the above works for me and modifies the View so that it says "Kyle's View" which is the name of it.

BestJewSinceJC 700 Posting Maven

That's fine. Eclipse RCP says Galileo on it when it runs anyway. It is just a question of having more functionality than "normal Eclipse". As long as you can call PlatformUI.getWorkbench() without problems, you should have the required libraries.

edit:

Another thing I just remembered: if you haven't properly set up the view (so that it actually shows up when your application launches) then calling findView() is not going to work and that might be why it is returning null. You should try running that other piece of code I gave you (the one that prints out all of the view IDs) and show me what it says. My bet is that your view ID is not one of the ones there...

BestJewSinceJC 700 Posting Maven

My main method is empty now but don´t know what you mean with rewriting the brackets :-)

BTW Thanks to both of you for your help i really apprechiate it...

He was just repeating what I said. Anyway, it would be helpful to have your main driver in another class... the TestHuman class is a good idea. Just make sure to define your variables as class variables by putting them like this in your Human class:

public class Human{
//Your variable declarations go up here if they're class variables!
}
BestJewSinceJC 700 Posting Maven

Hmm, that is interesting. I don't even have Eclipse RCP on my computer right now but I'll download it and make a project real quick and get back to you. Like I said, all of this has been from memory but it has been a while. So give me ten minutes and I'll reply back. .

BestJewSinceJC 700 Posting Maven

Read my previous post. We posted at the same time.

BestJewSinceJC 700 Posting Maven

you have name = CharSet.next()
perhaps what you need is CharSet.nextInt()

No; that would result in an error because name is a String and nextInt() obviously returns an int. He might want to say name = CharSet.nextLine() but that is not the source of his compiler error. His compiler error is a result of trying to define two methods, MyChar() and Reveal(), inside of the main method. You can't define one method inside of another method like that. Moving the two methods outside of main will get rid of one error, but introduce another. In order to use the variables name, strength, health, intelligence, and agility in MyChar() and Reveal(), they should be declared as class variables. The other alternative would be to pass them as parameters into the MyChar and Reveal methods.

BestJewSinceJC 700 Posting Maven

You have to give it the correct viewID where I put "viewID". It is the number associated with your view. If you give it an invalid ID (such as my example) it won't be able to find your view, and thus, will return null, ultimately leading to a NullPointerException. You already defined the View ID when you filled out the form in RCP for your view. But anyway, you can also list all of your view ID numbers like this:

IViewReference[] refs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
for (IViewReference ref: refs) System.out.println(ref.getID());

As for not knowing your view ID, remember this screen from when you originally created your view? It contains your view ID field. (Note: this image is taken from Vogella's Eclipse RCP tutorials)

BestJewSinceJC 700 Posting Maven

Ok, so here's the deal. I was able to connect to your server using telnet which, to me, indicates that your server is working more than you thought. All I did was compile & run your server, then say telnet localhost 21111. I compiled with just "gcc server.c", ran the executable, then issued the telnet command.

edit:
I also ran both of your programs like so:
gcc -o Server server.c
gcc -o Client client.c
Server
(in other terminal now)
Client localhost

Running it as I did above gave me output "My TCP port is (some number here)" then it seg faulted. Not what you'd want but better than not running at all I guess.

BestJewSinceJC 700 Posting Maven

You should post the code in code tags (unless it is actually too big to reasonably view on a normal size screen) instead of attaching it. I'm assuming that it is pretty small from your project description. Anyway I'll take a look at it since I recently did a similar project. Note that I'm not really much of a C developer so take any advice at your own risk or wait for someone else to respond. Here is his code for anyone interested.

Server.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <time.h>

#define SERVERPORT "21111" 
#define MAXLEN 100


void *get_in_addr(struct sockaddr *sa)
{
  if (sa->sa_family == AF_INET) {
    return &(((struct sockaddr_in*)sa)->sin_addr);
  }

  return &(((struct sockaddr_in6*)sa)->sin6_addr);
}


main() 
{ 
	int sockfd, newfd;    // listen on sockfd, new connection on newfd
	unsigned int len;
	socklen_t sin_size;
	char msg[80];
	char buf[MAXLEN];
	int st, rv;
	struct addrinfo hints, *serverinfo, *p; 
	struct sockaddr_storage client;
	char s[INET6_ADDRSTRLEN];
	time_t rawtime;

	
	
	
  //zero struct	
  memset(&hints,0,sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;

  if((rv = getaddrinfo("localhost", SERVERPORT, &hints, &serverinfo ) != 0)){
    perror("getaddrinfo");
    exit(-1);
  }

   // loop through all the results and bind to the first we can
  for( p = serverinfo; p != NULL; p = p->ai_next){
    if( (sockfd = socket( p->ai_family, p->ai_socktype, p->ai_protocol )) == -1 ) {
      perror("socket");
      continue;
    }
    
    if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1){
      close(sockfd);
      perror("bind");
      continue;
    }
    
    break;
  }

  if( p == NULL ){
    perror("Fail to bind"); …
BestJewSinceJC 700 Posting Maven
ViewPart yourView = (ViewPart)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("viewID");

You might have read through the API and gotten confused because the findView() method is defined as returning an IViewPart. IViewPart is an interface, not a class. ViewPart is a class that implements the IViewPart interface. ViewPart defines the view that you see on your screen, so casting to a ViewPart, like I did above, will be safe. Once you do what I did above to get a handle on your Eclipse RCP view, you can then modify the Composite (which is an SWT widget). I don't remember how to get the Composite with an "out of the box" method, but you can simply define your own getter method in the class. After you modify the Composite to put your diagram on the screen, you're going to want to do the following:

// .. add stuff to the Composite up here
yourComposite.layout(true);

Calling the layout() method causes the Composite to re-display all of its contents. So basically you're telling it you made changes. As for calling SWT from AWT, as far as I know it makes no difference where you call these methods from. As long as you are in an Eclipse RCP built application, you can call all the methods that I have shown you. And as long as you can call all of the methods that I have shown you, you are good to go. Do you get errors when you try to call PlatformUI.getWorkbench()?

BestJewSinceJC 700 Posting Maven

How so? Please post your solution so that if someone comes across your thread in a search (or is just reading the thread like I am) they can see it.

BestJewSinceJC 700 Posting Maven

Assigning x = board does nothing. The variable x goes out of scope as soon as your method call returns. You should be saying board = x. Once you fix that, your program still will not work. You never created & initialized any values in your 2D array. Really, at this point, you need to go read about 2 dimensional arrays, variable scope, and how methods work in Java. Once you read up on all of that information you will be ready to start your project.

BestJewSinceJC 700 Posting Maven

You could try serializing the JPanel class and seeing if that works as wanted when you load it. I have no idea if that will work or not for an image the user drew on the panel though, but it might be worth your time to try it out since Serializing something doesn't take very long.

BestJewSinceJC 700 Posting Maven
return (tr.setPrice() + tp.setPrice() + tq.setPrice());

If you look in your BillPanel class, you never initialized any of those variables. Therefore, there contents are null, and you are getting a NullPointerException. How do you expect to say tr.setPrice() when you never created "tr"? Essentially tr at that point is just an empty variable with nothing in it. You have to say tr = new PizzaPanel() at some point in your code before you try to use the variable tr. The same goes for all of your other variables.

BestJewSinceJC 700 Posting Maven

Are you sure you aren't missing a . right before the second ?
I just did something similar in Perl so I can probably help.. but an example of what your input and output are would be nice.

BestJewSinceJC 700 Posting Maven

character[0] wasn't initialized . . that's all I can tell you from the information given.

BestJewSinceJC 700 Posting Maven

You might also agree with him if he says 1 + 1 = 3 but that doesn't make it any more true.

BestJewSinceJC 700 Posting Maven

You told us this is a desktop application. What is a form? Just tell us what type of Objects you are using for your window... otherwise how are we supposed to help you?

BestJewSinceJC 700 Posting Maven

Well your switch statement has no affect because you declared your variables inside the switch. Declare the variables outside of the switch if you want your assignment statements to do anything. i.e. what I did with your 'm' variable below.

public class Travel {
   
   public static void main(String[] argv) 
   {
    
        double traveldistance, time, m;
        int convertseconds, hours, remainder, minutes, seconds;
        
        char roadtype;
                      
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        System.out.println ("Type the road type:");     // Prompts to enter travel distance and reads the values
        roadtype = UserInput.readChar();
                         
        switch (roadtype){
            case 'm':                                    // Motorway
                     m=85;
                     break;
                    
            case 'a':                                   // A Road
                     double a=70;
                     break;
                    
            case 'b':                                   // B Road
                     double b=55;
                     break;
                    
            case 'u':                                   // Urban Road
                     double u=40;
                     break;
            
            default:
                    System.out.println("Invalid road type " + roadtype);          // Invalid road type
                    System.exit(0);
                    break;
        }
                
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        System.out.println ("Type the travel distance:");    // Prompts to enter travel distance and reads the values
        traveldistance = UserInput.readDouble();
                      
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
        time = traveldistance / roadtype;            // formula (units in hours)
        
        convertseconds = (int)((time * 60) * 60);    // converts hours to seconds
                        
        hours = convertseconds / 3600;               // converts the seconds to HH:MM:SS
        remainder = convertseconds % 3600;
        minutes = remainder / 60;
        seconds = remainder % 60;
        
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        System.out.println ("To travel " + traveldistance + " takes " + convertseconds + " seconds");
        System.out.println ("To travel " + traveldistance + " takes " + hours + " hours " + minutes + " minutes " + seconds + " seconds");
              
   
   } // end of main

} // end class
BestJewSinceJC 700 Posting Maven

It means that, at the line where you got that error, you are trying to use a variable which has not been initialized.

BestJewSinceJC 700 Posting Maven

Another note: I actually had to play around a lot to get all of this to work. It took me maybe 30 minutes. I wasn't able to use getResource() to read a text file at all. If anyone else has any examples of using getResource (rather than getResourceAsStream which I ended up using), that would be helpful. It doesn't really matter what you use as long as it works, but I'm just curious how it would be done. The first link I posted in my first post in this thread claims that using getResourceAsStream is much easier, which is why I switched to that (besides, I could create a Scanner from it directly, given the fact that it returns an InputStream).

BestJewSinceJC 700 Posting Maven

Yes. In your "main package" you can add a folder called resources. In that resources folder you can put your files. Then you can export the project as a Runnable Jar File in eclipse by going to File->Export->(Choose Java Option)->Runnable Jar File. Here is a code sample to get you started. Keep in mind that the way I told you to add the folder and the location of that folder are *not* the only way to do it, but it is one way. You can experiment on your own to see other ways or you can read this article on accessing resources and this article on the various ways you can arrange resources in your jar files (go down to the section on loading images using getResource). Keep in mind that the code sample I provided below uses a file called test2.txt which is in a folder called resources. And if you run this on a long text file you'll be in for a surprise because JOptionPanes will probably keep popping up because of the while loop at the end.

import java.io.InputStream;
import java.util.Scanner;

import javax.swing.JOptionPane;


public class ResourceTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		InputStream stream = ResourceTest.class.getResourceAsStream("/resources/test2.txt");
		if (stream == null) JOptionPane.showMessageDialog(null, "Resource not located.");

		Scanner input = null;
		try {
		 input = new Scanner (stream);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, "Scanner error");
		}
		
		while (input.hasNextLine()) JOptionPane.showMessageDialog(null, input.nextLine());
	}
	
}
BestJewSinceJC 700 Posting Maven

Call the setDefaultCloseOperation method on your JFrame.

BestJewSinceJC 700 Posting Maven

User agreements are notoriously long and dry. . :(

BestJewSinceJC 700 Posting Maven

Follow the rules and post a question and show effort.