Author Topic: Random simple java programs that actually do something useful.  (Read 33784 times)

0 Members and 1 Guest are viewing this topic.

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Random simple java programs that actually do something useful.
« on: December 09, 2010, 09:42:30 pm »
I made it for my homework. You have to compile it first.
You type the sentence and it converts the sentence into piglatin!
Code: [Select]

import java.util.*;
import java.lang.*;
public class Prog508a
{
    public static Scanner k=new Scanner(System.in);
    public static int yesorno=1;
    public static String YN="";
    public static int yncheck(String YesOrNo)
    {
        if(YN.equals("Yes"))
         yesorno=1;
        else if(YN.equals("No"))
         yesorno=0;
       
        else if( !(YN.equals("Yes") || YN.equals("No")) && (YN.equalsIgnoreCase("yes") || YN.equalsIgnoreCase("no")))
        {
            System.out.print("Capitalization is important Yes or No? ");
            YN=k.nextLine();
            yncheck(YN);
        }
        else
        {System.out.print("You must answer either Yes or No? ");
            YN=k.nextLine();
            yncheck(YN);
        }
        return yesorno;
    }
      public static void main(String args[])
    {       
        String str="",str2="",str3="";
        while(yesorno==1){
        System.out.println();
        System.out.print("Enter a sentence: ");
        str=k.nextLine();
        String[] sarray = str.split(" ");
        int len = sarray.length;
        for(int i=0;i<len;i++)
        {
            char[] word = sarray[i].toCharArray();
            char firstletter = word[0];
            int len3=word.length;
            int len4=len3-1;
            if(firstletter!='a' && firstletter!='e' && firstletter!='i' && firstletter!='o' && firstletter!='u' && firstletter!='A' && firstletter!='E' && firstletter!='I' && firstletter!='O' && firstletter!='U')
            {
                for(int j=0;j<=len4-1;j++)
                {
                    word[j]=word[j+1];
                }
                word[len3-1]=firstletter;
            }           
            for(int l=0;l<=len3-1;l++)
            {
                str2=str2+word[l];
            }
            str3=str3+str2+" ";
            str2="";
        }       
        str3=str3.replace(" ","ay ");
        System.out.println(str3);
        str3="";
        System.out.print("Do you wish to convert another sentence? (Yes/No) ");
        YN=k.nextLine();
        yncheck(YN);   
       }
    }
}

 */
Optimizing comments thnx
« Last Edit: December 09, 2010, 10:26:24 pm by yeongJIN_COOL »
Sig wipe!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: java piglatin converter?
« Reply #1 on: December 09, 2010, 09:57:47 pm »
interesting. you know what i just thought of doing? censoring a string to take out all the swear words

edit: just finished it. it will change something like this:
Spoiler For language:
You fucking ass.
into
You endearing unicorn.

Code: [Select]
import java.util.*;

public class Censor{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.print("Enter a string to censor: ");
String evilness = reader.nextLine();
System.out.println("Your string in a more holy format: \n" + censor(evilness));
System.out.print("\nAgain? (y/n) ");
if(reader.nextLine().equalsIgnoreCase("y"))
main(null);                                    //recursive calls to main? i think yes.
}

public static String censor(String evilness){
String[] badWords = {"fuck","shit","damn","ass","FUCK","SHIT","DAMN","ASS"}; //yes, i know there are more swear words. this covers the basics.
String[] replacementWords = {"apple","magical","unicorn","fairies","endear","renowned","butterflies","sugar"};
Random r = new Random();
for(int i = 0; i < badWords.length; i++){
int indexOfBadWord = evilness.indexOf(badWords[i]);
if(indexOfBadWord != -1){
String begin = evilness.substring(0,indexOfBadWord);
String replacement = replacementWords[r.nextInt(replacementWords.length)];
String end = evilness.substring(indexOfBadWord + badWords[i].length());
evilness = begin + replacement + end;
i = -1;
}
}
return evilness;      //now holy.
}
}
« Last Edit: December 09, 2010, 10:39:04 pm by nemo »


Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: java piglatin converter?
« Reply #2 on: December 09, 2010, 10:14:01 pm »
woot :w00t:
Spoiler For Spoiler:
you butterfliesing butterflies
Sig wipe!

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #3 on: December 09, 2010, 10:24:57 pm »
Lol, "apple this magical unicorn". Interesting program, nemo. I love the comment on that return statement.

EDIT: What would be really funny, is to analyze the sentence, and get a new version that was "prettier" so that everyone talked pretty.

So "Hey, I think your program is a fail" becomes "I think your program is missing some butterflies, but other than that, I think the unicorns were happy."
« Last Edit: December 09, 2010, 10:26:28 pm by graphmastur »

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #4 on: December 09, 2010, 10:44:52 pm »
Lol, "apple this magical unicorn". Interesting program, nemo. I love the comment on that return statement.

EDIT: What would be really funny, is to analyze the sentence, and get a new version that was "prettier" so that everyone talked pretty.

So "Hey, I think your program is a fail" becomes "I think your program is missing some butterflies, but other than that, I think the unicorns were happy."

i think that's a little beyond my word processing abilities. it'd be cool if i made an advanced library to process strings, though.

also, if you want, you can just copy/paste my source code and add words. if you want to add the swear words list to include "snow" for some reason, you can do that. you can also add replacement words, if you wanted to replace snow with honey.

 maybe i could make a function that, when a certain word is spotted, it will always replace it with a certain other word...
« Last Edit: December 09, 2010, 10:46:03 pm by nemo »


Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #5 on: December 09, 2010, 10:51:40 pm »
Lol, "apple this magical unicorn". Interesting program, nemo. I love the comment on that return statement.

EDIT: What would be really funny, is to analyze the sentence, and get a new version that was "prettier" so that everyone talked pretty.

So "Hey, I think your program is a fail" becomes "I think your program is missing some butterflies, but other than that, I think the unicorns were happy."

i think that's a little beyond my word processing abilities. it'd be cool if i made an advanced library to process strings, though.

also, if you want, you can just copy/paste my source code and add words. if you want to add the swear words list to include "snow" for some reason, you can do that. you can also add replacement words, if you wanted to replace snow with honey.

 maybe i could make a function that, when a certain word is spotted, it will always replace it with a certain other word...
I'm sure you are referring to replacing Game with Lost, right?

Offline Happybobjr

  • James Oldiges
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2325
  • Rating: +128/-20
  • Howdy :)
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #6 on: December 09, 2010, 10:56:35 pm »
I wouldn't censor the game. When you see anything censored, you will assume and lose the game.
School: East Central High School
 
Axe: 1.0.0
TI-84 +SE  ||| OS: 2.53 MP (patched) ||| Version: "M"
TI-Nspire    |||  Lent out, and never returned
____________________________________________________________

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Random simple java programs that actually do something useful.
« Reply #7 on: December 10, 2010, 02:13:38 am »
interesting. you know what i just thought of doing? censoring a string to take out all the swear words

edit: just finished it. it will change something like this:
Spoiler For language:
You fucking ass.
into
You endearing unicorn.

Code: [Select]
import java.util.*;

public class Censor{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.print("Enter a string to censor: ");
String evilness = reader.nextLine();
System.out.println("Your string in a more holy format: \n" + censor(evilness));
System.out.print("\nAgain? (y/n) ");
if(reader.nextLine().equalsIgnoreCase("y"))
main(null);                                    //recursive calls to main? i think yes.
}

public static String censor(String evilness){
String[] badWords = {"fuck","shit","damn","ass","FUCK","SHIT","DAMN","ASS"}; //yes, i know there are more swear words. this covers the basics.
String[] replacementWords = {"apple","magical","unicorn","fairies","endear","renowned","butterflies","sugar"};
Random r = new Random();
for(int i = 0; i < badWords.length; i++){
int indexOfBadWord = evilness.indexOf(badWords[i]);
if(indexOfBadWord != -1){
String begin = evilness.substring(0,indexOfBadWord);
String replacement = replacementWords[r.nextInt(replacementWords.length)];
String end = evilness.substring(indexOfBadWord + badWords[i].length());
evilness = begin + replacement + end;
i = -1;
}
}
return evilness;      //now holy.
}
}
Haha this is awesome. ;D
« Last Edit: December 10, 2010, 02:14:22 am by DJ Omnimaga »

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #8 on: December 10, 2010, 02:15:29 am »
interesting. you know what i just thought of doing? censoring a string to take out all the swear words

edit: just finished it. it will change something like this:
Spoiler For language:
You fucking ass.
into
You endearing unicorn.

Brilliant. IRC should have this so we can fool around with it.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Random simple java programs that actually do something useful.
« Reply #9 on: December 10, 2010, 02:17:54 am »
Not public commands, though. Earlier I had to mute the IRC channel (disable it completely) to stop a spam fest because no one would listen. Imagine with bot commands, now. X.x

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #10 on: December 10, 2010, 02:19:05 am »
Not public commands, though. Earlier I had to mute the IRC channel (disable it completely) to stop a spam fest because no one would listen. Imagine with bot commands, now. X.x

The quote spamming? I was one of the ones devoiced, DJ :P

It'd still be awesome for us human bots.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Random simple java programs that actually do something useful.
« Reply #11 on: December 10, 2010, 02:19:50 am »
Ah right you were on. But still, I don,t want this to turn into a spam fest. That kind of bot should be kept for other channels.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #12 on: December 10, 2010, 02:20:52 am »
Yeah, right before you devoiced everyone I was telling you how I probably wasn't going to release Black for the z80.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Random simple java programs that actually do something useful.
« Reply #13 on: December 10, 2010, 02:36:08 am »
Ah, right. I wish you released the source or something, though, in case someone wanted to take over. :(

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #14 on: December 10, 2010, 02:37:36 am »
Methinks you might have found String.replace(String original,String replacement) helpful :)