[Java] making the text backwards - need help on project
Okay, so I need to make this code for my school, and it's really confusing.
Anyone know how to make the text backwards?
here's the code I have, its pretty messy.
PHP Code:
/**
*
Name: Danny
Assignment: IntroCS_2
Period: 10/11
Description: Write a program to input a word. Create a new String with the first letter of
the original String at the end, and the last letter of the original String at
the beginning. if the input were "computer", the output String would be "romputec"
Assume that no word will be less than 2 characters.
In accordance with the EBHS academic integrity policy, I have not copied, cheated, received
or given impermissible help on this assignment.
_________ (initial)
* a string is a collection or group of characters
* In Java a string is an object of the String class.
* All methods have (), and a semi colon at the each of the codes.
*/
public class ReverseChars {
public static void main (String[] args) {
do {
String testdata ;
testdata = IO.getString("testdata") ;
System.out.println(testdata.charAt(7)+testdata.substring(1,7)+testdata.charAt(0)) ;
}while( Doagain.ask() );
}
}
If anyone can help me, thanks.
//edit foxx
3. Use descriptive titles
Instead of "Need help!!" or "Look please!", explain in short what your title is about. Good titles are for example '[PHP] How to extract the title from a remote website' or '[MySQL] Problem with ordering tables by date'.
Re: [Java]Need help on project
I suggest finding a non-lazy way but enjoy
Code:
public class Reverse {
public static void main(String[] args) {
String test = "computer";
char firstChar;
char secondChar;
int length = test.length() - 1;
firstChar = test.charAt(0);
secondChar = test.charAt(length);
char[] array = new char[length];
array = test.toCharArray();
array[0] = secondChar;
array[length] = firstChar;
for(int i = 0; i < length+1; i++)
{
System.out.print(array[i]);
}
}
}
Whats happening:
Take string, determine length.
Get first and last char
turn string > character array
replace first and last char
Re: [Java]Need help on project
hmmm nice, okay thanks. I'll try this and see if it works~.
ooo nice it works. But, is there any simpler way? lol my teacher won't believe I did that code myself, cause we never learned arrays yet.
Re: [Java]Need help on project
I dont have JDK installed here to test, but it might work :P
I cant think on any better way :(
PHP Code:
public class Reverse {
public static void main(String[] args)
{
String input = "computer";
int len = input.length() - 1;
String newString = input.charAt(len) + input.substring(1, len) + input.charAt(0);
}
}
Re: [Java]Need help on project
Google: java string reverse, first match
Code:
String reverse = new StringBuffer(string).reverse().toString();
... all done.
edit: Oh, actually I only now read the code comments in full and realize full reverse wasn't what you wanted.
Re: [Java]Need help on project
Quote:
Originally Posted by
BBim
I dont have JDK installed here to test, but it might work :P
I cant think on any better way :(
PHP Code:
public class Reverse {
public static void main(String[] args)
{
String input = "computer";
int len = input.length() - 1;
String newString = input.charAt(len) + input.substring(1, len) + input.charAt(0);
}
}
Definitely works and alot simpler.
Re: [Java]Need help on project
Quote:
Originally Posted by
BBim
I dont have JDK installed here to test, but it might work :P
I cant think on any better way :(
PHP Code:
public class Reverse {
public static void main(String[] args)
{
String input = "computer";
int len = input.length() - 1;
String newString = input.charAt(len) + input.substring(1, len) + input.charAt(0);
}
}
thanks, I'll try it now.
awesome it works~ but u forgot the system.out.println. So I added it myself. thanks btw~!!!
o yea I have one more question.
Is this java or java script?
Re: [Java]Need help on project
Quote:
Originally Posted by
Ace
thanks, I'll try it now.
awesome it works~ but u forgot the system.out.println. So I added it myself. thanks btw~!!!
o yea I have one more question.
Is this java or java script?
Java.
Re: [Java]Need help on project
can u tell me whats the difference from javascript and java?
also what does the *import's do.
My teacher never told us thoroughly, he only said u need it.
But how do u know if u need the import, and where can u find these imports?
Re: [Java]Need help on project
Quote:
Originally Posted by
Ace
can u tell me whats the difference from javascript and java?
also what does the *import's do.
My teacher never told us thoroughly, he only said u need it.
But how do u know if u need the import, and where can u find these imports?
I didnt add the output cause it didnt say to output, it said to input but I forgot how in java so I didnt do it :P
Java is a language used for lots of things, server side applications, games, some IDE's were developped using java, it can be used on a website, etc.
Javascript is used on websites to "manage" the page, like change the tittle of the website, make the site more dynamic, etc.
It is interpreted by the web browser and there is a fight lol.
Once I saw something about javascript being used outside of a web page, but I didnt look much on it and dont remember.
Imports are used to get functions that are made but its not built in the compiler. Instead of the compiler having all functions and everything on it, there are other files with them, so you tell the compiler: "hey, I will use a function from that file" so it knows where to look for it.
You will need to use references to find which files you need, for example you need a function that gets a random number, then you find it and it is at a file that is not imported, so you import it and use the random function.
Re: [Java]Need help on project
Quote:
Originally Posted by
BBim
I didnt add the output cause it didnt say to output, it said to input but I forgot how in java so I didnt do it :P
Java is a language used for lots of things, server side applications, games, some IDE's were developped using java, it can be used on a website, etc.
Javascript is used on websites to "manage" the page, like change the tittle of the website, make the site more dynamic, etc.
It is interpreted by the web browser and there is a fight lol.
Once I saw something about javascript being used outside of a web page, but I didnt look much on it and dont remember.
Imports are used to get functions that are made but its not built in the compiler. Instead of the compiler having all functions and everything on it, there are other files with them, so you tell the compiler: "hey, I will use a function from that file" so it knows where to look for it.
You will need to use references to find which files you need, for example you need a function that gets a random number, then you find it and it is at a file that is not imported, so you import it and use the random function.
ooh, thanks for clearing the imputs for me.
So can people make inputs, for the compiler. And is it hard? Cause my teacher made some, and it looks hella hard to code.
Re: [Java]Need help on project
Quote:
Originally Posted by
Ace
ooh, thanks for clearing the imputs for me.
So can people make inputs, for the compiler. And is it hard? Cause my teacher made some, and it looks hella hard to code.
Did you mean imports?
Input is when the program gets something, from the user, from a file, from a touch monitor, mouse click, etc.
Output would be what the program shows on the screen, prints, etc.
I dont remember how to make imports on java, I had just like 4 months of java classes and didnt stick to it, I choose C++ :P
Re: [Java]Need help on project
Quote:
Originally Posted by
BBim
Did you mean imports?
Input is when the program gets something, from the user, from a file, from a touch monitor, mouse click, etc.
Output would be what the program shows on the screen, prints, etc.
I dont remember how to make imports on java, I had just like 4 months of java classes and didnt stick to it, I choose C++ :P
yea i meant imports lol srry.
Was half asleep when I typed that.
And yea, thanks for the help BBim. I'll make a new thread, with some other projects that I have to do.
Its for 2 whole semesters :P.
Re: [Java]Need help on project
Quote:
Originally Posted by
Ace
yea i meant imports lol srry.
Was half asleep when I typed that.
And yea, thanks for the help BBim. I'll make a new thread, with some other projects that I have to do.
Its for 2 whole semesters :P.
Wouldn't it be wiser to actually do the projects yourself?
Re: [Java] making the text backwards - need help on project
PHP Code:
import java.util.Scanner;
public class ReverseString
{
public static void main(String[] args)
{
Scanner scanInput = new Scanner(System.in).useDelimiter("\r\n");
String sInput = scanInput.next();
String sOut = "";
for (int i = sInput.length()-1; i >= 0; --i)
sOut += sInput.charAt(i);
System.out.println ("Origianl String: " + sInput + "\nReversed: "+ sOut);
}
}
Fully works. Have fun.
Re: [Java] making the text backwards - need help on project
Here's another obvious way, in C++ :wink:
PHP Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string str("abcdefghijklmnopqrstuvwxyz");
int sz = str.size();
for (int i = 2 - sz; i < sz - 1; i++)
{
int a(sz - abs(i) - 1);
int b(sz - abs(i) - 2);
str[a] ^= str[b];
str[b] ^= str[a];
str[a] ^= str[b];
}
cout << str << endl;
return 0;
}
Re: [Java] making the text backwards - need help on project
Quote:
Originally Posted by
Negata
PHP Code:
str[a] ^= str[b];
str[b] ^= str[a];
str[a] ^= str[b];
XOR, wat?
PHP Code:
#include <stdio.h>
#include <string>
int main(int argc, char *argv[])
{
char *szInput = (char *)malloc (512);
char *szOutput = (char *)malloc (512);
memset (szOutput, 0, 512);
scanf ("%[^\n]", szInput);
for (int i = strlen(szInput); i >= 1; --i) szOutput[i] = szInput[i];
printf ("Original: %s\nReversed: %s\n", szInput, szOutput);
free (szOutput);
free (szInput);
return 0;
}
Re: [Java] making the text backwards - need help on project
Quote:
Originally Posted by
Theoretical
XOR, wat?
He's trying to be clever. It's a way to exchange 2 variables without using a temporary variable.
Ok, let's review:
A ^ B = C
C ^ A = B
C ^ B = A
Now, two variables X and Y, we want to exchange them using XOR:
X = X ^ Y = Z (just as a reminder that it's the xor product)
Now, X is the Z value here, now we want to move X into Y:
Y = Y ^ Z = X
Now, X = Z and Y = X, one final move to make X be our Y value:
X = Z ^ Y = Z ^ X = Y
So now our two variables have been exchanged. It's a bit confusing calling it Z^Y on the last step because Y means X, it's notational, I didn't care to do it properly by denoting the original values as X_0 and Y_0 (primarily because subscripts are a pain in the ass to do in vBulletin without a math addon).
Re: [Java] making the text backwards - need help on project
Quote:
Originally Posted by
jMerliN
He's trying to be clever. It's a way to exchange 2 variables without using a temporary variable.
Ok, let's review:
A ^ B = C
C ^ A = B
C ^ B = A
Now, two variables X and Y, we want to exchange them using XOR:
X = X ^ Y = Z (just as a reminder that it's the xor product)
Now, X is the Z value here, now we want to move X into Y:
Y = Y ^ Z = X
Now, X = Z and Y = X, one final move to make X be our Y value:
X = Z ^ Y = Z ^ X = Y
So now our two variables have been exchanged. It's a bit confusing calling it Z^Y on the last step because Y means X, it's notational, I didn't care to do it properly by denoting the original values as X_0 and Y_0 (primarily because subscripts are a pain in the ass to do in vBulletin without a math addon).
I know what XOR is, I'm asking why he's trying to use it...