Program not waiting for input?
When I use keyboard.next() the program wiats for input, but when I use keyboard.nextLine() it doesn't wait for input. Can anyone tell me why? I am new to Java and this is the first programming language I have ever learnt. The problem is when option b is selected and the program returns no errors when compiled.
//Written SH 6/7/07
//This programme allows the user 3 options to either,
//print a number pattern, reverse a string or exit the programme.
import java.util.Scanner;
public class Assignment1 {
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println();
System.out.println("Print Pattern and Reverse String Menu");
System.out.println();
System.out.println("A. Print a pattern");
System.out.println("B. Reverse a string of text");
System.out.println("X. Exit the programme");
System.out.println();
System.out.print("Select an option from the menu (A, B or X): ");
char option = keyboard.next().charAt(0);
if (option == 'A' || option == 'a') {
System.out.println();
System.out.print("Enter the number of rows in the pattern to be printed: ");
int rows = keyboard.nextInt();
System.out.print("Enter character to be printed: ");
char character = keyboard.next().charAt(0);
for (int i = 1; i<=rows; i++) {
for (char j=1; j<=i; j++)
System.out.print(character + " ");
System.out.println();
}
}
else if (option == 'B' || option == 'b') {
System.out.println();
System.out.println("Enter a string of text to be reversed: ");
String text = keyboard.nextLine();
int last = text.length();
String reverse = text.substring(last,0);
System.out.println("The reverse of the string of text you entered is: " + reverse);
System.out.println();
}
else if (option == 'X' || option == 'x') {
System.out.println("Exiting the programme...");
System.out.println();
System.exit(0);
}
else {
System.out.println("Error! You did not select a valid option from the menu");
}
}
}
[2246 byte] By [
missy_suea] at [2007-11-27 9:37:16]

please enclose your code with the ['code'] tags to make it more readable, andobserve proper indentation. :)
sorry when I copied and pasted it came out like this, my acutal program has proper indentation.The program is probably so basic it would be easy to read as is. This is for an assignment and I am fustrated and in tears over it as it is. Could someone please just give me some guidance.
First, let's format this properly :)
/Written SH 6/7/07
//This programme allows the user 3 options to either,
//print a number pattern, reverse a string or exit the programme.
import java.util.Scanner;
public class Assignment1 {
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println();
System.out.println("Print Pattern and Reverse String Menu");
System.out.println();
System.out.println("A. Print a pattern");
System.out.println("B. Reverse a string of text");
System.out.println("X. Exit the programme");
System.out.println();
System.out.print("Select an option from the menu (A, B or X): ");
char option = keyboard.next().charAt(0);
if (option == 'A' || option == 'a') {
System.out.println();
System.out.print("Enter the number of rows in the pattern to be printed: ");
int rows = keyboard.nextInt();
System.out.print("Enter character to be printed: ");
char character = keyboard.next().charAt(0);
for (int i = 1; i<=rows; i++) {
for (char j=1; j<=i; j++)
System.out.print(character + " ");
System.out.println();
}
}
else if (option == 'B' || option == 'b') {
System.out.println();
System.out.println("Enter a string of text to be reversed: ");
String text = keyboard.nextLine();
int last = text.length();
String reverse = text.substring(last,0);
System.out.println("The reverse of the string of text you entered is: " + reverse);
System.out.println();
}
else if (option == 'X' || option == 'x') {
System.out.println("Exiting the programme...");
System.out.println();
System.exit(0);
}
else {
System.out.println("Error! You did not select a valid option from the menu");
}
}
}
> sorry when I copied and pasted it came out like this,
> my acutal program has proper indentation.The
> program is probably so basic it would be easy to read
> as is. This is for an assignment and I am fustrated
> and in tears over it as it is. Could someone please
> just give me some guidance.
Please first repost it using code tags which is explained here:
http://forum.java.sun.com/help.jspa?sec=formatting
> The problem is when option b is selected and the program
> returns no errors when compiled.
Ok, what is you applcation supposed to do then?
From a menu the user can select to either print a pattern of characters based on number of rows (I have this working), or enter a string of text which will then be displayed in reverse, or to exit the program. Also, another thing which I haven't worked out how to do yet) the program should automatically reutrn to the menu and only exit when the user selects that option.
> From a menu the user can select to either print a
> pattern of characters based on number of rows (I have
> this working), or enter a string of text which will
> then be displayed in reverse, or to exit the program.
In order to reverse a String, have a look at the StringBuilder class:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html
> Also, another thing which I haven't worked out how
> to do yet) the program should automatically reutrn
> to the menu and only exit when the user selects that
> option.
You can use a while-statement to keep looping until the user enters 'X', for example:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html
with the string reversal we are not allowed to use stringBuffer. And my original question was why won't the program pause to allow the user to enter a string of text when option 'b' is selected?
> with the string reversal we are not allowed to use
> stringBuffer.
but your solution won't work. prometheuzz is right - you need
to use StringBuilder, which is different from StringBuffer
>And my original question was why won't
> the program pause to allow the user to enter a string
> of text when option 'b' is selected?
Instead of keyboard.nextLine( ), try using just .next( ). :)
> with the string reversal we are not allowed to use
> stringBuffer.
Ok.
> And my original question was why won't
> the program pause to allow the user to enter a string
> of text when option 'b' is selected?
Ah, I see. That's because by using keyboard.next()... the first time you use your Scanner object, you leave the line-break in your Scanner object. So when you call keyboard.nextLine() it will immediatelly return the an empty line.
I suggest always reading entire lines when dealing with command line input. After reading a line, convert it to whatever you want. Something like this, for example:
char option = keyboard.nextLine().charAt(0);
> ...> Instead of keyboard.nextLine( ), try using> just .next( ). :)Err, you mean the other way around, I guess?
use an infinite loop before the first sysout.... eg.while(true){................}
> use an infinite loop before the first sysout.... eg.>> while(1){> ................> }If only we were talking about C...Message was edited by: prometheuzzAh, I see you edited it!; )
> see you edited it!> ; )pretty quick... wasn't I? ;)
Thankyou all for your help so far. I have worked out my original problem and successfully looped the menu. I am still having problems with the string reversal. To solve this we are expected to use loops.
> Thankyou all for your help so far. I have worked out
> my original problem and successfully looped the menu.
Good to hear that.
> I am still having problems with the string reversal.
>To solve this we are expected to use loops.
You'll need to be a bit more specific than that.
You can use a for-statement for this:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
On the specifications it says...when constructing the reversed string it may be helpgul to use the concatenation operator ("+") and the charAt() method (from the String class).
> On the specifications it says...when constructing the> reversed string it may be helpgul to use the> concatenation operator ("+") and the charAt() method> (from the String class).Ok, use that in combination with the for-statement I posted a link
I feel so stupid I still can't get it to work. Short of actually telling me what to put, I just can't work it out. I'll just have to hand in the assignment without that part done thanks anyway for all your help.
Probably too late for ya but here you go:
public void reverseString(String input){
String reverse = "";
for(int i =input.length()-1; i >= 0; i--){
reverse = reverse + input.charAt(i);
}
System.out.println(reverse);
}
Go through this step by step and see if you understand. If not ask :-)
I have followed and studied what u said step by step, applied it to my code and it has worked. Thank you so much for your help, it has clarified everything in my mind. Thank you also to all who helped. I'm glad there are forums such as this which can help newbies like me to understand.
> Probably too late for ya but here you go:> .....................> Go through this step by step and see if you> understand. If not ask :-)Or just copy and claim as your own the program idiotically give to you by ita6cgr.spoonfeeding !=
> > Probably too late for ya but here you go:
> > .....................
> > Go through this step by step and see if you
> > understand. If not ask :-)
>
> Or just copy and claim as your own the program
> idiotically give to you by ita6cgr.
>
> spoonfeeding != teaching
I was feeling nice leave me alone ;-)
> ...
> I was feeling nice leave me alone ;-)
Petes has the right to not leave you alone (on this forum). I agree with him. It would be even nicer of you to guide the OP towards a solution: that way s/he would've learned a lot more.
You of course have the right to ignore our posts.
Indeed apologies for my momentary lapse I will be back to normal as of now :-)p.s. What kind of person ignores posts!!! lol
> Indeed apologies for my momentary lapse I will be> back to normal as of now :-)Thanks, your consideration is much appreciated.
> Indeed apologies for my momentary lapse I will be> back to normal as of now :-)> > p.s. What kind of person ignores posts!!! lollol, no prob. and my apologies for using the word idiotic.
Ah no problem I been called worse lol
Oh, guys, stop being so friendly! We don't want this place to become a second JavaRanch, now do we?; )
I think I'm a member yet have have never looked properly at the ranch..... hmm next time i'm bored lol