-
new to java, and need help with assigment
Hi all, i need to make a translater program that translate words. example if u type in hello all it should return ellohsese llasese. it moves the first letter in the words to last and puts sese at the end of the words, then i need to have a way to reverse the method, so if i type in ellohsese llasese it should return hello all.
i have got the program to almost work as i like :rolleyes: , the thing is it just works if i type in one word ie hello and returns ellohsese. and i dont really know how to fix it, so if someone could give me some advise to how i can get it to work i would be happy :o
Code Java:
import javax.swing.*;
public class TransLater {
public static void main(String[] arg) {
String val;
JOptionPane.showMessageDialog(null, "Welcome to the Translater");
while(true) {
val = JOptionPane.showInputDialog("1) Translate to \n2) Translate from");
if(val.equals("1")) {
StringBuffer str = new StringBuffer("");
String indata;
char first;
indata = JOptionPane.showInputDialog("Type ur words here:");
str.insert(0,indata);
first = str.charAt(0);
str.deleteCharAt(0);
str.append(first);
str.append("sese");
JOptionPane.showMessageDialog(null, str);
}
else if(val.equals("2")) {
String indata2;
indata2 = JOptionPane.showInputDialog("Type ur words here:");
while (indata2.endsWith("sese")) {
indata2 = indata2.substring(0, indata2.length() - 4);
String last = indata2.substring(indata2.length() -1);
indata2 = indata2.substring(0, indata2.length() -1);
JOptionPane.showMessageDialog(null, last + indata2);
}
}
else {
JOptionPane.showMessageDialog(null, "Wrong selection, try again");
}
}
}
}
-
Re: new to java, and need help with assigment
Can you post the output from when the code does not work that shows the problem.
Add comments saying what is wrong with the output and show what the output should be.
-
Re: new to java, and need help with assigment
well there are no errors, its just that the program dont do as i want it to do. i want it to print all words in a phrase. Example: hello world. should be ellohsese orldwsese, but it prints ello orldwhsese. it doesnt give all world sese at the end just the last word. :( any suggestions on how i could make it work?
-
Re: new to java, and need help with assigment
You need to get the individual words from the String so you can work on them one by one.
Then you can change them and build a new String with the changed words.
The String class has methods to help you separate the String into separate words. See the split() method.
-
Re: new to java, and need help with assigment
Okey i understand what to do so ill give it a try tomorrow its night time here in sweden atm :/ thanks for ur input
-
Re: new to java, and need help with assigment
Hi again ive been trying to get the split method to work but, i cant figure it out :( can anyone help me? where in the code should i set it up for it to work? :/
-
Re: new to java, and need help with assigment
Quote:
where in the code should i set it up for it to work?
After you have read in the String that you want to split.
-
Re: new to java, and need help with assigment
i tried that but it didnt change the outcome at all :( i have to do something wrong :/
-
Re: new to java, and need help with assigment
How did you use the split method? It returns an array that you must then go into to get the strings that were split from the original string.
-
Re: new to java, and need help with assigment
i did put it here
Code :
if(val.equals("1")) {
StringBuffer str = new StringBuffer("");
String indata;
char first;
indata = JOptionPane.showInputDialog("Type ur words here:");
[B][/B] String[] words = indata.split(" ");[B][/B]
str.insert(0,indata);
first = str.charAt(0);
str.deleteCharAt(0);
str.append(first);
str.append("sese");
JOptionPane.showMessageDialog(null, str);
}
im really new at java this is only my 3 week and get this hard assignments :(
-
Re: new to java, and need help with assigment
The words array now has the split strings in it. You will need a loop to get them out one by one and do the conversion on each of them.
-
Re: new to java, and need help with assigment
well think im starting to get somewhere:P when i type in test abc it should print esttojoj bcaojoj, but for me it return test abcest abctojoj.
sorry for the trubble:/ im not the best at english so its abit hard to understand sometimes :/
indata = JOptionPane.showInputDialog("type ur words here:"); indata
String[] words = indata.split("\\s ");
str.insert(0,indata);
first = str.charAt(0);
str.deleteCharAt(0);
str.append(first);
str.append("ojoj");
for (String word : words)
{
System.out.print(word+str);
// JOptionPane.showMessageDialog(null, str);
}
-
Re: new to java, and need help with assigment
Add some printlns to the code to show the value of variables as they are set and changed.
-
Re: new to java, and need help with assigment
i dont get it at all :/ can you maybe show men an exemple? ive tryed to change in the code but without luck :(
-
Re: new to java, and need help with assigment
You get to the elements of an array by using an index value inside of []s
words[0] is a reference to the first element in the words array.
The elements of an array are items of the same class as the array type:
words[0] is a String.
-
Re: new to java, and need help with assigment
i got the code working :) did do the split method wrong :/ the program now works, but i get a Exception in thread "main" java.lang.NullPointerException
at OverSattaren.main(OverSattaren.java:23)
when i close the program and i cant figure out why?
-
Re: new to java, and need help with assigment
What is the code at the line where the exception occurs?
Can you post the full text of the error message.
Edit your post and change the tags to[code=java]<YOUR CODE HERE>[/code] to get highlighting
-
Re: new to java, and need help with assigment
its at line 14 at "if(val.equals("1")) {"
is it the space in split(" ") ?
-
Re: new to java, and need help with assigment
What is the value of the val variable? That is the only thing that would cause a NPE with statement you posted.
-
Re: new to java, and need help with assigment
the NPE comes when i close the program, when i run now its on this line String[] words = indata.split(" ");
-
Re: new to java, and need help with assigment
What is the statement that throws the NPE? You need to find the variable with null value!!!
What is the value of indata when the code in post#20 is executed?
-
Re: new to java, and need help with assigment
well i found it :) thank you so much for all your help Norm :) did put null in the intext fields and it solved it :)
-
Re: new to java, and need help with assigment