Reading in a String with a space character in Java.
The problem is when I use nextLine() to read in strings with spaces, for some reason it skips scanning in gender.
@e.mccormick
Because of: age = scanner.nextInt(); The changing back and forth between int and line.
http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx
Ah, thanks, I got it working now. ``` age = 0; try { age = Integer.parseInt(scanner.nextLine()); } catch (NumberFormatException e) { e.printStackTrace(); }
I just switched it to convert the nextLine() to an int. :D
Yah, it has to do with the newline character that gets left in the buffer.
I found out when I ran into the same thing.
Ah, well I just started up a java class this semester, it seems quite similar to C++
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.
so when I do people[i-1] = new Person(name, age, gender); I'm not adding an object to an array?
I don't understand what you mean when you say, objects are not an option.
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.
Oh, That makes sense, lol. I was thinking it meant you couldn't use objects in java.
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.
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
Computer math: things start at 0.
Right, I switched the for loop to start at 1 because I needed my print statement where it prints i to start at 1.
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.
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)
seem*
//Write code to print out the array of objects and their attributes for(int i = 0; i <= entries; i++){
woops, I still left the <= in the print code haha
Boom, working now :D
Yah, the first one was doing entries things. The second loop was entries + 1, so out of range.
Yeps, glad to have that working now, got a lab tomorrow, thanks for all the help!
Join our real-time social learning platform and learn together with your friends!