473,405 Members | 2,141 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,405 software developers and data experts.

char array terminates for loop at spaces

theNewb
Hey All,

My objective is to : Write a small program that asks the user to input the name of a good book that he or she has read recently, stores the input as elements of a character array rather than as a string, then counts the occurrences of the letter 'a' in the input.

My problem is that my program only works when there are no spaces in the title, if there is a space the program stops counting the letter a's after that point. It seems that the spaces are terminating my for loop. Why does that happen?

I've been googling, and picking at the program for hours now -- but I just can't figure this last part out.

Program below:

import java.util.Scanner;

public class GoodBook {

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

System.out.print("Enter the name of a good book you've read recently:");
String yourBook = in.next();
char bookArray [] = yourBook.toCharArray();

int lettersA = 0;
for (int index = 0; index < bookArray.length; index++) {
if ((bookArray[index] == 'a') || (bookArray[index] == 'A')) {
lettersA++;
}
}
System.out.println(lettersA);
}
}


Thanks in advance for any and all help.
Mar 3 '07 #1
3 1840
DeMan
1,806 1GB
I'm not vertain, but I think the scanner tokenises strings using spaces as delimiters,

try
Expand|Select|Wrap|Line Numbers
  1.  String inString = "";
  2. String temp = "";
  3. while(temp != NULL)
  4. {
  5.   inString = inString + tem[;
  6.   temp = in.next();
  7. }
  8.  
then call the toArray on inString and continue as you did
Mar 3 '07 #2
r035198x
13,262 8TB
I'm not vertain, but I think the scanner tokenises strings using spaces as delimiters,

try
Expand|Select|Wrap|Line Numbers
  1. String inString = "";
  2. String temp = "";
  3. while(temp != NULL)
  4. {
  5. inString = inString + tem[;
  6. temp = in.next();
  7. }
  8.  
then call the toArray on inString and continue as you did
The problem is the method used to read in the input.

Calling next() on Scanner does not give the whole line(Just the next word) but calling nextLine() reads the full line.
Just change the code to

Expand|Select|Wrap|Line Numbers
  1.  String yourBook = in.nextLine();
Mar 3 '07 #3
Good to know! Thank-you very much. :)

The problem is the method used to read in the input.

Calling next() on Scanner does not give the whole line(Just the next word) but calling nextLine() reads the full line.
Just change the code to

Expand|Select|Wrap|Line Numbers
  1.  String yourBook = in.nextLine();
Mar 3 '07 #4

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

Similar topics

3
by: Ian Arnold | last post by:
Alright, so I know that you can use char pointers to store strings, and I'm trying to make a very simple program to see how it all works that will have the user enter 5 characters, then the...
10
by: techie | last post by:
I'm creating a class BookType that will store information about books. Each book type can have up to 4 authors. I defined a new type for an array of char: typedef char array4_t; This will...
13
by: mike79 | last post by:
Hello all, I am a newbie to C, so please bear with me :) I need to create an array in a function (a local variable) with size of 1MB. Since local variables get stored on the stack, at...
11
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I've noticed a few threads (full of sound and fury, signifying nothing) here recently about allocation of large memory blocks. I'm about to start on a personal pet project where I'll be using...
11
by: cdg | last post by:
Could anyone tell me how to write a loop for an array, that would have to check each iteration for duplications of previous entered values. So, the exact number of loops is not known, but the total...
3
by: Jacob.Bruxer | last post by:
Hi, I want to be able to append text using a For loop to each textbox in an array of textboxes that I've created, called tBoxes(). Basically I want to add a number of spaces to each textbox in...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
10
by: vignesh4u | last post by:
I am trying to implement the Split function in C ie. if i have a string: char* S="This is a test"; and if i try to split based on space ' ' it should return an array of strings like: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.