Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Save Russian Jews
Jun 7, 2007

who the fuck is this guy anyway, i can't even see his face

Lipstick Apathy
EDIT: I have thoroughly poo poo up this post to the point where it is just entirely out of control and I should figure out what I am trying to ask before I actually post a question.

Save Russian Jews fucked around with this message at 07:10 on Jan 28, 2012

Adbot
ADBOT LOVES YOU

Harold Ramis Drugs
Dec 6, 2010

by Y Kant Ozma Post
I'm working on a program to calculate Pascal's Triangle up to 16 rows, and then print it out as a (mostly) perfect isosceles triangle. I'm having trouble coding a formula to do this cleanly though, and the only appearant solution I can think of is to write a method that will print each line of the triangle with unique directions for spacing. This is what I have so far:

code:


   public class PascalTester
   {
      public static void main (String[] args)
      {
         int[] starter = {0, 1, 0};
         int[][] ara1 = new int[16][];
         ara1[0] = starter;
         ara1 = extender(ara1);
         ara1 = maker(ara1);
         printy(ara1);
      
      
      
      }//end main
      public static int[][] extender(int[][] ara3)
      {
         int[] temp;
         for (int i = 1; i < ara3.length; i++)
         {
            temp = new int[ara3[i-1].length + 1];
            ara3[i] = temp;
         	
         }
         return ara3;
      }
   
   
      public static int[][] maker (int[][] ara2)
      {
         int temp;
         for (int i = 1; i < ara2.length; i++)
         {
            for (int j = 1; j <ara2[i].length -1; j++)
            {
               ara2[i][j] = (ara2[i-1][j-1] + ara2[i-1][j]);
            }
         }	
         return ara2;
      }//end maker
      public static void printy (int[][] ara4)
      {
         for(int i = 0; i < ara4.length; i++)
         {
				for (int k = i; k < ara4.length; k++)
				{
					System.out.print("  ");
				}
            for(int j = 0; j < ara4[i].length; j++)
            {
               if(ara4[i][j] > 0)
               {
                  System.out.print("" + ara4[i][j] + "  ");
               }
            }
            System.out.println();
         }
      	
      }//end printy
   
   
   }//end class
Which prints this...

code:


                                1  
                              1  1  
                            1  2  1  
                          1  3  3  1  
                        1  4  6  4  1  
                      1  5  10  10  5  1  
                    1  6  15  20  15  6  1  
                  1  7  21  35  35  21  7  1  
                1  8  28  56  70  56  28  8  1  
              1  9  36  84  126  126  84  36  9  1  
            1  10  45  120  210  252  210  120  45  10  1  
          1  11  55  165  330  462  462  330  165  55  11  1  
        1  12  66  220  495  792  924  792  495  220  66  12  1  
      1  13  78  286  715  1287  1716  1716  1287  715  286  78  13  1  
    1  14  91  364  1001  2002  3003  3432  3003  2002  1001  364  91  14  1  
  1  15  105  455  1365  3003  5005  6435  6435  5005  3003  1365  455  105  15  1  
If you guys can give me any pointers on how to format my printing so that the correct numbers line up, I would be greatly appreciative. Thanks

DholmbladRU
May 4, 2006
code:
public List<homeObj> read(){
 
	  SAXBuilder builder = new SAXBuilder();
	  File xmlFile = new File("C:/Users/Darren/workspace/test/src/homesDbase.xml");
	  List<homeObj> homes = new ArrayList<homeObj>();
	  try {
 
		Document document = (Document) builder.build(xmlFile);
		Element rootNode = document.getRootElement();

		List list = rootNode.getChildren("home");
		homeObj homeObject = null;
		//still need to work on getting ID
		//System.out.println(rootNode.getAttributeValue("id"));
		
		
		for (int i = 0; i < list.size(); i++) {
 
		   Element node = (Element) list.get(i);
		   homeObject = new homeObj();
		   		   
		   homeObj.setLocation(node.getChildText("location"));
Why do I get an error message saying 'cannot make a static reference t non-static method' on the last line. None of the methods being called are static, and I believe I have properly created the homeObj reference in this method.

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

DholmbladRU posted:

code:
   homeObject = new homeObj();
   homeObj.setLocation(node.getChildText("location"));
Why do I get an error message saying 'cannot make a static reference t non-static method' on the last line. None of the methods being called are static, and I believe I have properly created the homeObj reference in this method.

Your class is called 'homeObj', and your last line is 'homeObj.<method>', which looks like a static call. The last line should say 'homeObject.setLocation...' instead.

Also, class names should start with an upper-case letter in Java. It helps avoid things like this.

DholmbladRU
May 4, 2006

Kilson posted:

Your class is called 'homeObj', and your last line is 'homeObj.<method>', which looks like a static call. The last line should say 'homeObject.setLocation...' instead.

Also, class names should start with an upper-case letter in Java. It helps avoid things like this.

thanks... I have been racking my brain over this for a little while. dyslexia at its finest..

Doctor w-rw-rw-
Jun 24, 2008

Kilson posted:

Your class is called 'homeObj', and your last line is 'homeObj.<method>', which looks like a static call. The last line should say 'homeObject.setLocation...' instead.

Also, class names should start with an upper-case letter in Java. It helps avoid things like this.
Indeed. It would behoove all Java programmers to remember to follow standard convention:
ClassName
variableName (or variable_name, but this is awkward)
CONSTANT_NAME

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Harold Ramis Drugs posted:

I'm working on a program to calculate Pascal's Triangle up to 16 rows, and then print it out as a (mostly) perfect isosceles triangle. I'm having trouble coding a formula to do this cleanly though, and the only appearant solution I can think of is to write a method that will print each line of the triangle with unique directions for spacing. This is what I have so far:

code:
-snip-
Which prints this...

code:


                                1  
                              1  1  
                            1  2  1  
                          1  3  3  1  
                        1  4  6  4  1  
                      1  5  10  10  5  1  
                    1  6  15  20  15  6  1  
                  1  7  21  35  35  21  7  1  
                1  8  28  56  70  56  28  8  1  
              1  9  36  84  126  126  84  36  9  1  
            1  10  45  120  210  252  210  120  45  10  1  
          1  11  55  165  330  462  462  330  165  55  11  1  
        1  12  66  220  495  792  924  792  495  220  66  12  1  
      1  13  78  286  715  1287  1716  1716  1287  715  286  78  13  1  
    1  14  91  364  1001  2002  3003  3432  3003  2002  1001  364  91  14  1  
  1  15  105  455  1365  3003  5005  6435  6435  5005  3003  1365  455  105  15  1  
If you guys can give me any pointers on how to format my printing so that the correct numbers line up, I would be greatly appreciative. Thanks
In your output, look at the lines which seem to increase the length the most. What's different about the contents of these lines compared to the line above? Think of ways you can adjust for that difference. If you want to see how it's built, replace each space in your spacing println() statements with a different, visible character.

Max Facetime
Apr 18, 2009

Harold Ramis Drugs posted:

If you guys can give me any pointers on how to format my printing so that the correct numbers line up, I would be greatly appreciative. Thanks

Make it look like this:

code:
                                             x1xx
                                          xx1x..x1xx
                                       xx1x..x2xx..x1xx
                                    xx1x..xx3x..x3xx..x1xx
                                 xx1x..xx4x..x6xx..x4xx..x1xx
                              xx1x..xx5x..x10x..x10x..x5xx..x1xx
                           xx1x..xx6x..x15x..x20x..x15x..x6xx..x1xx
                        xx1x..xx7x..x21x..x35x..x35x..x21x..x7xx..x1xx
                     xx1x..xx8x..x28x..x56x..x70x..x56x..x28x..x8xx..x1xx
                  xx1x..xx9x..x36x..x84x..x126..126x..x84x..x36x..x9xx..x1xx
               xx1x..x10x..x45x..x120..x210..252x..210x..120x..x45x..x10x..x1xx
            xx1x..x11x..x55x..x165..x330..x462..462x..330x..165x..x55x..x11x..x1xx
         xx1x..x12x..x66x..x220..x495..x792..924x..792x..495x..220x..x66x..x12x..x1xx
      xx1x..x13x..x78x..x286..x715..1287..1716..1716..1287..715x..286x..x78x..x13x..x1xx
   xx1x..x14x..x91x..x364..1001..2002..3003..3432..3003..2002..1001..364x..x91x..x14x..x1xx
xx1x..x15x..x105..x455..1365..3003..5005..6435..6435..5005..3003..1365..455x..105x..x15x..x1xx

DholmbladRU
May 4, 2006
I am developing a small web app in jsp. All my dev is done with eclipse/tom cat. Well I have created all my java code for the back end, and now it is on to learn how to make a jsp page(first time). Well I am trying to create an instance of a java class on a jsp page. Where do I place this inside the eclipse directories? I saw somewhere in wen-inf/lib but that didnt seem to work.

trying to import with something like this
<%@page import="myClass.java"%>

I was able to import classes which were in the standard java package with the above code. Is there some additional step I am missing other than placing the .java file in the director?

code:

 <%
  //on-load set sessin variables to read from xml file
  //this array will be passed to controler(sortMethods) when user wants to search
  
  readXMLFile read=new readXMLFile();
  homeObj[] homesUnsorted = read.convert(read.read());
  
  session.setAttribute( "homesUnsortedSes", homesUnsorted );
  %>

Get error 'The import readXMLFile cannot be resolved' when I attempt to call the .java file by name in the import statement. Tried creating a build path to the /src directory and add the java files to it, but that didnt resolve it. Once I understand this aspect I should be able to get moving on the dev of this small project



<%@ page import="readXMLFile.java" %>


I did just find this resource online:
http://www.coderanch.com/t/288534/JSP/java/importing-class-file-jsp
I will try it when I get home.

DholmbladRU fucked around with this message at 15:26 on Jan 30, 2012

pigdog
Apr 23, 2004

by Smythe
Um, yeah. Java is a compiled language, you need to compile the .java file to a .class file before you can use it. The Java code that's contained in the JSP files is compiled automatically by Tomcat, but regular Java files need compiling before use.

Volguus
Mar 3, 2009

DholmbladRU posted:

words...
JSP's are just servlets, think of them this way. They get compiled into servlets by your application server (tomcat, jboss, etc.) .
What are servlets? http://en.wikipedia.org/wiki/Java_Servlet
Therefore, when you put in a statement like <%@page import...%> that becomes just an import myClass; in the generated servlet. And you already know that you can't import a .java file, just a class (compiled one).

One small piece of advice: avoid if you can java code in a jsp. Is just bad practice. Use tags (JSTL for example) instead.
Additionally, avoid logic in the JSP, use it just to present data. It will come handy if your application grows.

If this is a homework though...don't worry about any of this, just do whatever your professor told you.

DholmbladRU
May 4, 2006

rhag posted:

JSP's are just servlets, think of them this way. They get compiled into servlets by your application server (tomcat, jboss, etc.) .
What are servlets? http://en.wikipedia.org/wiki/Java_Servlet
Therefore, when you put in a statement like <%@page import...%> that becomes just an import myClass; in the generated servlet. And you already know that you can't import a .java file, just a class (compiled one).

One small piece of advice: avoid if you can java code in a jsp. Is just bad practice. Use tags (JSTL for example) instead.
Additionally, avoid logic in the JSP, use it just to present data. It will come handy if your application grows.

If this is a homework though...don't worry about any of this, just do whatever your professor told you.

Once I understand how to import my java classes(thank you for the above info) I will be calling a controler java class which will take the use input and pass it to some logic class, which will then return an array to be displayed on a "results".jsp. It isnt for homework. Thank you for the information.

Boz0r
Sep 7, 2006
The Rocketship in action.
I have one of these and at my school we're writing a synthesizer, and I'd like to use this thing to trigger it. (How) can I access information from it in Java?

DholmbladRU
May 4, 2006

rhag posted:

words

code:
  <%@ page import="build.clases.readXMLFile.class" %>
the .class files for the .java files in my dynamic web project are located in the workspace/project/build/classes. I created a build path to this directory and explicitly defined the .classes I want to use. But it still wont let me access them..:(. I know this is and should be very simple but I cant seem to get it going.

Contra Duck
Nov 4, 2004

#1 DAD

DholmbladRU posted:

code:
  <%@ page import="build.clases.readXMLFile.class" %>
the .class files for the .java files in my dynamic web project are located in the workspace/project/build/classes. I created a build path to this directory and explicitly defined the .classes I want to use. But it still wont let me access them..:(. I know this is and should be very simple but I cant seem to get it going.

Take the .class off the end. Also spell 'classes' correctly in your package name.

DholmbladRU
May 4, 2006

Contra Duck posted:

Take the .class off the end. Also spell 'classes' correctly in your package name.

ah sorry, was typing quickly into the forums. have tried with and without the .classes

with
code:
 <%@ page import="build.classes.readXMLFile" %>
it says "the import build could not be found"

lamentable dustman
Apr 13, 2007

🏆🏆🏆

DholmbladRU posted:

ah sorry, was typing quickly into the forums. have tried with and without the .classes

with
code:
 <%@ page import="build.classes.readXMLFile" %>
it says "the import build could not be found"

Is readXMLFile in a jar? If so what package is it in? build.classes dosen't sound like a package but the root of your compile directory.

Also follow the Java style guide. Classes start with a capital letter.

DholmbladRU
May 4, 2006

donglelord posted:

Is readXMLFile in a jar? If so what package is it in? build.classes dosen't sound like a package but the root of your compile directory.

Also follow the Java style guide. Classes start with a capital letter.

no, build and classes are directories in the workspace folder. readxmlfile is not a jar file, it is a .java class in my dynamic web project. I found the .class files in the workspace folder.

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
When you use "<@ page import", you're not importing a file, you're importing a java class. If you open the .java file, it will say "package x.y.z" at the top somewhere. Your import should look like 'import="x.y.z.<classname>"'.

DholmbladRU
May 4, 2006

Kilson posted:

When you use "<@ page import", you're not importing a file, you're importing a java class. If you open the .java file, it will say "package x.y.z" at the top somewhere. Your import should look like 'import="x.y.z.<classname>"'.

if I do "import=build.classes.readXMLFile" it states that the import build cannot be fond. readxmlFile is a .class file found in the directory in my previous comm.

Blacknose
Jul 28, 2006

Meet frustration face to face
A point of view creates more waves
So lose some sleep and say you tried
I need a good Spring/Hibernate textbook that walks through Spring MVC project setup etc. I've used Spring a lot but never done a complete greenfield project setup which I think is a pretty gaping hole in my knowledge.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Blacknose posted:

I need a good Spring/Hibernate textbook that walks through Spring MVC project setup etc. I've used Spring a lot but never done a complete greenfield project setup which I think is a pretty gaping hole in my knowledge.

I don't think you need a textbook to do this, it's really not that hard especially with annotations in 3.0+. I would just make a few simple hello world-ish projects to get used to all the initial boilerplate web.xml stuff and some of the gotchas with dispatcherservlet and you should be fine.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

DholmbladRU posted:

if I do "import=build.classes.readXMLFile" it states that the import build cannot be fond. readxmlFile is a .class file found in the directory in my previous comm.

This is because build.classes is not your package path. Your package path is com.company.division.project.reaadxmlfile or whatever. It will be at the very top of the .class file as "package whatever". If you don't declair a package just have import readxmlfile in your jsp page

cenzo
Dec 5, 2003

'roux mad?

DholmbladRU posted:

if I do "import=build.classes.readXMLFile" it states that the import build cannot be fond. readxmlFile is a .class file found in the directory in my previous comm.

Look in your .java file, the first few lines should look something like this:

code:
package com.foo.webservice.global;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.foo.webservice.dao.AgencyInfoDAO;

public class AgencyInfoService {
Looking at the package (first line) and the class (last line) you make your import statement look like this:

code:
import="com.foo.webservice.global.AgencyInfoService"

Blacknose
Jul 28, 2006

Meet frustration face to face
A point of view creates more waves
So lose some sleep and say you tried

TRex EaterofCars posted:

I don't think you need a textbook to do this, it's really not that hard especially with annotations in 3.0+. I would just make a few simple hello world-ish projects to get used to all the initial boilerplate web.xml stuff and some of the gotchas with dispatcherservlet and you should be fine.

I guess you're right. I have used Spring quite a lot, it's mainly just setup and some of the newer features. I guess I'll make some kind of noddy little webapp and see how it goes. Now if I can just stop getting distracted by Scala...

DholmbladRU
May 4, 2006

cenzo posted:

words

These .java files were created in this dynamic web page project in Eclipse. There is no "package" declaration at the top. They do fall under the "default package" in the Java Resources/src, can I call this default package?


code:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.w3c.dom.Entity;

 
public class readXMLFile {
	
	public List<homeObj> read(){
Here is a screenshot of my dynamic web project

http://img.photobucket.com/albums/v422/dholmblad/t1.png

As you can see the java classes I have written are in src>xml(package I just created). The jsp file is WebContent. Still unable to import these files:(

DholmbladRU fucked around with this message at 05:08 on Feb 1, 2012

lamentable dustman
Apr 13, 2007

🏆🏆🏆

The Java files aren't compiling. You can tell because the J in the icon isn't filled in. That means your folder called 'src' isn't set up as the source folder in eclipse. Looks like you set src/XML as your source folder.

You also have build/classes set as a source folder which isn't right

lamentable dustman fucked around with this message at 06:38 on Feb 1, 2012

DholmbladRU
May 4, 2006

donglelord posted:

The Java files aren't compiling. You can tell because the J in the icon isn't filled in. That means your folder called 'src' isn't set up as the source folder in eclipse. Looks like you set src/XML as your source folder.

You also have build/classes set as a source folder which isn't right

src is now the source folder, and the J icon is filled in on all of the java classes.
Java Resources>src>(default package)>all the java files

How do I import one of these classes into jsp, it is really killing me. Seems so simple I just cant get it right.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
You really should put your files into a package of some kind. Try
code:
package ru.dholmblad.ReadXMLFile;

public class ReadXMLFile {
.
.
.
}
then in your jsp:

code:
<%@page import="ru.dholmblad.ReadXMLFile"%>
.
.
.

The Selling Wizard
Apr 10, 2009

CAN I BUM A SMOKE
OFF YA, SPACE MAN
This might be an unanswerable question, but does anyone know what percentage of users have upgraded to JRE 1.7, as opposed to staying with the latest 1.6 update?

I mean, if I'm writing to something with JDK 1.7, how likely do you guys think I am going to be getting people coming back to me with some issue?

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

The Selling Wizard posted:

I mean, if I'm writing to something with JDK 1.7, how likely do you guys think I am going to be getting people coming back to me with some issue?
100%.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

The Selling Wizard posted:

This might be an unanswerable question, but does anyone know what percentage of users have upgraded to JRE 1.7, as opposed to staying with the latest 1.6 update?

I mean, if I'm writing to something with JDK 1.7, how likely do you guys think I am going to be getting people coming back to me with some issue?

The amount of people who like Java applets is about the same amount of people who have upgraded to 1.7

DholmbladRU
May 4, 2006

TRex EaterofCars posted:

You really should put your files into a package of some kind. Try
[code
.

[/code

I am pretty sure that worked, thank you so much.

The Selling Wizard
Apr 10, 2009

CAN I BUM A SMOKE
OFF YA, SPACE MAN
I figured it was something obvious like that. Thank you.

geeves
Sep 16, 2004

This is Struts 2 specific - but <html:select /> won't apply the error css class or style when I add addFieldError(String elementName, String errorText).

Google has failed me and all I've been able to find is discussion about the addFieldError method in general. We don't have any special tags to further handle the struts tags in case of an error.

Edit: figured it out. html:select needs cssErrorClass to be set. Which for some reason is not an attribute that's required for textfields / areas / etc.

geeves fucked around with this message at 17:23 on Feb 2, 2012

Clanpot Shake
Aug 10, 2006
shake shake!

I've got an object that has two private members: a String and an int. I've got a container of these objects and I need to sort them in two different ways: alphabetically using the String, and numerically using the int. Implementing Comparable would only allow me to do one of these.

I recall (possibly from other languages I've done) that you can define the comparison function to use during the call to sort the container (sort of like an in-line method declaration). What is this structure called, and can someone point me to a good example of it?

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
You're thinking of a comparator. Here's the learning trail.

Edit: and if you really want to define it inline you'll use the Anonymous Class facility: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.
I'm building a little something for school where a system has a number of different types of accounts (eg: admins, mods, shmoes) and my wish is to create a default 'Account' class and then to extend it to the various other classes, but I think I may be cornering myself by being too eager to use inheritance.


Question: If my Account class implements serializable so that I can write Account files, would my read/write methods in the parent class be properly inherited by the subclasses? ie, reading an Account object from its file in the parent method would require typing the object as an Account - so would typing a Shmoe as an Account destroy his attributes which are distinct to Shmoes?

eg, from the 'Account' class,
code:
	public Account open(String l){
		try{
		  	ObjectInputStream load =
		  		new ObjectInputStream(
		  		new FileInputStream("accounts/"+l));
		  	Account a = (Account) load.readObject();
		  	load.close();
		  	return a;
		  } catch(Exception e){
		  	System.out.println("There is no account with this username.");
		  	return null;
		  }
	}
would this method work for subclasses?

Luminous
May 19, 2004

Girls
Games
Gains
You may want to consider an account as, well, just an account that has f.ex, a username and a role or a list of roles, where a role is an admin, a mod, or a schmoe (use inheritance here).

Then serializing an account and determining which actual role objects need to be instantiated is more straightforward.

Adbot
ADBOT LOVES YOU

DholmbladRU
May 4, 2006
I am having a hard time getting images to show up frm a jsp page

code:
<th rowspan="3"><IMG SRC="<%= results[i].getImgURL() %>"/></th>
how the image path is stored in the xml file
code:
<imageURL>/src/images/3.jpg</imageURL>
The image folder is in the src/images directry in the dynamic web project in eclipse


page source ends up being

code:
<th rowspan="3"><IMG SRC="/src/images/3.jpg"/></th>

when displayed in browser, which seems correct

DholmbladRU fucked around with this message at 17:04 on Feb 5, 2012

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply