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
Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

clayburn posted:

Simple question I think. I have a very long string, that I want to take all n-length substrings out of and place into an array. For example if n is 5 and the sentence is "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt whatever", I would want string one to be "Lorem ipsum dolor sit amet,"
, string two to be "ipsum dolor sit amet, consectetur", string three to be "dolor sit amet, consectetur adipisicing", and so on. What would be the best way to go about this? I am having a hard time getting anything to work really.
I don't know anything about Java's regex libraries, but I would think that a regex could do this fairly easily.

Adbot
ADBOT LOVES YOU

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

craig8429 posted:

snip
Put your code in [code ][ /code] blocks, and use proper indentation, like thus:
code:
//This is a program to tell simple intrtest
//Written by 
//october 5th, 2008
//JDK 1.6

import javax.swing.*;
import java.text.*;
public class Chp4prac1
{
    public static void main (String [ ] args)
    {
        String response;
        response = JOptionPane.showInputDialog(null, "Dollars you would like to borrow");
        double Dollars = Double.parseDouble(response);
        {
            String response;
            response = JOptionPane.showInputDialog(null, "Interest is");
            double Interest = Double.parseDouble(response);
            String response;
            {
                response = JOptionPane.showInputDialog(null, "years");
                double years = Double.parseDouble(response);
                Interest over time=Dollars*Interest*years; 
                {
                }// ends main method
            }// ends program
Notice a problem?

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!
Fake Edit: Or just do what 1337JiveTurkey says. Though you did close the catch block, it just didn't look like it because the indentation was messed up.

cyberburrito posted:

looping stuff
I have never used java before, but break, well, breaks out of the loop. If you want to skip back to the beginning of the loop, you want to use the continue statement.

Also, I think it would make more sense to put the rest of the logic inside the try block, because otherwise you catch the exception and then keep going inside the loop as if everything were alright, which it isn't.

And this isn't a java issue, but I'd put the increment of n at the end of the loop. Otherwise, every time someone tries a bad input, you skip a number, which I assume is not desired.

So, 'my' code would look like
code:
import java.util.*;
import java.lang.Integer;
public class Main {	
        public static void main(String[] args ) {
		// TODO Auto-generated method stub
		final int SENTINEL =-1;
		final int MAX = 120;
		int total = 0;
		double average = 0;
		int highest = 0;
		int lowest = 0;
		int n=0;
		int next =0;		
		int [] grade = new int[MAX];
		Scanner keyboard =	new Scanner(System.in); 
		System.out.println("Welcome to the test score program");
		while(next != SENTINEL)
		{
			n = n+1;
			System.out.println("Please enter a test score (-1 to exit)");
			try{

				next = keyboard.nextInt();
			
				if(next != SENTINEL)
				{
					if(next > 100 || next < 0)
					{
						System.out.println("Invalid choice");
						continue;
					}
					grade[n] = next;
					total = total + next;
					//System.out.println(total);
					n = n+1;
				}
			}
			catch(InputMismatchException nFE) {
				     System.out.println("Not an Integer");
			}
		}
	}
}

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!
From Sun's Java Documentation

quote:

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
So you will need to manually write the line feed yourself.

E:F,b.

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

HFX posted:

people in general write terrible code and you will spend hours fixing their gently caress ups.
They confuse homophones too.

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

Colonel J posted:

Anybody have an idea what's wrong with the second bit of code?
You almost certainly had a typo or copy/paste screw up somewhere in "the program".

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

Kilson posted:

I can see the code entering doThing1(), because of the log statement. However, at some point in doThing1(), something is happening to cause all the rest of the code to be skipped. All remaining code in doThing1(), and even all remaining code in someMethod() is simply not executed. I tried the catch(Throwable) to see if I was missing something, but there's nothing.

Is there any possible explanation for such behavior? I could try to give some more specific details, but I don't know if they'd really be helpful.
Can you step through it in a debugger?

Alternatively, pepper in some more log statements to narrow down the problem.

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!
Unless he did a fantastically bad job of explaining his problem, I think he was saying that the program was dieing, but even once he added in the try...catch no exceptions were being caught.

If this was actually his problem, then you have some excellent question deciphering skills.

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

wigga please posted:

Hi, my education focuses on .NET but I'm trying to get some Java experience under my belt as well. I was trying my hand at Hibernate, specifically the exercise from the Netbeans site using the default MySQL sakila DB.

First thing I noticed is that in said exercise I'm supposed to use a concatenated string to build the HQL query so naturally the :siren: injection vulnerability:siren: went off in my head.

I tried to use a parametrized query but ran into some trouble:
code:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: 
Parameter value does not exist as a named parameter in [from Actor a where :column like '%:value%']
in this method:
code:
private void executeHQLQuery(String column, String value)
    {
        String qString = "from Actor a where :column like '%:value%'";
        try
        {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query q = session.createQuery(qString);
            q.setString("column", column);
            q.setString("value", value);
            List resultList = q.list();
            displayResult(resultList);
            session.getTransaction().commit();
        }
        catch (HibernateException he)
        {
            he.printStackTrace();
        }
    }
I'm not sure what causes this (maybe the '%:value%' part?), any ideas?
While your desire to avoid injection is admirable, unless HQL parameterized queries are significantly different from most SQL parameterized queries, you can't do what you want to do. Things like table and column names cannot be parameters in a query, so if the column name is dynamic, you will have to build at least part of the query string dynamically. Obviously you should take care to ensure that you are only building the query with valid column names, but there is no way around it.

As for the bit with :value, you can either put the % on either side of the string before you bind it to the query parameter, or you can do '%'+:value+'%' in your query.

Adbot
ADBOT LOVES YOU

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

Magicmat posted:

It turns out I need one more generic CS course for school, and I picked intermediate Java. The description reads "[f]undamentals of encapsulation, inheritance, polymorphism, abstraction, method overloading and overriding, exception handling, GUI components, event handling, multimedia programming, and input/output streams are introduced."

Now I know C, C++, C#, Ruby, and a smattering of other languages to varying levels of proficiency, but not Java. Given that, what's the best way to quickly get up to speed with Java?

As you can see, the course isn't hard -- all of the things listed are topics I'm familiar with in other languages. C# is also close enough to Java that the syntax and overall concepts are all familiar. But I'm unfamiliar with the specifics of Java, especially it's libraries and quirks.

I'm perusing some "Learn Java" books right now, like Head First Java, and while they'll work, they're a bit basic. I mean, I'm still at their start, but they mostly have "this is a 'Class'. Can you say 'Class'?" I've thought about just going over the Java API docs, but I feel that won't give me a good working base knowledge. Being a school course, I can't spend time bumbling through each assignment trying to kludge through the syntax quirks and language gotchas I never learned.

Is there a book out there that already assumes OO familiarity, and that I'm not an idiot, that can teach me Java? I just need it to an intermediate level, but I do need a strong grounding in Java itself.
The "pre-requisite" Java courses are probably just the equivalent of the Learn Java books, so skimming those should be more than enough to get you up to speed.

Besides, Java is just C# but more verbose and with the sharp edges left unfiled.

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