Word advancing in letters each new line
- I want the user to type in a string.
- The console output should have string.length lines (plus the line where the user inputs the string).
- The first line should output the first symbol of the string (string.length) times.
- The second line should output the first symbol of the string and then repeat the second symbol (string.length - 1) times, and so on.
Here is an example of what I want the console output the be with the word "example".
What will your word be?: example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
I have no idea where to start with this one. I'd appreciate any help.
Edit
Sorry for being so unclear and not providing any code. This is what I have so far.
import java.util.Scanner;
public class muster{
public static void main(String s){
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++)
System.out.println(word.substring(0,i+1));
}
}
This will read the users input and print the word starting from the first letter with a new letter in each new line. What I still need is that the code repeats the letters for as long as the rest of word.length is.
java
|
show 1 more comment
- I want the user to type in a string.
- The console output should have string.length lines (plus the line where the user inputs the string).
- The first line should output the first symbol of the string (string.length) times.
- The second line should output the first symbol of the string and then repeat the second symbol (string.length - 1) times, and so on.
Here is an example of what I want the console output the be with the word "example".
What will your word be?: example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
I have no idea where to start with this one. I'd appreciate any help.
Edit
Sorry for being so unclear and not providing any code. This is what I have so far.
import java.util.Scanner;
public class muster{
public static void main(String s){
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++)
System.out.println(word.substring(0,i+1));
}
}
This will read the users input and print the word starting from the first letter with a new letter in each new line. What I still need is that the code repeats the letters for as long as the rest of word.length is.
java
2
Did you already try anything yourself?
– MWB
Oct 28 '18 at 14:49
Start by making some code attempt, then narrow down the question to where things are still unclear. At SO, you cannot expect to simply be given a solution. We're happy to answer questions but need to see some research effort/attempt.
– paisanco
Oct 28 '18 at 14:50
Please try it out yourself and ask here if you face any issue in your solution.
– Jignesh M. Khatri
Oct 28 '18 at 14:50
Break the problem down into small steps -- get user input, get length of input, create loop to print "x" lines, consider using an inner loop to help figure out what letters to print on each line.... and then try to solve each small step, one at a time.
– Hovercraft Full Of Eels
Oct 28 '18 at 15:03
No idea where to start? How about writing a class with amain
method? If you’ve been given this assignment, surely you know how to write the code for the first bullet point, at least. Are you familiar with loops? Are you familiar with thesubstring
method of String? Unless you provide your code attempt, we can’t write an answer that addresses your knowledge level.
– VGR
Oct 28 '18 at 15:06
|
show 1 more comment
- I want the user to type in a string.
- The console output should have string.length lines (plus the line where the user inputs the string).
- The first line should output the first symbol of the string (string.length) times.
- The second line should output the first symbol of the string and then repeat the second symbol (string.length - 1) times, and so on.
Here is an example of what I want the console output the be with the word "example".
What will your word be?: example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
I have no idea where to start with this one. I'd appreciate any help.
Edit
Sorry for being so unclear and not providing any code. This is what I have so far.
import java.util.Scanner;
public class muster{
public static void main(String s){
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++)
System.out.println(word.substring(0,i+1));
}
}
This will read the users input and print the word starting from the first letter with a new letter in each new line. What I still need is that the code repeats the letters for as long as the rest of word.length is.
java
- I want the user to type in a string.
- The console output should have string.length lines (plus the line where the user inputs the string).
- The first line should output the first symbol of the string (string.length) times.
- The second line should output the first symbol of the string and then repeat the second symbol (string.length - 1) times, and so on.
Here is an example of what I want the console output the be with the word "example".
What will your word be?: example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
I have no idea where to start with this one. I'd appreciate any help.
Edit
Sorry for being so unclear and not providing any code. This is what I have so far.
import java.util.Scanner;
public class muster{
public static void main(String s){
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++)
System.out.println(word.substring(0,i+1));
}
}
This will read the users input and print the word starting from the first letter with a new letter in each new line. What I still need is that the code repeats the letters for as long as the rest of word.length is.
java
java
edited Oct 28 '18 at 15:37
Dio
asked Oct 28 '18 at 14:46
DioDio
112
112
2
Did you already try anything yourself?
– MWB
Oct 28 '18 at 14:49
Start by making some code attempt, then narrow down the question to where things are still unclear. At SO, you cannot expect to simply be given a solution. We're happy to answer questions but need to see some research effort/attempt.
– paisanco
Oct 28 '18 at 14:50
Please try it out yourself and ask here if you face any issue in your solution.
– Jignesh M. Khatri
Oct 28 '18 at 14:50
Break the problem down into small steps -- get user input, get length of input, create loop to print "x" lines, consider using an inner loop to help figure out what letters to print on each line.... and then try to solve each small step, one at a time.
– Hovercraft Full Of Eels
Oct 28 '18 at 15:03
No idea where to start? How about writing a class with amain
method? If you’ve been given this assignment, surely you know how to write the code for the first bullet point, at least. Are you familiar with loops? Are you familiar with thesubstring
method of String? Unless you provide your code attempt, we can’t write an answer that addresses your knowledge level.
– VGR
Oct 28 '18 at 15:06
|
show 1 more comment
2
Did you already try anything yourself?
– MWB
Oct 28 '18 at 14:49
Start by making some code attempt, then narrow down the question to where things are still unclear. At SO, you cannot expect to simply be given a solution. We're happy to answer questions but need to see some research effort/attempt.
– paisanco
Oct 28 '18 at 14:50
Please try it out yourself and ask here if you face any issue in your solution.
– Jignesh M. Khatri
Oct 28 '18 at 14:50
Break the problem down into small steps -- get user input, get length of input, create loop to print "x" lines, consider using an inner loop to help figure out what letters to print on each line.... and then try to solve each small step, one at a time.
– Hovercraft Full Of Eels
Oct 28 '18 at 15:03
No idea where to start? How about writing a class with amain
method? If you’ve been given this assignment, surely you know how to write the code for the first bullet point, at least. Are you familiar with loops? Are you familiar with thesubstring
method of String? Unless you provide your code attempt, we can’t write an answer that addresses your knowledge level.
– VGR
Oct 28 '18 at 15:06
2
2
Did you already try anything yourself?
– MWB
Oct 28 '18 at 14:49
Did you already try anything yourself?
– MWB
Oct 28 '18 at 14:49
Start by making some code attempt, then narrow down the question to where things are still unclear. At SO, you cannot expect to simply be given a solution. We're happy to answer questions but need to see some research effort/attempt.
– paisanco
Oct 28 '18 at 14:50
Start by making some code attempt, then narrow down the question to where things are still unclear. At SO, you cannot expect to simply be given a solution. We're happy to answer questions but need to see some research effort/attempt.
– paisanco
Oct 28 '18 at 14:50
Please try it out yourself and ask here if you face any issue in your solution.
– Jignesh M. Khatri
Oct 28 '18 at 14:50
Please try it out yourself and ask here if you face any issue in your solution.
– Jignesh M. Khatri
Oct 28 '18 at 14:50
Break the problem down into small steps -- get user input, get length of input, create loop to print "x" lines, consider using an inner loop to help figure out what letters to print on each line.... and then try to solve each small step, one at a time.
– Hovercraft Full Of Eels
Oct 28 '18 at 15:03
Break the problem down into small steps -- get user input, get length of input, create loop to print "x" lines, consider using an inner loop to help figure out what letters to print on each line.... and then try to solve each small step, one at a time.
– Hovercraft Full Of Eels
Oct 28 '18 at 15:03
No idea where to start? How about writing a class with a
main
method? If you’ve been given this assignment, surely you know how to write the code for the first bullet point, at least. Are you familiar with loops? Are you familiar with the substring
method of String? Unless you provide your code attempt, we can’t write an answer that addresses your knowledge level.– VGR
Oct 28 '18 at 15:06
No idea where to start? How about writing a class with a
main
method? If you’ve been given this assignment, surely you know how to write the code for the first bullet point, at least. Are you familiar with loops? Are you familiar with the substring
method of String? Unless you provide your code attempt, we can’t write an answer that addresses your knowledge level.– VGR
Oct 28 '18 at 15:06
|
show 1 more comment
1 Answer
1
active
oldest
votes
You got very close. You already had the idea to print the substring from 0 to i
. Then you just need an inner loop that starts at i+1
and loops until word.length
and print out the char at i
. Also you need to use System.out.print()
so that they will be on the same line:
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++) {
System.out.print(word.substring(0,i+1));
for(int j = i+1; j < word.length(); j++) {
System.out.print(word.charAt(i));
}
System.out.println();
}
Output:
What will your string be?
example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53032727%2fword-advancing-in-letters-each-new-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You got very close. You already had the idea to print the substring from 0 to i
. Then you just need an inner loop that starts at i+1
and loops until word.length
and print out the char at i
. Also you need to use System.out.print()
so that they will be on the same line:
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++) {
System.out.print(word.substring(0,i+1));
for(int j = i+1; j < word.length(); j++) {
System.out.print(word.charAt(i));
}
System.out.println();
}
Output:
What will your string be?
example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
add a comment |
You got very close. You already had the idea to print the substring from 0 to i
. Then you just need an inner loop that starts at i+1
and loops until word.length
and print out the char at i
. Also you need to use System.out.print()
so that they will be on the same line:
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++) {
System.out.print(word.substring(0,i+1));
for(int j = i+1; j < word.length(); j++) {
System.out.print(word.charAt(i));
}
System.out.println();
}
Output:
What will your string be?
example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
add a comment |
You got very close. You already had the idea to print the substring from 0 to i
. Then you just need an inner loop that starts at i+1
and loops until word.length
and print out the char at i
. Also you need to use System.out.print()
so that they will be on the same line:
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++) {
System.out.print(word.substring(0,i+1));
for(int j = i+1; j < word.length(); j++) {
System.out.print(word.charAt(i));
}
System.out.println();
}
Output:
What will your string be?
example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
You got very close. You already had the idea to print the substring from 0 to i
. Then you just need an inner loop that starts at i+1
and loops until word.length
and print out the char at i
. Also you need to use System.out.print()
so that they will be on the same line:
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++) {
System.out.print(word.substring(0,i+1));
for(int j = i+1; j < word.length(); j++) {
System.out.print(word.charAt(i));
}
System.out.println();
}
Output:
What will your string be?
example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
answered Oct 28 '18 at 16:03
GBlodgettGBlodgett
10.3k42035
10.3k42035
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53032727%2fword-advancing-in-letters-each-new-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Did you already try anything yourself?
– MWB
Oct 28 '18 at 14:49
Start by making some code attempt, then narrow down the question to where things are still unclear. At SO, you cannot expect to simply be given a solution. We're happy to answer questions but need to see some research effort/attempt.
– paisanco
Oct 28 '18 at 14:50
Please try it out yourself and ask here if you face any issue in your solution.
– Jignesh M. Khatri
Oct 28 '18 at 14:50
Break the problem down into small steps -- get user input, get length of input, create loop to print "x" lines, consider using an inner loop to help figure out what letters to print on each line.... and then try to solve each small step, one at a time.
– Hovercraft Full Of Eels
Oct 28 '18 at 15:03
No idea where to start? How about writing a class with a
main
method? If you’ve been given this assignment, surely you know how to write the code for the first bullet point, at least. Are you familiar with loops? Are you familiar with thesubstring
method of String? Unless you provide your code attempt, we can’t write an answer that addresses your knowledge level.– VGR
Oct 28 '18 at 15:06