d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [java] Bug With My Whitespace Index > Java, Indexing, Whitespace, Bug, String
Add Reply New Topic New Poll
Member
Posts: 111
Joined: Jun 24 2012
Gold: 0.00
Feb 11 2014 10:56pm
So this is my code. I'm writing a much larger program for a classwork assignment. I've only posted a snippet and I'm not looking for new code so nobody has to worry there. I'm trying to figure out why my indexing isn't working. Here's the code:

Again, this is a snippet from a much larger class. My issue is with my spaceIndex = enteredGrades.indexOf(" ")
Code
String programString, examString, enteredGrades;
final int programGrade, examGrade;
System.out.println("Enter a program grade and then an exam grade: ");
enteredGrades = keyboard.next();

System.out.print(enteredGrades);

int firstIndex = 0,
spaceIndex = enteredGrades.indexOf(" "),
lastIndex = spaceIndex + 1;

System.out.print(" " + firstIndex + ", " + spaceIndex + ", " + lastIndex);


I execute my code and enter
Code
80 90


It prints out
Code
80 0, -1, 0


Can anyone help me figure out why my whitespace isn't indexing? If I make it index with single char quotations, I get the same output. Any help would be great. Thank you!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 11 2014 11:01pm
Code
keyboard.next()


Read the documentation

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()

Quote
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.


your problem isn't that the space isn't indexing, your problem is that keyboard.next() will only read until the space since space is the default delimiter. you have four options:
1) do not separate your numbers by spaces
2) keep reading until you get all tokens (nextInt might be better than next)
3) change the delimiter
4) use nextLine() instead of next() -> i think this works, but you can double check

This post was edited by carteblanche on Feb 11 2014 11:08pm
Member
Posts: 111
Joined: Jun 24 2012
Gold: 0.00
Feb 11 2014 11:02pm
In fact, looking closer, it's not even printing the
Code
90
from the System.out.print statement on the String. I suppose that's where my issue lies. It's not being entered into the designated variable correctly?
Member
Posts: 111
Joined: Jun 24 2012
Gold: 0.00
Feb 11 2014 11:04pm
Quote (carteblanche @ Feb 12 2014 01:01am)
Code
keyboard.next()


Read the documentation

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()



your problem isn't that the space isn't indexing, your problem is that keyboard.next() will only read until the space.


Oh wow I didn't know that at all. Thanks so much for the help!
Member
Posts: 111
Joined: Jun 24 2012
Gold: 0.00
Feb 11 2014 11:16pm
So I had done exactly that before reading your edit, and replaced next() with nextline(). A brand new type of error appeared, so I'll post a little bit more of my code to show you where the new error lies. Again, I don't want anyone to think I'm looking for physical code. The new bug isn't even appearing by the compiler.

The code:
Code
String programString, examString, enteredGrades;
final int programGrade, examGrade;
System.out.println("Enter a program grade and then an exam grade: ");
enteredGrades = keyboard.nextLine();

System.out.print(enteredGrades);

int firstIndex = 0,
spaceIndex = enteredGrades.indexOf(" "),
lastIndex = spaceIndex + 1;

System.out.print(" " + firstIndex + ", " + spaceIndex + ", " + lastIndex);

programString = enteredGrades.substring(firstIndex, spaceIndex);
programString = programString.trim();
programGrade = Integer.parseInt(programString);
System.out.print(programGrade);

examString = enteredGrades.substring(lastIndex, enteredGrades.length()-1);
examString = examString.trim();
examGrade = Integer.parseInt(examString);
System.out.print(examGrade);


The input:
Code
80 90


The output:
Code
80 90 0, 2, 3809


It's not even printing the statements after the sections that parse. It's jumping into the next method that it's supposed to after execution.

e/ Yes it is! It's printing, without spaces, 809. So I figured out my issue. It's the enteredGrades.length()-1 that's ruining my output. Thanks for the help!

This post was edited by haeload on Feb 11 2014 11:25pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 11 2014 11:25pm
it's printing like it's supposed to. i'm not sure what you think the problem is. i think you're confusing yourself because you use print(..) instead of println(..).

if you'd like to make your life easier, i recommend you look at String's split() method. it will do the parsing for you. it looks like your last index is off.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 11 2014 11:33pm
input is

Code
80 90


You then print enteredGrades, which it shows.

You then set firstIndex to 0, spaceIndex to enteredGrades.indexOf(" ") which is 2, and lastIndex as spaceIndex + 1 which is 3

You then print firstIndex, spaceIndex, and lastIndex which it shows

You then set programString to enteredGrades.substring(firstIndex, spaceIndex) which is substring(0,2). Which returns a string starting from firstIndex of length spaceIndex - firstIndex, which is 2. Setting programString to "80". You trim whitespace, which there is none, parse it to an int, and then print it. Which it shows.

You then set examString to be enteredGrades.substring(lastIndex, enteredGrades.length()-1) which is substring(3, 4) since enteredGrades.length() is 5. This returns a substring starting from lastIndex of length enteredGrades.length()-1-lastIndex which is 5-1-3 which is 1. Setting examString to "9". Trim it, parse it. print it. Which it shows
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll