![]() |
| ||
| Homework help Hi all, I'm new here, and am currently taking an introductory programming course taught with Java 5. I really hate to come here, and bother people with my homework, but I really don't have anywhere else to turn at the moment. I have a couple of quick questions about some assignments. Any help you all could give me would be very much apreciated. My first question concerns an assignment I have already completed, but had alot of problems with scanner commands on. We were given a shell program to copy, and modify, however there was a problem with a section of the program that I coppied not working correctly. Here is the problem section: import java.util.Scanner; For some reason when the program ran the "rating = scan.nextLine();" was ignored. When the previous scan command "currentSalary = scan.nextDouble();" was removed from the program though the scan.nextLine(); command would work. I wound up using scan.next instead of scan.nextLine, and the program ran as intended. I'm just curious why this happened. My second question concerns a CodeLab(a programming Q & A website) question that I have been stuck on for quite some time. Here is the question: Quote:
Thanks in advance. |
| ||
| Re: Homework help On the first exercise, you should have used .next() to read the string instead of .readLine(). I'm not sure why that didn't work unless that method is only for the BufferedReader. Maybe someone can help clarify that for you. rating = scan.next(); On the second assignment, you should have something of the sort: variable 1; That is of course only the constructor. That should help you out a little. For me to help you out more, you'll have to show some work so I don't get yelled at for doing someones homework. One more thing, when I'm assigning those objects variables to the current objects, I'm guessing you have methods in the other class that returns the variables value. |
| ||
| Re: Homework help Thank you very much. Turns out I was using the right parameter, and just forgetting the "c." infront of my getters. So it was easy to fix with your help. |
| ||
| Re: Homework help I was reading the api for Scanner and I could not find any readLine method. I think the reason it was allowed to compile, is somewhere down the line it inherits that method...But if it inherited it, it should be able to use it...?? I'm confused on that one. I know the readLine() method is suppose to be for the BufferedReader, but it didn't give us a syntax error. |
| ||
| Re: Homework help You missunderstood my question conserning the scanner methods a bit. The problem command was nextLine, not readLine. I'll try and explain a little better. As part of a larger program I need to scan a double and a string token, and for some reason the nextLine command would not work when proceeded by a nextDouble, however it would work if the proceeding nextDouble was removed from the program. So here are three short programs I used to test the issue, and the results... import java.util.Scanner; This would scan the double, then print the double, and then end. Skipping the nextLine command in the process.
Now without the proceeding nextDouble. It will scan the string, and print it.
Finally, if I use next instead of nextLine. It scans both the double, and the string token, and prints both. |
| ||
| Re: Homework help Opps the second program should use the nextLine method not the next method so...
|
| ||
| hey I am new in java and i want a program that makes the user enter his e_mail address like (user1@liu.edu.uk) and the program output it's: country code: uk master domain: edu sub domain: liu user name: user1 please can anny one made it?? :S:S |
| ||
| Tenchi, your problem with the nextLine() not working is the same problem I recently explained to another user on Daniweb. I'll paste my response to him here since the problem you have is exactly identical. "The reason it gives you trouble is because when the user enters an integer then hits enter, two things have just been entered - the integer and a "newline" which is \n. The method you are calling, "nextInt", only reads in the integer, which leaves the newline in the input stream. But calling nextLine() does read in newlines, which is why you had to call nextLine() before your code would work. You could have also called next(), which would also have read in the newline." |
| ||
| Quote:
|
| All times are GMT -4. The time now is 4:10 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC