So I have this intro to Java class and I am having difficulty because my teacher is a senile 75-year old lady who thinks that teaching us how many bits are in different thing is more important then teaching us how to actually program.
Our assignment is to "Write a program that will read a line of text as input and then output the line with the first word moved to the end of the line."
We have to use strings, and can't use anything complex like if statements because we are not there in our crappy book yet. Does anyone know how to do this? Here is the code I have written so far, I just don't know how to use the strings to get it done.
<font class="small">Code:</font><hr /><pre>
import javax.swing.*;
public class wordchanger
{
public static void main(String[] args)
{
String phrase, output;
phrase = JOptionPane.showInputDialog(null,
"Please enter a line of text. Don't use any punctuation please.");
JOptionPane.showMessageDialog(null, "Your re-worded phrase is: " + output);
System.exit(0);
}
}</pre><hr />
Replies
You don't learn by having someone else do your work. And generally command line is easier to start off with so avoid gui until you understand the basics of java itself.
import java.lang.*;
public class help
{
public static void main (String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userInput = br.readLine();
int wordends = userInput.indexOf(' ');
String firstWord = userInput.substring(0, wordends);
int stringLength = userInput.length();
int beginNew = wordends + 1;
String stringCheese = userInput.substring(beginNew, stringLength);
stringCheese = stringCheese + " " + firstWord;
System.out.println(stringCheese);
}
} </pre><hr />
Since you couldn't use loops or conditionals that made it a pain and very messy
The only reason I did it for you is so I can explain it to you. I found it a lot harder to learn without a programmer explaining to me what each thing does.
On the first line of the main method I set up an instance of BufferedReader, and on the next line I set a string equal to the input taken by BufferedReader. On the next line I used the method indexOf to get the location in the string where the first space is used and set that to a variable (wordends). I then stored the first word of the input by assigning to a variable called firstWord using the substring method. The substring method works by grabbing whatever characters are between the locations you input (0, wordends). I then set an integer to the userInput string length using the length() method. Since we want the output to start after the first word along with the space, we set the starting location for the next substring to beginNew (which is wordends + 1). After we have the stringCheese string without the first word or the first space, we use basic concatenation to add the firstWord variable.
Hope this helps!
Fuck you.
Thanks for the link Keg. Looks like a good site.
I would normally not ask for help like this, however my tutor was not able to help me this week and this assignment is due Monday. I don't have the time to read 100+ pages in a book when I cam carrying six other classes as well.
Fuck you, you gave him the answer. You should have explained how you would go about it and then let him try to write the code. But no, instead you did his homework for him.
Fuck you.
[/ QUOTE ]
Wow Noel, your emoblog is true to your character. It sucks to learn programming when you have a shitty teacher and no one to guide you. I've always learned easier when I can see how a problem is done and have it explained to me, then I can do the exercise over myself. So calm down grandpa.
And also I used BufferedReader in my very first Java class in high school many years ago when we were working on "Hello World" shit.
Don't people come on here and ask how to do something like normal mapping and people help them out? Whats the diffrence?
I've been in numerous situations where teachers, instead of actualy doing their job and, well, teaching, just handed out assignments and told their students to figure it out on their own. Fuck them, if they dont want to do their job the student shouldnt be punished via poor grades and metal stress because of it.
Also, whats wrong with showing someone how somethings done? Explaining in general terms how something works in any sort of programming language only works if the person who's having it explained to them has a good grasp of the fundamentals of said language. But if their just starting out then their going to need specifics, otherwise any minute mistake they make will break the whole thing. You can't tech a beginner the same way you teach a pro or someone in between. This is why its said that starting is always the hardest part for so many disciplins.
If we had someone in here asking for assistance with a texture would you give them a link to Ror's Journal and say "here you go, good luck"? Of course not, So why should it be any different for code.
'm someone who's self taught (Javascript, not Java), and I've never asked for help with my code as I prefer to work it out myself. But I gotta tell you, if I wanted to know how something was done in a hurry I'd much rather someone just show me how to do what I need to do than piss about sending me on a wild goose chase.
Should Striff look there and do other research first before coming to ask on here next time? Yes, I think he should, but I also think we shouldn't deny him straightforward asistance when he asks for it.
"Fuck you" indeed. You can be such a twat sometimes Noel.
I definately agree with the paint over analogy.
IMO newbies should be at the REPL hacking up Python (or Scheme, if they are bright).
If you had posted just the answer, then that's a bit dodge (but in the end it would be up to Striff not to just cop out and just copy it straight). But you went step-by-step explaining how it all works and mad props to you for that!
I too know what its like having teachers that just hand you a tute straight out of the application help file and send you on your way.
Having said that, I'm on the same page as Harl, where I like to learn shit on my own. Though it is definitely handy having someone run you through it first. Saves you a whole lot of time trawling through pages and pages of useless crap.
-caseyjones
(No smiley for you!)