Jump to content

[Java] Program cannot count spaces placed after a newline when counting spaces in a string

WhitetailAni
Go to solution Solved by Vicarian,
On 11/30/2022 at 2:02 PM, FakeKGB said:

For my CS class (it's Java because CollegeBoard), I'm attempting to create a program that counts all the spaces in a given block of text. Said block of text includes newlines, which my code isn't parsing.

Here's my code:

import java.util.*;
public class Main {

    public static void main(String[] args) {
	Scanner input = new Scanner(System.in); // get ready to receive text
	System.out.print("Paste your text here:");
	String str = input.nextLine() // places given text into a string
	int length = str.length();
	int space = 0;
	for(int i=0; i<length-1; i++) {
		if(str.charAt(i) == ' ') {
		    space++;
		}
	}
	System.out.println(space);

When given any length of text without newlines, it parses it perfectly. example:

for real this should work why doesnt it ok maybe it does work what if i add a newline

^^ This returns 18, which is how many spaces there are.

 

But if I add a newline, to make the text this:

for real this should work why doesnt it ok maybe it does work
what if i add a newline

it returns 12 spaces, which is how many spaces there are in the first line of text. It ignores all spaces in the second line.

 

Why is this happening? It should just ignore the \n and parse the rest of the spaces but it's not and I can't figure out why.

 

Note: I won't be able to test solutions until tomorrow, I'm writing this as my CS class ends

Thanks!

From the docs:
 

Quote

nextLine()

Advances this scanner past the current line and returns the input that was skipped.

From: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Which to my mind means that @Eigenvektor is correct.  You'd need to repeatedly invoke

input.nextLine()

in a loop until such time as there is no further input.  Meanwhile, you'd concatenate each line into your string.  Look into

Scanner::hasNextLine()

and use a while loop.

For my CS class (it's Java because CollegeBoard), I'm attempting to create a program that counts all the spaces in a given block of text. Said block of text includes newlines, which my code isn't parsing.

Here's my code:

import java.util.*;
public class Main {

    public static void main(String[] args) {
	Scanner input = new Scanner(System.in); // get ready to receive text
	System.out.print("Paste your text here:");
	String str = input.nextLine() // places given text into a string
	int length = str.length();
	int space = 0;
	for(int i=0; i<length-1; i++) {
		if(str.charAt(i) == ' ') {
		    space++;
		}
	}
	System.out.println(space);

When given any length of text without newlines, it parses it perfectly. example:

for real this should work why doesnt it ok maybe it does work what if i add a newline

^^ This returns 18, which is how many spaces there are.

 

But if I add a newline, to make the text this:

for real this should work why doesnt it ok maybe it does work
what if i add a newline

it returns 12 spaces, which is how many spaces there are in the first line of text. It ignores all spaces in the second line.

 

Why is this happening? It should just ignore the \n and parse the rest of the spaces but it's not and I can't figure out why.

 

Note: I won't be able to test solutions until tomorrow, I'm writing this as my CS class ends

Thanks!

elephants

Link to comment
Share on other sites

Link to post
Share on other sites

Pretty sure you'd need to call this multiple times to get the next line after a line break?

input.nextLine()

Try echoing the text you read back onto the terminal. You'll only see the first line, since you never parse anything else.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Java.util.Scanner.nextLine only gets a single line, you'll need to figure out a way to loop it if you want multiple line inputs to work.

 

for(int i=0; i<length-1; i++) {
	if(str.charAt(i) == ' ') {
	    space++;
	}
}

This loop also ends in the wrong place. Ending when i < length-1 means it ends at index length-2, missing the last character of the string. You either want i <= length-1 or i < length.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

On 11/30/2022 at 2:02 PM, FakeKGB said:

For my CS class (it's Java because CollegeBoard), I'm attempting to create a program that counts all the spaces in a given block of text. Said block of text includes newlines, which my code isn't parsing.

Here's my code:

import java.util.*;
public class Main {

    public static void main(String[] args) {
	Scanner input = new Scanner(System.in); // get ready to receive text
	System.out.print("Paste your text here:");
	String str = input.nextLine() // places given text into a string
	int length = str.length();
	int space = 0;
	for(int i=0; i<length-1; i++) {
		if(str.charAt(i) == ' ') {
		    space++;
		}
	}
	System.out.println(space);

When given any length of text without newlines, it parses it perfectly. example:

for real this should work why doesnt it ok maybe it does work what if i add a newline

^^ This returns 18, which is how many spaces there are.

 

But if I add a newline, to make the text this:

for real this should work why doesnt it ok maybe it does work
what if i add a newline

it returns 12 spaces, which is how many spaces there are in the first line of text. It ignores all spaces in the second line.

 

Why is this happening? It should just ignore the \n and parse the rest of the spaces but it's not and I can't figure out why.

 

Note: I won't be able to test solutions until tomorrow, I'm writing this as my CS class ends

Thanks!

From the docs:
 

Quote

nextLine()

Advances this scanner past the current line and returns the input that was skipped.

From: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Which to my mind means that @Eigenvektor is correct.  You'd need to repeatedly invoke

input.nextLine()

in a loop until such time as there is no further input.  Meanwhile, you'd concatenate each line into your string.  Look into

Scanner::hasNextLine()

and use a while loop.

Link to comment
Share on other sites

Link to post
Share on other sites

19 hours ago, Vicarian said:

From the docs:
 

Which to my mind means that @Eigenvektor is correct.  You'd need to repeatedly invoke

input.nextLine()

in a loop until such time as there is no further input.  Meanwhile, you'd concatenate each line into your string.  Look into

Scanner::hasNextLine()

and use a while loop.

This was it, thanks! Set up this:

String strIn = new String();
		String str = new String();
		Scanner sc = new Scanner(input);
		while(sc.hasNextLine()){
			strIn = sc.nextLine();
			str = str + strIn;
		}
		sc.close();
		int length = str.length();
		int spaceCount = 0;
		for(int i=0; i<length; i++) {
			if(str.charAt(i) == ' ') {
				spaceCount++;
			}
		}
		System.out.println("The document provided has " + lines + " lines of text.");
		System.out.println("The document provided has " + spaceCount + " spaces.");

and it now accurately counts the number of spaces and lines.

elephants

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×