Hello I'm having problems with a program that's supposed to take in a string and then capitalize the first letters of each word using the Character Wrapper class.
import java.util.*;
public class wrapper
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
String s1;
s1=input.nextLine();
s1= s1.trim();
int howLong= s1.length();
int i;
int counter=0;
char cho;
for(counter=1; counter<= howLong+1; counter++)
{
cho=s1.charAt(counter);
if(Character.isLetter (cho) && ! Character.isLetter(s1.charAt(counter-1)))
{
System.out.print( Character.toUpperCase(cho) );
}
else
{
System.out.print(cho);
}
System.out.println();
}
}
}
That's the program so far, but while it compiles with no errors according to BlueJ, it doesn't run. Any help as to why this is happening would be great.
Edit: Changed the program to what I believe would make it not just print out the spaces that the char variable was initialized to, but it still does not run. Maybe there's something wrong with the loop?
The reason your program compiles but doesn't run is because of the line s1=input.nextLine();. At this line, the program is waiting for input from the user to use as the string s1, but does not show the terminal in order for the user to give such input. A way you can get around this is to force the terminal to show itself before that line. I would recommend putting something like
System.out.println("Enter input:");
before the line, so that the terminal will show itself & the user can enter input into it. From there, you can work on the program like you would normally.
Related
public static void main(String[] args) {
//For String
Scanner input = new Scanner(System.in);
//For Letter
Scanner word = new Scanner(System.in);
String name;
//Input For a String
System.out.println("Enter A String");
name = input.nextLine();
//Input For a Char
char letter[] = new char[3];
System.out.println("Enter a Letter You Want to Search ");
for(char c : letter)
letter = word.nextLine().toCharArray();
elfish(letter, name);
}
public static void elfish(char letter[], String name) {
if(name.toLowerCase().contains(name.valueOf(letter).toLowerCase())) {
System.out.println("Yes The Letter Contains Elfish");
} else {
System.out.println("You're Word is not in this String");
}
}
i m making the program which is near to completion. It actually searches the desired letter in the string and if condition is true is prints the "if block" and if condition is false "else block" should run but actually its not working now. only "if block" is working. "else block" was also working fine until it was directly placed in main function. please tell me where what is wrong.
You're trying to iterate through letter before putting anything in the array. If you take out the for loop, (leaving the assignment of letter) the code should run properly in many cases. I do not know how you want to validate input.
I'm guessing you are new. Learning to use a debugger can be overwhelming, but it is a powerful tool. It'll be slightly different for every debugger and IDE but the process is the same.
A little more info here: Breakpoints and step by step debug in Java?
However, in this instance finding a guide for your IDE on Google might be the best option.
I'm using the Scanner class as stdin for read input from keybord.
I would read token by token the string insert until the end of the line.
token are separated by white spaces.
this is my code:
Scanner in = new Scanner(System.in);
int count=0;
while(in.hasNextLine()){
while(in.hasNext()){
String s=in.next();
System.out.println(s);
if(s.equals("\n"))
break;
count++;
}
break;
}
System.out.println(count);
The problem is that when i press enter, it seems considering it like a white space and it never exit from while block, so it continues to expect another input.
the only way to exit from the while and end the program in Linux is to press ctrl+D (=enter) but i'm searching another way...
I tried to insert an if statement which check if the token is a "\n" but it doesn't work.
Can you help me find a solution to the problem?
The in.hasNextLine() and in.hasNext() methods are blocking ones when used with streams: if there is no new line or new token, they will wait until you write something and press enter again.
If you only want one line you can use something like:
package test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int count=0;
Scanner in =new Scanner(System.console().readLine()); //scanning just one line
while(in.hasNext()){
String s=in.next();
System.out.println(s);
count++;
}
System.out.println(count);
in.close();
}
}
Note:
If you execute this code from an IDE, there is no console, so System.console() return null. In order to run it from the command line you need to type something like:
$ pwd
/home/user/development/workspace/test/bin
$ ls
test
$ ls test
Test.class
$ java -classpath /home/user/development/workspace/test/bin/ test.Test
I ran into an issue. Below is my code, which asks user for input and prints out what the user inputs one word at a time.
The problem is that the program never ends, and from my limited understanding, it seem to get stuck inside the while loop. Could anyone help me a little?
import java.util.Scanner;
public class Test{
public static void main(String args[]){
System.out.print("Enter your sentence: ");
Scanner sc = new Scanner (System.in);
while (sc.hasNext() == true ) {
String s1 = sc.next();
System.out.println(s1);
}
System.out.println("The loop has been ended"); // This somehow never get printed.
}
}
You keep on getting new a new string and continue the loop if it's not empty. Simply insert a control in the loop for an exit string.
while(!s1.equals("exit") && sc.hasNext()) {
// operate
}
If you want to declare the string inside the loop and not to do the operations in the loop body if the string is "exit":
while(sc.hasNext()) {
String s1 = sc.next();
if(s1.equals("exit")) {
break;
}
//operate
}
The Scanner will continue to read until it finds an "end of file" condition.
As you're reading from stdin, that'll either be when you send an EOF character (usually ^d on Unix), or at the end of the file if you use < style redirection.
When you use scanner, as mentioned by Alnitak, you only get 'false' for hasNext() when you have a EOF character, basically... You cannot easily send and EOF character using the keyboard, therefore in situations like this, it's common to have a special character or word which you can send to stop execution, for example:
String s1 = sc.next();
if (s1.equals("exit")) {
break;
}
Break will get you out of the loop.
Your condition is right (though you should drop the == true). What is happening is that the scanner will keep going until it reaches the end of the input. Try Ctrl+D, or pipe the input from a file (java myclass < input.txt).
it doesn't work because you have not programmed a fail-safe into the code. java sees that the scanner can still collect input while there is input to be collected and if possible, while that is true, it keeps doing so. having a scanner test to see if a certain word, like EXIT for example, is fine, but you could also have it loop a certain number of times, like ten or so. but the most efficient approach is to ask the user of your program how many strings they wish to enter, and while the number of strings they enter is less than the number they put in, the program shall execute. an added option could be if they type EXIT, when they see they need less spaces than they put in and don't want to fill the next cells up with nothing but whitespace. and you could have the program ask if they want to enter more input, in case they realize they need to enter more data into the computer.
the program would be quite simplistic to make, as well because there are a plethera of ways you could do it. feel free to ask me for these ways, i'm running out of room though. XD
If you don't want to use an EOF character for this, you can use StringTokenizer :
import java.util.*;
public class Test{
public static void main(){
Scanner sc = new Scanner (System.in);
System.out.print("Enter your sentence: ");
String s=sc.nextLine();
StringTokenizer st=new StringTokenizer(s," ");//" " is the delimiter here.
while (st.hasMoreTokens() ) {
String s1 = st.nextToken();
System.out.println(s1);
}
System.out.println("The loop has been ended");
}
}
I had the same problem and I solved it by reading the full line from the console with one scanner object, and then parsing the resulting string using a second scanner object.
Scanner console = new Scanner(System.in);
System.out.println("Enter input here:");
String inputLine = console.nextLine();
Scanner input = new Scanner(inputLine);
List<String> arg = new ArrayList<>();
while (input.hasNext()) {
arg.add(input.next().toLowerCase());
}
You can simply use one of the system dependent end-of-file indicators ( d for Unix/Linux/Ubuntu, z for windows) to make the while statement false. This should get you out of the loop nicely. :)
Modify the while loop as below. Declare s1 as String s1; one time outside the loop. To end the loop, simply use ctrl+z.
while (sc.hasNext())
{
s1 = sc.next();
System.out.println(s1);
System.out.print("Enter your sentence: ");
}
I use this code to input something in the String variable n, after I type what is asked, I don't want the program to print what I typed.
The code:
String n= Scanner.nextLine();
I feel is something you type after Scanner. , but I don't know what is it.
EDITED:
Thanks for answer, but I don't know how to Close the Scanner.
Here is the program...:
My program:
import java.util.Scanner;
public class A{
public static void main(String args[]){
String n;
Scanner bananas=new Scanner(System.in);
System.out.println("First Digit: ");
n=bananas.nextLine();
}
}
When I run the program, on the screen appears: "First Digit:",
then type, let's say I type "apple", the String n will be equals to
"apple", it's ok, that is what I want, what I don't like is that the
program prints on the screen the String n, I didn't want the program
to do that, this is what happens:
First Digit:
apples
What I want I the program to print is just:
"First Digit: ", without showing what I typed, just keep it.
Thanks.
First, you have to instantiate the Scanner, like so:
Scanner scanner = new Scanner(System.in); //if you want to read from the console
Then, you can receive your input.
String n = scanner.nextLine();
Make sure you close your scanner when you don't need it anymore:
scanner.close();
There is nothing tricky about it. You can do it like below and still your variable will hold your desired value.
import java.util.Scanner;
public class A{
public static void main(String args[]){
String n;
Scanner bananas=new Scanner(System.in);
System.out.println("First Digit: ");
n=bananas.nextLine(); // once you are done with input
bananas.close();
}
}
Even if you don't close scanner it won't make any difference as far as your code execution is concerned. Only a warning some where in your class will arise which doesn;t really affect your code. But still it is better to close the scanner once you are done with it. Its a good practice.
I ran into an issue. Below is my code, which asks user for input and prints out what the user inputs one word at a time.
The problem is that the program never ends, and from my limited understanding, it seem to get stuck inside the while loop. Could anyone help me a little?
import java.util.Scanner;
public class Test{
public static void main(String args[]){
System.out.print("Enter your sentence: ");
Scanner sc = new Scanner (System.in);
while (sc.hasNext() == true ) {
String s1 = sc.next();
System.out.println(s1);
}
System.out.println("The loop has been ended"); // This somehow never get printed.
}
}
You keep on getting new a new string and continue the loop if it's not empty. Simply insert a control in the loop for an exit string.
while(!s1.equals("exit") && sc.hasNext()) {
// operate
}
If you want to declare the string inside the loop and not to do the operations in the loop body if the string is "exit":
while(sc.hasNext()) {
String s1 = sc.next();
if(s1.equals("exit")) {
break;
}
//operate
}
The Scanner will continue to read until it finds an "end of file" condition.
As you're reading from stdin, that'll either be when you send an EOF character (usually ^d on Unix), or at the end of the file if you use < style redirection.
When you use scanner, as mentioned by Alnitak, you only get 'false' for hasNext() when you have a EOF character, basically... You cannot easily send and EOF character using the keyboard, therefore in situations like this, it's common to have a special character or word which you can send to stop execution, for example:
String s1 = sc.next();
if (s1.equals("exit")) {
break;
}
Break will get you out of the loop.
Your condition is right (though you should drop the == true). What is happening is that the scanner will keep going until it reaches the end of the input. Try Ctrl+D, or pipe the input from a file (java myclass < input.txt).
it doesn't work because you have not programmed a fail-safe into the code. java sees that the scanner can still collect input while there is input to be collected and if possible, while that is true, it keeps doing so. having a scanner test to see if a certain word, like EXIT for example, is fine, but you could also have it loop a certain number of times, like ten or so. but the most efficient approach is to ask the user of your program how many strings they wish to enter, and while the number of strings they enter is less than the number they put in, the program shall execute. an added option could be if they type EXIT, when they see they need less spaces than they put in and don't want to fill the next cells up with nothing but whitespace. and you could have the program ask if they want to enter more input, in case they realize they need to enter more data into the computer.
the program would be quite simplistic to make, as well because there are a plethera of ways you could do it. feel free to ask me for these ways, i'm running out of room though. XD
If you don't want to use an EOF character for this, you can use StringTokenizer :
import java.util.*;
public class Test{
public static void main(){
Scanner sc = new Scanner (System.in);
System.out.print("Enter your sentence: ");
String s=sc.nextLine();
StringTokenizer st=new StringTokenizer(s," ");//" " is the delimiter here.
while (st.hasMoreTokens() ) {
String s1 = st.nextToken();
System.out.println(s1);
}
System.out.println("The loop has been ended");
}
}
I had the same problem and I solved it by reading the full line from the console with one scanner object, and then parsing the resulting string using a second scanner object.
Scanner console = new Scanner(System.in);
System.out.println("Enter input here:");
String inputLine = console.nextLine();
Scanner input = new Scanner(inputLine);
List<String> arg = new ArrayList<>();
while (input.hasNext()) {
arg.add(input.next().toLowerCase());
}
You can simply use one of the system dependent end-of-file indicators ( d for Unix/Linux/Ubuntu, z for windows) to make the while statement false. This should get you out of the loop nicely. :)
Modify the while loop as below. Declare s1 as String s1; one time outside the loop. To end the loop, simply use ctrl+z.
while (sc.hasNext())
{
s1 = sc.next();
System.out.println(s1);
System.out.print("Enter your sentence: ");
}