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 19 of 19

Thread: help fix this code

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default help fix this code

    how do i get the inputed strings to print out after i turn them into tokens ? I need it to ask for a number and then multiple lines of strings, turn them into tokens, then print them out.


     
    import java.util.*;
    public class TokenDriver{
      public static void main(String [] args){
     
       // TokenStore words = new TokenStore();
        String[] tokens = new String[300];
     
        Scanner scan = new Scanner(System.in);
        Scanner scan2 = new Scanner(System.in);
        System.out.println("enter number of words to display per line");
        int num = scan.nextInt();
        System.out.println(num);
        //scan.nextLine();
       System.out.println("Enter lines of text, type two returns in a row to end");
       String t = " ";
       int pos = 0;
        String[] store = new String[300];        
      while(t.length() > 0)
      {
        t = scan2.nextLine();
        store[pos] = t;
        pos++;
      }
     StringTokenizer str = new StringTokenizer(t);  
      while(str.hasMoreTokens())
      {
        String token = str.nextToken();
        System.out.println(token);
      }


    --- Update ---

    also as a follow up how would use an array in an other class (TokenStore) to save the typed line information
    Last edited by dmanmeds; June 23rd, 2014 at 07:34 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help fix this code

    how do i get the inputed strings to print out
    Use: System.out.println(THESTRINGHERE);

    What is wrong with what the code does now? It is using the above mentioned method to print tokens.

    Can you explain what the program is supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: help fix this code

    right now the code asks for a line then prints the tokens, but i need it to ask for multiple lines of strings, turn them into tokens then print the tokens

    the program wants me input a number (lets say k) and lines of text. then print the lines of text but with k tokens in each line. it says to turn the line of texts into tokens and store the tokens into an array to store the tokens


    this is what i have for the TokenStore class but I'm pretty sure i am doing it completely wrong so I might have to start over

     
    import java.util.*;
     
     
    public class TokenStore{
     
     
     
      final int lastWord = 300;
     private int lastEmpty = 0;
     private String[] lines = new String[lastWord];
     private String word;
     int targetLine;
     
    public TokenStore( String w){
      word = w;
      int count = 1;
     
     }
     
     
       public void addWord( String w) {
         if ((lastEmpty + 1) == lastWord)
           System.out.println( "word full" + w + "not add");
         else{
           lines[lastEmpty] = new String(w);
           lastEmpty++;
         }
       }
     
       public void updateWords( String w){
         if( lines.length>0)
         addWord(w);
       }
     
     
       public void wordReport(){
         for( int i = 0; i<lastEmpty; i++)
           System.out.println(lines[i]);
       }
     
       public void nextEmpty(){
     
     
       }
     
       public void breakToTokens(){
         StringTokenizer str = new StringTokenizer(lines[lastWord]);
         while(str.hasMoreTokens()){
           addWord(lines[lastWord]);
         }
       }
     
     
    }
    Last edited by dmanmeds; June 23rd, 2014 at 09:05 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help fix this code

    i need it to ask for multiple lines of strings, turn them into tokens then print the tokens
    Which part are you having problems with?
    ask for multiple lines of strings
    {I assume the code will read those strings}
    turn them into tokens
    {What happens to the tokens? Saved or printed immediately?}
    print the tokens

    Suggestion: Work on one step at a time. Design it, code it and test it before moving to the next step.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: help fix this code

    what i am really having trouble on is storing the tokens from TokenDriver in an array in the other class Token Store

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help fix this code

    storing the tokens from TokenDriver in an array in the other class Token Store
    Add a method to the TokenStore class that stores the tokens in the array. Call that method with the token to be stored from the TokenDriver class.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    dmanmeds (June 23rd, 2014)

  8. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: help fix this code

    would this be how a method like that looks like? I get an error with this but if I am on the right track then I could probably figure it out. Do you think I would be able to get an example of what a method like that would look. Also do you think all the other methods I have are necessary?

    public void storeToken(){
         lines[lastWord] =new String[word];
       }

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help fix this code

    The method is nearly of the right form, but Norm suggested passing a parameter. In order to pass a parameter, the method must accept one, as in:

    public void storeToken( String token )

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    dmanmeds (June 24th, 2014)

  11. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: help fix this code

    Sorry for asking so many questions but I think I might me getting somewhere now. my professor told me make a TokenStore object t inside TokenDriver.

    Read in a line of text s

    Then: t.process(s);

    where process is a method inside the TokenStore class that does the important stuff: it breaks the line s into tokens, and it puts each token in a separate cell in the array attribute of the TokenStore class. Of course you’ve got to write the process method..

    i made a method where the lines of text in tokenDriver are first made into tokens then stored into the array. The only problem is that when I run the program the console asks me to enter a text then it prints the text I just entered in tokens but then it tells me to enter another text then it prints new tokens I just entered.

    How would I make it ask for multiple lines of text then prints them in tokens?

    enter number of words to display per line
    [DrJava Input Box] 5
    Enter lines of text, type two returns in a row to end
    [DrJava Input Box] hello there
    hello
    there
    [DrJava Input Box] how is your day
    hello
    there
    how
    is
    your
    day

    import java.util.*;
     
    public class tokenStore2{
     
     
      final int lineLength = 300;
      private String[] lines = new String[lineLength];
     private int nextEmpty = 0;
     
      public void makeToken( String line){
          if(nextEmpty + 1 == lineLength)
            System.out.println("full");
            else{
        StringTokenizer str = new StringTokenizer(line);
          while(str.hasMoreTokens()){
            //str.nextToken();
            String s = str.nextToken();
           lines[nextEmpty] = new String(s);
           nextEmpty++;
           }
          for(int i =0; i<lines.length;i++)
             if  (lines[i] != null) System.out.println(lines[i]);
            }
      }



    import java.util.*;
     
    public class tokenDriver2{
      public static void main(String [] args){
     
        tokenStore2 t = new tokenStore2();
        Scanner scan1 = new Scanner(System.in);
        Scanner scan2 = new Scanner(System.in);
        System.out.println("enter number of words to display per line");
        int num = scan1.nextInt();
        System.out.println("Enter lines of text, type two returns in a row to end");
        int pos = 0;
        String s = " ";
        while(s.length() > 0){
          s = scan2.nextLine();
          t.makeToken(s);
        }
     
     
     
     
     
     
      }
    }

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help fix this code

    How would I make it ask for multiple lines of text
    There are several ways all involving loops:
    1) get the number of lines from the user and loop that number of times
    2) have a "sentinel" value that the user enters to tell the program that there is no more input

    prints them in tokens?
    Use a loop to print the saved tokens.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #11
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default help with this code

    I am trying to get my code to to print out a certain number of tokens in a line then make a new line. The number is determined by a number entered to the console. How do I get it to do this. I have most of it done and know i need to have if((i%num) == 0) System.out.println();
    this is how it should run.

    enter number of words to display per line
    > 3
    Enter lines of text, type two returns in a row to end
    >
    hello, how are you
    I am doing well
    what is the time today
    (empty line)

    (this is what it should print with 3 tokens in each line)
    hello, how are
    you I am
    doing well what
    is the time
    today

    but my code just prints this:
    hello, how are you I am doing well what is the time today





    import java.util.*;
     
     public class TokenStore2{
     
      final int lastEmpty = 300;
      private String[] lines = new String[lastEmpty];
     private int index = 0;
     
     
     String s;
     int count = 0;
     
      public void makeToken( String line,int num)
      {
          if(index + 1 == lastEmpty)
            System.out.println("full");
            else
            {
     
        StringTokenizer str = new StringTokenizer(line);
           while(str.hasMoreTokens())
           {
             s = str.nextToken();
           lines[index] = new String(s);
           index++;
          count++;
           }
          }
         }
     
      public void printTokens(int num)
      {
        for(int i =0; i<index;i++)
    {
          if (lines[i] != null)
         System.out.print(lines[i] + " ");
    }
        else if((i%num)==0)
         {
     System.out.println();
    }
      }
     }

    import java.util.*;
     
    public class TokenDriver2{
      public static void main(String [] args){
     
        tokenStore2 t = new tokenStore2();
        Scanner scan1 = new Scanner(System.in);
        Scanner scan2 = new Scanner(System.in);
        System.out.println("enter number of words to display per line");
        int num = scan1.nextInt();
        System.out.println("Enter lines of text, type two returns in a row to end");
        int pos = 0;
        String s = " ";
        while(s.length() > 0){
          s = scan2.nextLine();
          t.makeToken(s, num);
        }
           t.printTokens(num);
     }
    }
    Last edited by dmanmeds; June 25th, 2014 at 11:21 AM.

  14. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: help with this code

    Why would lines[i] be null when you're printing out the line?

    Please use curly braces around every for loop and if statement body, even if you think it's only a single line. It also would help if you used standard naming conventions: classes start with an upper-case letter, methods and variables with a lower-case letter.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with this code

    Is this the same problem: http://www.javaprogrammingforums.com...-fix-code.html

    Threads merged.

    NOTE: With an if statement, if the if condition is true, the else statement will NOT be executed.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #14
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: help with this code

    kevin-I tied to fix it up

    Norm- what kind of statement would i have to use to get it to print the token then print a line if it is divisible by a certain number? oh and yes that is the same problem I meant to put it in a different post but for sum reason they are in they same post.
    Last edited by dmanmeds; June 25th, 2014 at 11:32 AM.

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help fix this code

    Did you understand my NOTE? I made it because of a problem in the code you are asking about.

    The {}s in the TokenStore2 class need to be fixed. Many of them are not in the proper column to show the nesting of the logic.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #16
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: help fix this code

    from your note I think you were trying to tell me that since the if statement is true then it won't go on to execute anything after it

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help fix this code

    since the if statement is true then it won't go on to execute anything after it
    Sort of true. It will not execute the else. But it will execute what comes after the else.
    Look at your code. What is in the else statement? Is that related to your problem?

    --- Update ---

    How many blanks lines are printed by the program after the line it outputs?
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    dmanmeds (June 25th, 2014)

  21. #18
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: help fix this code

    I think I am getting really close I have this code. this method makes it so there is a line of the specified number of tokens then creates a new line. The only problem now is that it does not include the last token of the line.
    ex of how it should look:

    enter number of words to display per line
    3
    Enter lines of text, type two returns in a row to end
    the pond is very big
    but it is
    crowded


    this is what it should print:
    the pond is
    very big but
    it is crowded

    this is what mine prints:
    the pond
    very big
    it is

    it skips the token where (i%num ==0) I expected it to do this but is there a way in which it will skip the line but also print the word at (i%num ==0)? Or will coding it this way will not let me print that token?


     
    public void printTokens(int num)
      {
        for(int i =1; i<index;i++)
     
          if(i%num ==0)
        {
           System.out.println(lines[i]);
        }
     
        else 
        {
          System.out.print(lines[i] + " ");
        }
      }


    --- Update ---

    never mind I got it. Thank you so much!

  22. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help fix this code

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

Similar Threads

  1. help fix this code
    By dmanmeds in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 16th, 2014, 08:49 PM
  2. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  3. Please anyone can you fix my Code??
    By java_rookie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2012, 09:59 AM
  4. How do i fix my code?
    By beebee007 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2012, 10:49 AM
  5. How to fix this code?
    By ice in forum AWT / Java Swing
    Replies: 26
    Last Post: November 29th, 2010, 07:10 PM