Results 1 to 11 of 11
  1. #1
    bill1991 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Using scanner to pad requested word

    Hi all,

    So I was given a practice problem for my programming course based on an assignment we've had to do in class. We're learning about manipulating strings, so the assignment itself was to write a program that will take a string and pad it with periods to a certain length, then print the string backwards.

    My code to do that was:

    Java Code:
    public class Homework1 {
    
    	public static void main(String args[]) {
    		
    		String s = (pad("Hello", 10));
    		printStringReverse(s);
    		
    	}
    
    
    	public static String pad(String s, int desiredLength) {
    			for(int i = s.length(); i<desiredLength; i++) {
    				s+=".";
    			}
    				return s;
    	}
    	
    	public static void printStringReverse (String s) {
    			for(int i = s.length()-1; i>=0; i--) {
    				System.out.print(s.charAt(i));
    			}
    	}
    
    			
    }

    This program worked fine; however the second part of the assignment my teacher assigned for additional practice with strings was to have the program request a single word to use and the length to which I want it padded. I'm not quite sure how I would want to change this around to fit those requirements. If anybody could help adjust what I've already tried, or explain a way that I could do this, it'd really be appreciated. Here's that code:

    Java Code:
    import java.util.*;
    
    public class ScannerPractice {
    	public static void main(String[] args) {
    		
    
    		Scanner console = new Scanner(System.in);
    		System.out.print("Enter a word: ");
    		String s = console.next();
    		System.out.print("How long would you like this word padded: ");
    		int n = console.nextInt();
    		
    		
    		System.out.print(n);
    		
    
    	}
    	
    	public static String pad(String s, int desiredLength) {
    		for(int i = s.length(); i<desiredLength; i++) {
    			s+=".";
    		}
    			return s;
    	}
    
    }
    I can get the program to ask to enter a word then ask to enter a number, but I can't get the word printed and padded. Also, I'm well aware I didn't call anything to do the padding; I just had no luck with calling it, so I removed the errors in the program to give a cleaner slate for the reader.

    Again, any help is very much appreciated!
    Last edited by bill1991; 10-21-2012 at 08:57 PM.

  2. #2
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Using scanner to pad requested word

    Perhaps look at String.split()

  3. #3
    bill1991 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Using scanner to pad requested word

    Quote Originally Posted by Reeling View Post
    Perhaps look at String.split()
    That's not something we covered, and the idea behind the problem is that he wants us to try to do it based on what we've learned. Is there a way for me to call the pad method without an error? Every method I've tried seems to fail in Eclipse.

  4. #4
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Using scanner to pad requested word

    What error do you get?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Using scanner to pad requested word

    :confused:

    You store the word in the variable s and the length in the variable n. So how come you cannot pass those variables to the pad method instead of the hard coded values you have in the original code?

  6. #6
    bill1991 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Using scanner to pad requested word

    Quote Originally Posted by Reeling View Post
    What error do you get?
    I fooled around with different calls, and either they worked but didn't accomplish what I wanted, or they did give actual compiler errors.

    Essentially what I am trying to work out is how to make the program ask for a word, then ask for an integer that describes how many characters the user wants the word padded to with periods.

    For example:

    Word = Java
    Padded to 15th character
    Output: Java...........



    My problem with the current code (second one posted) is simple; I don't know how to call this so it will work as I desire. I'm hoping to figure this out because it would certainly open up plenty of opportunity to practice with the Scanner, which clearly I need to. :P

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Using scanner to pad requested word

    Still confused.

    So now you have managed to pass user input to the pad method but now you cannot manage to pass the returned value from the pad method to the printStringReverse method? Making method calls are exactly the same regardless of where the values come from or are stored.

  8. #8
    bill1991 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Using scanner to pad requested word

    Not exactly. What I did was used main to add a scanner, and the scanner will accept obviously the two println's in main.

    Forget the first code entirely; that was simply showing how I padded (then reversed, which is irrelevant in this question), and showing my basis for using that as my pad method in the second code.

    Now, what I am trying to do is pad ANY word that the user inputs to ANY amount of characters the user wishes with periods. I feel like I may be onto something since I had the pad method, but what I am confused on is how I should call it and make sure it will take any word that is inputted and pad it appropriately.

    Sorry for the confusion, is that more logical? Basically ignore the first code entirely, that isn't where my problem lies.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Using scanner to pad requested word

    Quote Originally Posted by bill1991 View Post
    Forget the first code entirely; that was simply showing how I padded (then reversed, which is irrelevant in this question)
    No it is not irrelevant. You code is doing exactly the same thing except that the String and int are now coming from the user instead of being hard coded.
    Java Code:
    class Foo {
        public static void main(String[] args) {
            bar(5);
            int num = 5;
            bar(num);
            bar(getUserInput());
        }
    
        public static void bar(int val) {
    
        }
    
        public static int getUserInput() {
            // code to get and return user input.
        }
    }
    In the main method the bar method is called 3 times and each time the value passed as a parameter is obtained a different way but calling the method is exactly the same. You seem to thing that because you need to get user input your entire code needs to completely change. It doesn't.

    Get user input for word and store in variable
    Get user input for length and store in variable
    Call pad method, pass user input as parameters, store returned value in variable
    Call printStringReverse method passing above returned value as parameter

  10. #10
    bill1991 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Using scanner to pad requested word

    Thanks for the help; it's slowly starting to come together for me. If I have any other questions about the syntax of this code I'll probably post something about it, but again, thank you very much!

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Using scanner to pad requested word

    Here's a big helping hand. Your original code (minus the unnecessary brackets):
    Java Code:
    public static void main(String args[]) {
        String s = pad("Hello", 10);
        printStringReverse(s);
    }
    How it needs to be modified:
    Java Code:
    public static void main(String args[]) {
        // code to get user input
        String s = pad(abcd, efgh);
        printStringReverse(s);
    }
    But use better variable names.

Similar Threads

  1. The requested resource () is not available
    By hartmannr76 in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-31-2012, 07:39 AM
  2. Replies: 3
    Last Post: 08-30-2011, 04:44 PM
  3. Get requested URL from Browser
    By strongarm in forum New To Java
    Replies: 3
    Last Post: 07-18-2010, 01:03 AM
  4. The requested resource () is not available. Error
    By shyameni in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-17-2010, 09:22 AM
  5. The requested resource is not available.
    By ravichandra.t in forum Java Servlet
    Replies: 0
    Last Post: 07-29-2009, 02:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •