Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: new to java, and need help with assigment

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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 , 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

    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"); 
                }
     
            } 
     
        } 
     
    }
    Last edited by knotski; March 27th, 2012 at 02:43 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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? :/

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new to java, and need help with assigment

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 :/

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: new to java, and need help with assigment

    i did put it here

    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

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: new to java, and need help with assigment

    well think im starting to get somewhere 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);
    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by knotski; March 30th, 2012 at 02:37 PM.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: new to java, and need help with assigment

    its at line 14 at "if(val.equals("1")) {"
    is it the space in split(" ") ?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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(" ");

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new to java, and need help with assigment

    Glad you figured it out.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with java assigment
    By Lutzor in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 3rd, 2011, 05:29 PM
  2. No Time To Deal With The Assigment
    By Vins25 in forum Java Theory & Questions
    Replies: 14
    Last Post: May 3rd, 2011, 07:39 AM