Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

Reading in a String with a space character in Java.

OpenStudy (anonymous):

The problem is when I use nextLine() to read in strings with spaces, for some reason it skips scanning in gender.

OpenStudy (anonymous):

@e.mccormick

OpenStudy (e.mccormick):

Because of: age = scanner.nextInt(); The changing back and forth between int and line.

OpenStudy (anonymous):

Ah, thanks, I got it working now. ``` age = 0; try { age = Integer.parseInt(scanner.nextLine()); } catch (NumberFormatException e) { e.printStackTrace(); }

OpenStudy (anonymous):

I just switched it to convert the nextLine() to an int. :D

OpenStudy (e.mccormick):

Yah, it has to do with the newline character that gets left in the buffer.

OpenStudy (e.mccormick):

I found out when I ran into the same thing.

OpenStudy (anonymous):

Ah, well I just started up a java class this semester, it seems quite similar to C++

OpenStudy (e.mccormick):

Oh yah, but objects are not an option in Java and pass by reference is replaced by use of objects. Otherwise, a lot of the syntax is similar.

OpenStudy (anonymous):

so when I do people[i-1] = new Person(name, age, gender); I'm not adding an object to an array?

OpenStudy (anonymous):

I don't understand what you mean when you say, objects are not an option.

OpenStudy (e.mccormick):

You can do a lot of thigns in C++ without making any objects. Just functions and the main. In Java, the instant you make a Scanner you made an object.

OpenStudy (anonymous):

Oh, That makes sense, lol. I was thinking it meant you couldn't use objects in java.

OpenStudy (e.mccormick):

No, no. There is no option to NOT use them. Hehe. They are 100% required. Even the main is treated as an Object. It is one of the fully OOP languages.

OpenStudy (anonymous):

Right right, makes sense now, thanks for the help! I'm gonna get back to the programming, I still seem to be getting and java.lang.ArrayIndexOutOfBoundsException: 2

OpenStudy (e.mccormick):

Computer math: things start at 0.

OpenStudy (anonymous):

Right, I switched the for loop to start at 1 because I needed my print statement where it prints i to start at 1.

OpenStudy (e.mccormick):

Not that loop. You are adjustng it down at the end. This loop: for(int i = 0; i <= entries; i++){ That means for 10 items: 0,1,2,3,4,5,6,7,8,9,10 Oops.

OpenStudy (anonymous):

With this I still seam to be getting ``` Exception in thread "main" Stats: sdf, 3, sdf Stats: df, 3, df java.lang.ArrayIndexOutOfBoundsException: 2 .......................................LabTwoDriver.main(LabTwoDriver.java:48)

OpenStudy (anonymous):

seem*

OpenStudy (e.mccormick):

//Write code to print out the array of objects and their attributes for(int i = 0; i <= entries; i++){

OpenStudy (anonymous):

woops, I still left the <= in the print code haha

OpenStudy (anonymous):

Boom, working now :D

OpenStudy (e.mccormick):

Yah, the first one was doing entries things. The second loop was entries + 1, so out of range.

OpenStudy (anonymous):

Yeps, glad to have that working now, got a lab tomorrow, thanks for all the help!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!