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:
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:
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!
Re: Using scanner to pad requested word
Perhaps look at String.split()
Re: Using scanner to pad requested word
Quote:
Originally Posted by
Reeling
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.
Re: Using scanner to pad requested word
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?
Re: Using scanner to pad requested word
Quote:
Originally Posted by
Reeling
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
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.
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.
Re: Using scanner to pad requested word
Quote:
Originally Posted by
bill1991
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.
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
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!
Re: Using scanner to pad requested word
Here's a big helping hand. Your original code (minus the unnecessary brackets):
Code:
public static void main(String args[]) {
String s = pad("Hello", 10);
printStringReverse(s);
}
How it needs to be modified:
Code:
public static void main(String args[]) {
// code to get user input
String s = pad(abcd, efgh);
printStringReverse(s);
}
But use better variable names.