Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

How do I into java

Name: Anonymous 2011-06-03 19:02

Any Javafags here?

So I'm trying too write a program that will print the letters of a string backwards on each line, but I can only get it too print the last letter of the string infinitely. Can anyone show me how I would get the result I want?

This is what I wrote so far:

String word = "anything";
        int length = word.length();
        char letter = word.charAt(length-1);
        while(length > 0)
        System.out.println(letter);

I'm trying to teach my self java, so any help is much appreciated.

Name: Anonymous 2011-06-03 19:10

>>1
Okay, I'll humour you:

You don't reduce your integer by one after each time you've printed the letter.



String word = "anything";
         int length = word.length();
         char letter = word.charAt(length-1);
         while(length > 0)
         System.out.println(letter);
         length == length-1;
         letter == word.charAt (length-1);

Name: Anonymous 2011-06-03 19:13

>>2
Okay, I see what you're saying

I was trying to do it completely wrong.

Name: Anonymous 2011-06-03 19:17

>>3
Hm, well, actually I believe that you should have something like



String word = "anything";
         int length = word.length();
         char letter = word.charAt(length-1);
         while(length > 0)
{
         System.out.println(letter);
         length == length-1;
         letter == word.charAt (length-1);
}


In the end of it because the while-loop now takes more than one expression.

And yeah, you were trying to do it completely wrong, is this your first language?

Name: Anonymous 2011-06-03 19:18

>>2
okay nevermind, that just gives a syntax error.

Name: Anonymous 2011-06-03 19:19

>>4
It might as well be, I learned Alice in highschool but I didn't get a whole lot out of that besides the absolute basics of programming.

Name: Anonymous 2011-06-03 19:24

>>5
Yeah, see >>4.

>>6
Oh yeah, Alice. Hm, anyway, wanna try out something else? Oh, and also: start to sage your posts, it's considered polite here.

Anyway, download this:
http://thepiratebay.org/torrent/6076908/Land_of_Lisp.pdf

When you've read the book you can come back here and we'll give you more cool literature.

Name: Anonymous 2011-06-03 19:29

I guess I'll give lisp a shot then.

I think part of the trouble I'm having with java though is I stopped practicing it for a month or so and I forgot a lot of the basics..

Name: Anonymous 2011-06-03 19:42

String word = "anything";
for(int i = word.length - 1; i >= 0; i--) {
    System.out.println(word.charat(i));
}

Name: nambla_dot_org_rules_you 2011-06-03 19:44

>>1
You mean like the following...

/*
 *Begin the  rogaine and viagra session.
 */

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner userIn = new Scanner(System.in);
        System.out.printf("Enter a word: ");
        String word = userIn.nextLine();

        char[] reverse = word.toCharArray();
        int length = reverse.length;

        for (int i = reverse.length - 1; i >= 0; i--) {
            System.out.println(reverse[i]);
        }
    }
}

Name: nambla_dot_org_rules_you 2011-06-03 19:45

And the output....


run:
Enter a word: anything
g
n
i
h
t
y
n
a
BUILD SUCCESSFUL (total time: 3 seconds)

Name: nambla_dot_org_rules_you 2011-06-03 19:50

Yeah, I forgot to remove the scaffolds...

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner userIn = new Scanner(System.in);
        System.out.printf("Enter a word: ");
        String word = userIn.nextLine();

        char[] reverse = word.toCharArray();

        for (int i = reverse.length - 1; i >= 0; i--) {
            System.out.println(reverse[i]);
        }
    }
}


So I was able to do this in 16 lines of Java code on the first attempt and the entire thing took me 5 minutes.

Name: Anonymous 2011-06-03 20:19

$ php -r "print strrev(implode(' ', array_slice($argv, 1)));" this is not a test
tset a ton si siht

Name: Anonymous 2011-06-03 20:22

>>13
But the OP was asking for Java you fucking retard.

Name: Anonymous 2011-06-03 21:05

Look at how fucking sexy C# is.

List<char> backwards = "Anything".ToArray<char>().Reverse().Where(n =>
{
    Console.WriteLine(n);
    return true;
}).ToList();

Look at that shit, enterprise, fucking, scalable.

LINQ tip of the day: .ToList() force the LINQ to execute immediately, rather than when in this case our list backwards is used.

Go learn a real language (like C#) instead of java.

Name: Anonymous 2011-06-03 21:32

>>15
So that we can end up working at taco bell like you?

Name: Anonymous 2011-06-03 22:39

yada

Name: Anonymous 2011-06-03 22:43

>>15
Look at that shit
You're right, it is shit!

Name: Anonymous 2011-06-03 23:15

>>14
#!/usr/bin/php -q
<?php

/**
 * Set of classes for handling advanced operations on strings,
 * as per application guidelines.
 *
 * @package AdvancedStringHandling
 * @author Anonymous
 */


interface ASH_IStringContainer
{
    public function __construct($string);
    public function __toString();
}

interface ASH_IFactory
{   
    public static function create($type);
}

class ASH_StandardStringFunctionAsMethodCallException extends InvalidArgumentException
{
    const UNSPECIFIED = 0;
    const UNRECOGNIZED = 1;
    const UNSUPPORTED = 2;
   
    public function __construct($code = self::UNSPECIFIED, $message = '')
    {
        $this->code = (int)$code;
        $this->message = (string)$message;
    }
}   

class ASH_FactoryException extends Exception { }
class ASH_StringContainerFactoryException extends ASH_FactoryException { }



/**
 * Base inheritance class for all string containers. Not to be used as a standard string container.
 *
 * @abstract
 * @see ASH_StandardString
 */
abstract class ASH_StringContainer implements Countable, ArrayAccess, ASH_IStringContainer
{
    protected $str = array();
   
    public function __construct($string = '')
    {
        $this->_set($string);
    }
   
    public function __toString()
    {
        return implode($this->str);
    }
   
    /**
     * Magic method for allowing chainable standard string function calls on the string container's internal string.
     *
     * @final
     * @param mixed ... Use whichever variables are appropriate for the function call you want to make
     * @return $this if the result of the call was null, or if the call modified the internal string/array, otherwise returns the value of the call
     */
    final public function __call($name, $arguments)
    {
        switch ($name)
        {
            case 'echo':
            case 'print':
                return call_user_func_array(array(__CLASS__, '_echo'), $arguments);
            break;
           
            default:
                try
                {
                    $rf = new ReflectionFunction($name);
                    $offset = -1;
                   
                    // determine where our stored string is supposed to go in the argument list
                   
                    foreach ($rf->getParameters() as $paramNum => $param)
                    {
                        switch ($param->getName())
                        {
                            case 'glue':
                            case 'subject':
                            case 'str':
                            case 'str1':
                            case 'string':
                            case 'hebrew_text':
                                $offset = $paramNum;
                            break 2;
                        }
                    }
                   
                    if ($offset === -1)
                    {
                        throw new ASH_StandardStringFunctionAsMethodCallException(
                            ASH_StandardStringFunctionAsMethodCallException::UNSUPPORTED,
                            "Function {$name} not a supported standard string function to use as a magic method with StringContainer."
                        );
                    }
                    else
                    {
                        // insert our stored string in the appropriate space in the argument list before we pass it to the function
                        array_splice($arguments, $offset, 0, (string)$this);
                                               
                        $retval = $rf->invokeArgs($arguments);
                       
                        // should we return a (chainable) reference to self, or keep the value given to use by the call?
                        if (is_string($retval) || is_array($retval))
                        {
                            // this function intends to modify the stored value
                            $this->_set($retval);
                            $retval =& $this;
                        }
                        elseif(is_null($retval))
                        {
                            $retval =& $this;
                        }
                        return $retval;
                    }
                }
                catch (ReflectionException $e)
                {
                    throw new ASH_StandardStringFunctionAsMethodCallException(
                        ASH_StandardStringFunctionAsMethodCallException::UNRECOGNIZED,
                        "Function {$name} is not a known function."
                    );
                }
            break;
        }
       
    }
   
   
    /**
     * Manages the intricacies of our internal "string array"
     * @return $this
     */
    private function _set($string)
    {
        $this->str = str_split(is_array($string) ? implode(' ', $string) : (string)$string);
    }
   
    /**
     * Chainable command for outputting current state of the string.
     * @return $this
     */
    private function _echo($prepend = '', $append = '')
    {
        print($prepend . (string)$this . $append);
        return $this;
    }
   
    function prepend($string)
    {
        $this->_set($string . (string)$this);
        return $this;
    }
   
    function append($string)
    {
        $this->_set((string)$this . $string);
        return $this;
    }
   
    function set($string)
    {
        $this->_set($string);
    }
   
/// Countable implementation
 
    public function count()
    {
        return sizeof($this->str);
    }

/// ArrayAccess implementation
   
    public function offsetSet($offset, $value)
    {
        $this->str[$offset] = (string)$value;
    }
   
    public function offsetExists($offset)
    {
        return isset($this->str[$offset]);
    }
   
    public function offsetUnset($offset)
    {
        $this->str = array_splice($this->str, $offset, 1);
    }
   
    public function offsetGet($offset)
    {
        return $this->str[$offset];
    }   
}

/**
 * Standard string container, default created by factory
 */
class ASH_StringContainer_Standard extends ASH_StringContainer { }

/**
 * Standard string container, default created by factory
 */
class ASH_StringContainer_Reverse extends ASH_StringContainer
{
    public function __construct($string)
    {
        parent::__construct($string);
        $this->str = array_reverse($this->str);
    }
}


abstract class ASH_Factory implements ASH_IFactory
{
    /**
     * used to cache classname bases (expensive string operations)
     * @internal
     */
    private static $classnameBaseLookup = array();
   
    /**
     * Private constructor prevents inherited classes from being able to instantiate
     * @private
     */
    private function __construct()
    {
    }
   
    /**
     * Default implementation of factory creation.
     * If the class to be created does not follow naming conventions,
     * or requires arguments in the constructor, you must override $create.
     *
     * @return object classname decided by naming convention
     * @see getClassName
     * @see getClassNameBase
     */   
    public static function create($type)
    {
        $className = self::getClassName($type);
        if (class_exists($className))
        {
            return new $className();
        }
        else
        {
            throw new ASH_FactoryException("Unknown class type {$containerType} for factory " . get_called_class());
        }
    }
   
    /**
     * @return string Full class name being called by the Factory (by convention)
     */   
    protected static function getClassName($type)
    {
        $classSubname = preg_replace('/[\s]+/', ' ', ucwords(preg_replace('/[^a-zA-Z ]/', '', (string)$type)));
        return self::getClassNameBase() . "_{$classSubname}";
    }
   
    /**
     * @return string Base class name prefix for which the inherited factory is associated (by convention)
     */
    protected static function getClassNameBase()
    {
        $classname = get_called_class();
        if (!isset(self::$classnameBaseLookup[$classname]))
        {
            $parts = explode('_', $classname);
            if ('Factory' === end($parts))
            {
                array_pop($parts);
            }
            self::$classnameBaseLookup[$classname] = implode('_', $parts);
        }
        return self::$classnameBaseLookup[$classname];
    }
}

abstract class ASH_StringContainer_Factory extends ASH_Factory
{
    const DEFAULT_CONTAINER_TYPE = 'Standard';
   
    public static function create($type = self::DEFAULT_CONTAINER_TYPE)
    {
        return self::createString('', $type);
    }
   
    public static function createString($string, $type = self::DEFAULT_CONTAINER_TYPE)
    {
        $className = self::getClassName($type);
        if (class_exists($className) && is_subclass_of($className, self::getClassNameBase()))
        {
            return new $className($string);
        }
        else
        {
            throw new ASH_StringContainerFactoryException("Unknown StringContainer type {$type} (expected class {$className})");
        }
    }
}


/**
 * Inline Unit Test (IUT)
 * @internal
 */
if (isset($argc) && strstr($argv[0], basename(__FILE__)))
{
    // environment setup
    $argumentString = implode(' ', array_slice($argv, 1));
    $argumentString = $argumentString ? $argumentString : 'Default Unit Test String';
   
    // test
    ASH_StringContainer_Factory::createString($argumentString, 'Reverse')
        ->print()
        ->strrev()
        ->print("\n")
        ->strtoupper()
        ->append(' (I am typing in all caps to indicate that I am shouting)')
        ->print("\n");
}


$ php -f "AdvancedStringHandling_Main.php"
gnirtS tseT tinU tluafeD
Default Unit Test String
DEFAULT UNIT TEST STRING (I am typing in all caps to indicate that I am shouting)

Name: Anonymous 2011-06-03 23:24

>>19

line 90:
- case 'glue':
+ case 'pieces':

line 171:
- }
+ return $this;
+ }

Name: Anonymous 2011-06-03 23:56

While I'm here, do you guys recommend learning a language with an IDE like netbeans, or should I just start with notepad and a compiler?

Name: Anonymous 2011-06-03 23:58

>>21
You fail at trolling. I wont feed you.

Name: Anonymous 2011-06-04 0:00

>>22
I'm not even trolling, this is my first day at /prog/

But I guess every board on 4chan can find something too argue about.

Name: Anonymous 2011-06-04 0:18

>>23
Learn how to use Emacs, and use it. You have a good IDE for most languages out there.
Read SICP, now learn a Lisp. You have enough knowledge to learn any other language and get only the good from it, and

Name: Anonymous 2011-06-04 1:37

>>24
Emacs is just plain wrong. I'd sooner recommend eclipse, and it's written in Java for christ's sake.

Name: Anonymous 2011-06-04 1:40

learning Lisp on anything but Emacs
God help you.

Name: Anonymous 2011-06-04 2:41

>>26
This is one of the bigger obstacles to CL adoption today.  Of course, enduring the shitty editor isn't quite as bad as enduring the shitty evaluator, or enduring the shitty libraries.

Name: Anonymous 2011-06-04 3:12

>>23

vim is rather nice. I recommend that.

Or, if you prefer a GUI, you should use notepad++ if you use Windows.

Or you could install a linux distro. If you're new to those, I recommend Ubuntu, within which you should use gedit.

Name: Anonymous 2011-06-04 3:58


# pythonistas do it better
print '\n'.join(list(reversed('anything')))

Name: Anonymous 2011-06-04 4:13

>>21
Emacs and Vim are good, but if you're stuck on Windows for the rest of your life and can't figure out how to install a VM I'd go with SublimeText.

Name: Anonymous 2011-06-04 4:21

>>29
print '\n'.join(reversed('anything'))

Name: Anonymous 2011-06-04 4:36

You can't spell `Ashkenazi' without `Nazi'!   ;)

Name: Anonymous 2011-06-04 5:11

>>28
I think you mean:

vim is rather nice. I recommend that.
Or, if you prefer a GUI, you should use gvim if you use Windows.
Or you could install a linux distro. sage for ubanto within which you should use vim/gvim.


Seriously. You can use vim or gvim anywhere. There's no reason not to use it on one system and not on another.

>>30
I found Sublime was more interested in flirting with my GPU than helping me write code. While I was evaluating Windows editors a while back, I found e was quite good, in a comprehensive sense. vim on Windows still beats it, but worth having nonetheless.

>>29,31
Am I reading that right? Is join() really a method on the glue string in Python?

Name: >>33 Fix. 2011-06-04 5:20

There's no reason to use it on one system and not on another.

Name: Anonymous 2011-06-04 6:02

>>33
Am I reading that right? Is join() really a method on the glue string in Python?
Yes, can't you see how clean, elegant and pythonic it is?

Name: Anonymous 2011-06-04 6:03

I'm sure there's enough vim scripts to turn it into an above-par Lisp editor.

Name: Anonymous 2011-06-04 7:24

>>35
It's about as elegant as making all functions know their names but not letting the programmer change the __name__ field of functions, or making print a keyword instead of a function.

Name: Anonymous 2011-06-04 8:17

>>37
ELEGANT AS FUCK

Name: Anonymous 2011-06-04 13:41

It's certainly pythonic, I seem to recall deciding it wasn't worth it when all the string methods turned out to be ass-backwards.

Name: Anonymous 2011-06-04 22:04

Use Eclipse for Java.
Or at least that's what I'd do.
I use Emacs for my Python and Lisp, never done a single line of Java. Which is why I have no job.

Name: Anonymous 2011-06-04 22:47

>>40
eclipse is a bloated pile of shit

Name: Anonymous 2011-06-04 23:10

>>41
You say that like you're not posting in a Java thread... or comparing eclipse to emacs for that matter.

Name: Anonymous 2011-06-05 0:57

Learn C# instead.

everythingwentbetterthanexpected

Name: Anonymous 2011-06-05 1:17

>>43
IAWTP

Name: Anonymous 2011-06-05 1:44

guys I accidentally java

Name: Anonymous 2011-06-05 2:17

>>45

The whole java?!

Name: Anonymous 2011-06-05 2:32

>>19 here.

I'm kind of disappointed that I spent time on this elaborate "Java is Enterprise" joke, to nary a chuckle from my fellow /prog/lodytes.

Such is life, I suppose.

Name: Anonymous 2011-06-05 2:45

>"Java is Enterprise" joke
But it is not a joke, Enterprise=Java or .NET

Name: Anonymous 2011-06-05 5:57

>>48
Learn to quote, learn to sage.

Name: Anonymous 2011-06-05 6:09

>>49
Become autistic, post on /prog/ everyday.

Name: Anonymous 2011-06-05 10:14

>>49
What is the bb-code for quotes? I know it's not [quote], so wtf?

Name: Anonymous 2011-06-05 10:38

The jews are after me.

Name: Anonymous 2011-06-05 11:01

>>51
Many /prog/ BBCode GUIDES were posted, just google them.

Name: Anonymous 2011-06-05 15:51

[u][o][b][spoiler]AUTISM[/spoiler][/b][/o][/u]

Name: Anonymous 2011-06-06 11:31

Name: Anonymous 2011-06-06 17:44


Don't change these.
Name: Email:
Entire Thread Thread List