473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

how to find a 'carriage return' and loop back to main until user enters a string

1
excuse me, simple question
I want a user to enter a string so that I may recursively call it to reverse it.
Just one thing, i wanna make sure the user did not enter a carriage return, if he/she did, it will loop and ask them to enter a string again. Basically error prevention. So I tried a while (x=="\n") or "" or "\r" where x is my string..it did not do anything. BTW i am using Eclipse.
So basically how can i do a loop in main that will keep asking user for a string and checking whether or not he enterd a "Enter"
Thanks ahead of time.

The prog:
**********************************

import java.io.*;
import java.util.Scanner;
public class stringreverse
{

public static void stringreversed(String s, int n)
{
if (n==0){
System.out.print("you entered nada");
return;}
else{

System.out.print(s.charAt(n-1));
stringreversed(s,n-1);
}
}

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("What do you want to reverse? ");
int t;
String x = scan.next();
while (x !="")
{
t = x.length();

stringreversed(x,t);
}

else
System.out.print("You entered an empty string,try again");
//want it to loop back to main here until a string is entered









}
}
Mar 29 '10 #1
1 3063
jkmyoung
2,057 Expert 2GB
use the nextLine() function if you're looking for line by line input as opposed to word by word input.

scan.next(); will never return "" unless you have changed the input delimiters.
Mar 29 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Madestro | last post by:
Hi guys, I am making a small program to retrieve e-mails from POP accounts. I got all the e-mail parsing stuff figured out, but I cannot seem to come up with a way to find out which e-mails are...
5
by: Ferdi | last post by:
I have a WebForm with one control: A HTML INPUT control with the property "runat=server" set. If this page is running and I set the cursor in this control and enter "Carriage return", the page...
1
by: Steve | last post by:
I have a simple form with a textbox and a command button, both web form controls (not raw HTML controls). I notice that when the user enters text in the textbox and hits carriage return, the form...
2
by: Matt Mercer | last post by:
Hi all, I am having a frustration problem, and I have read about 25 newsgroup postings that do not have a satisfying answer :) The problem appears to be common where carriage returns are lost...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
1
by: Arpan | last post by:
A class file named "Users.vb" has the following code: Namespace Users Public Class UserDetails Public FirstName As String Public LastName As String Public UserName As String Public Password...
8
by: mharry | last post by:
Hi guys! I have a VB6 program that uses FileSystemObject to read a csv file,Basically I want to add commas into a specified locations of the csv. The program works fine on a generic csv file...
4
by: saytri | last post by:
I have a problem with do while loop. I am displaying a dialogue box where a person should enter the type of quiz he had performed. The problem is that the user should enter only the following 3...
11
by: evenlater | last post by:
My db allows the user to send email via CDO. The body of the email is determined in code. I have built an email form with To, CC and Subject lines and a large text box for the body of the message...
1
by: robotlizz | last post by:
Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.