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
Malfeasible
Sep 10, 2005

The sworn enemy of Florin

GregNorc posted:

So I have this really simple code I need to run. There seems to be a syntax issue with the negative number, but I'm not sure how to write it... do I put it in quotes or something?

code:
import java.text.DecimalFormat;

/** 
This program demonstrates the DecimalFormat class 
*/

public class Format4
{
	public static void main(String[] args)
	{
		float x; 
		x = -123.987;
		// Create a DecimalFormat object
		DecimalFormat formatter = new DecimalFormat("%10.1f");

		//display the four variable's contents
		System.out.println(formatter.format(x));
	}
}
Here's the error I get:


I'm very new to java, and extremely confused... I thought I just specified that x is a float? Why would the compiler tell me to make it a float if I just set it to float?

In Java you have to put an 'f' after float values.
So change the line where you assign x to :
code:
x = -123.987f;
Also, are you sure about the format "%10.1f" ? What do you want the output to look like?

Adbot
ADBOT LOVES YOU

Malfeasible
Sep 10, 2005

The sworn enemy of Florin
I have encountered a problem I cannot begin to figure out.

I have a static method that works perfectly when I test it in the main method of that same class. Here is the header:
code:
 public static Map<String, Object> getDataMap(int zipcode)
But when I try to call it from a different package the compiler tells me that it's a non-static method and cannot be referenced from a static context. I have googled this looking for anyone with the same problem but it's always people who haven't actually declared the method as static, which I clearly have. Why on earth would the compiler think that a static method is non-static? Here is the line of code where I attempt to call the method:
code:
java.util.Map<String, Object> zipData = ZipcodeJDBC.getDataMap(Integer.parseInt(zipcode));
I have been scouring my computer deleting old class files or old jar files that the compiler might be seeing instead of seeing the current version I have quoted here. I do not think it is a case of javac looking at an old class file from before I declared the method as static.

Also, I get a warning that I have an unchecked conversion, as if the method were only returning a Map and not a Map<String, Object>. Is this because generics are stripped out at compile time and do not actually exist in the class file, or is it related to the other problem?

edit: ARG! I didn't scour hard enough. I found an old version of the file that is definitely the culprit. The unchecked conversion issue seams to have cleared up as well.

Malfeasible fucked around with this message at 23:28 on May 18, 2010

Malfeasible
Sep 10, 2005

The sworn enemy of Florin

Fitret posted:

...
it's not working:
...
I don't know Android programming, I only know some Servlet/JSP stuff but...

What is it doing exactly? Can you show the full response that you got back? Since you got the 200 status code I assume the server didn't choke on your request, but what did it send back to you?

Malfeasible
Sep 10, 2005

The sworn enemy of Florin

ynef posted:

tcpmon is quite good for this. You start it up, tell it to act as a proxy, it prints out both request and response. Simple and just what is most likely needed here.
This is awesome! This is something I've been looking for to trouble shoot web-services, but the only ones I could find were for windows.

Malfeasible
Sep 10, 2005

The sworn enemy of Florin

Aleksei Vasiliev posted:

Also don't ever do anything like this:

code:
		String s = "";
		for (int i = 0; i < sArray.length; i++)
		{
			s += sArray[i];		
		}
Because Strings are immutable, so every iteration is an object creation involving copying the previous String.
...

Just to elaborate on this:

The JVM keeps a kind of String pool and will not usually instantiate a brand new String if it knows it already has an equivalent String available, but you can crowd your String pool with abandoned Strings which eats up RAM.

StringBuilder and StringBuffer are mutable without abandoning the old Object and cluttering up any kind of Object pool.

Malfeasible
Sep 10, 2005

The sworn enemy of Florin
I have been trying to figure out a weird newline issue with JOptionPane. I already have a work around, but would still like to understand why it behaves the way that it does. Actually, System.out.println() show the same behavior so it's not a Swing quirk, I guess it's a matter of how newline is parsed in java.

This part stays the same:
code:
javax.swing.JOptionPane.showMessageDialog(null, message, title, javax.swing.JOptionPane.INFORMATION_MESSAGE);
The difference is how I set the message String. If I create the String in the java class that displays the message dialog the newline prints correctly:
code:
String message = "line 1\nline2";
But if I create the string containing the newline in a bash script and pass it as an argument to the java class that displays the message dialog, the \n newline prints out literally no matter what quotes or escapes I throw at it:
code:
#!/bin/bash
java ShowMyDialog "Oh, hello. \n You are in $PWD" &
I just don't understand how it makes any difference how the String is created, but it does. The '\n' char sequence must be getting parsed differently when it comes in as an arg so the resultant message Strings are different. I just don't understand this.

Adbot
ADBOT LOVES YOU

Malfeasible
Sep 10, 2005

The sworn enemy of Florin

rjmccall posted:

...
bash doesn't recognize backslash as an escape in double-quoted strings. Even outside of double-quoted strings, it doesn't turn \n into a newline. $'\n' gets expanded to a newline, but not inside double-quoted strings.

Regardless, your problem is with bash, not Java.

My major confusion is still with Java's behavior though, not Bash.
I wasn't trying to get bash to expand the \n into a newline because I didn't realize I had to pass Java an actual newline. I thought it would evaluate the \n character sequence as a newline whenever it encountered it when creating a String. I guess this only happens inside of double quotes in Java?

Whether inside double quotes or in the (String[] args) parameter in main, in my mind in both scenarios Java is creating a String from data containing the '\n' character sequence, but is parsing it as a newline in one case and as a literal char sequence in the other case. This still weirds me out!

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