• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Scanner to read one line at a time

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that with BufferedReader its pretty simple, but I need to use scanner.

Here's what I have, loosely based off of how BufferedReader does it



the huge comment portion in the middle is what I need this method to do.

I get an error on the inputLine = scanner.readLine()
[ December 11, 2005: Message edited by: Kevin Tansey ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the method is nextLine() and not readLine()
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you, that looks right!
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
building on this scanner thing, I'm having trouble understanding what is needed to be done to complete this method:



I'm getting "the field course.X is not visible" for all 3 of the lines I added
but I can't find any other way of doing that in my book.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like those variables are private in Course. I would recommend creating set methods for those variables.
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I figured it out.



however I get 2 "warnings" (I'm using Eclipse as my editer)
"The local variable title (and numCredits) is never read"

I dont understand what that means or if anything is actually wrong?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Keith]: It looks like those variables are private in Course. I would recommend creating set methods for those variables.



They're not private, they're local. We're looking at a method here, not a class. Most of those variables are of no interest outside the method - the point is to take a String representing a line, and return a Course object. Any variables used along the way are irrelevant outside the method.

[Kevin]: I dont understand what that means or if anything is actually wrong?

Let's just say it's a clue. Going back to my previous paragraph - what exactly is being returned by this method? Have you tested the result in any way? Even, say, by printing it out?
[ December 11, 2005: Message edited by: Jim Yingst ]
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
heh, I haven't figured out how to get anything to run in eclipse yet

so I put all I've got into BlueJ and tried running it and got an exception error at another spot, although I'm not sure if this chunk of code has been executed at that point or not...

the only other location I can see createCourses being used (called) is in these methods:




it looks like its just used as a placeholder for the array semesterList?
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it to run in eclipse, this gives me a better output than blueJ did:


The whole transcript for student1

Transcript:

Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Student.main(Student.java:246)
The gpa is



decimalformat is throwing an exception because it has nothing to format

for the record the output should be this:



The whole transcript for student1

Transcript:
Course: Java (4 credits); grade=A
Course: Basic (2 credits); grade=C
Course: C++ (3 credits); grade=D

The gpa is 2.56

Java for student1 -- Course: Java (4 credits); grade=A
Worst grade for student1 -- Course: C++ (3 credits); grade=D
Change the grade to A
The course for student1 is now -- Course: C++ (3 credits); grade=A
The gpa is now 3.56


[ December 11, 2005: Message edited by: Kevin Tansey ]
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hopefully I didn't scare you guys off




this is the last chunk I'm working on, all it had before I started was the "return null"

right now I'm getting 6 errors:
"getNumCourses cannot be resolved to a type"
"numCourse cannot be resolved"
"course cannot be resolved" x4


I *think* what I need this method to do is print the course listing, but the commenting on it was a bit vague as to what needs to be done ("ADD CODE" doesn't really describe much!)

this is in a seperate file from the other fragments.

this whole file is:

 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well that was pretty easy.

had to step back and think about it a little harder...ugh





now I gotta fix some bugs I guess, as the program doesn't work right

The whole transcript for student1

Transcript:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Transcript.getCourseGrade(Transcript.java:59)
at Transcript.getWorstGradeIndex(Transcript.java:132)
at Student.main(Student.java:250)
The gpa is NaN

Java for student1 -- null
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe my errors are back to the scanner problem



I have 2 warnings for this area:
"the local variable (title, numCredits) is never used" - Student.java
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Kevin]: "the local variable (title, numCredits) is never used" - Student.java

Let's just say it's a clue. Going back to my previous paragraph (in my last post) - what exactly is being returned by this method? Have you tested the result in any way? Even, say, by printing it out?

If the code is copmpiling now (even with warnings), then you can run it and add some code to see what it's doing. For example, you could add this at the end of the createCourse() method:

If you do this, you may discover there's something you have forgotten.
[ December 12, 2005: Message edited by: Jim Yingst ]
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm


The current value of course is: null
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Transcript.getCourseGrade(Transcript.java:59)
at Transcript.getWorstGradeIndex(Transcript.java:132)
at Student.main(Student.java:250)
The current value of course is: null
The current value of course is: null
The whole transcript for student1

Transcript:

The gpa is NaN

Java for student1 -- null



I put it above and below the (catch) area also, same output.


interesting, that means to me that in my scanner area the variables are being read in but I forgot to assign them to the course object
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw, the file I am scanning in is:


Java 4 A
Basic 2 C
C++ 3 D
END



so I dont need to use the useDilimeter(" ") do I? I think because of the spaces they're all treated as seperate tokins and just the scanner.next(); should pick them up
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Kevin]: interesting, that means to me that in my scanner area the variables are being read in but I forgot to assign them to the course object

Yep. Also that you've never actually created a Course object.

[Kevin]: so I dont need to use the useDilimeter(" ") do I? I think because of the spaces they're all treated as seperate tokins and just the scanner.next(); should pick them up

Sounds correct. Of course you will want to test this after you've written it to make sure it works, right? But the idea sounds correct.
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this doesn't work, but am I going in the right direction with this?



it says I "cannot convert from string to course"
[ December 12, 2005: Message edited by: Kevin Tansey ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you're calling createCourse() from within the createCourse() method. There are times when that makes sense (you will probably study recursion soon) but the way you're doing it seems likely to create an infinite loop. Instead of calling createCourse() from wtihin createCourse(), try constructing an instance of Course, and setting its fields with the data you've obtained from the Scanner.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so now you've edited the post during the time I composed the response. Anyway, my last suggestion still stands.
[ December 12, 2005: Message edited by: Jim Yingst ]
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you say constructing an instance of Course, do you mean an array?

course = (title + numCredits + grade);

so instead of that I should create something like:




 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't mean an array - I mean a single instance, such as you might create using a constructor. Something like, I dunno, "Course course = new Course();" That's creating an instance using a constructor.

Now I'm assuming there's actually a class called Course somewhere, since you haven't mentioned compilation errors related to Course being undefined. I don't konw what constructors and methods it has. You may want to show the code for that class in order to continue this discussion.
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's Course



The only problem I see with what you're saying is I was given the declaration of course already as:



so if I try "Course course = new Course();" that is just a duplicate. Everything above the chunks of //comments in the code I've given is instructor provided and not to be changed.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I was assuming that one would simply modify the existing declaration. But if the instructor doesn't want that, OK, don't redeclare the variable course - just set it to a new value, namely the instance you're creating.
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
just set it to a new value, namely the instance you're creating.



I'm not following
[ December 12, 2005: Message edited by: Kevin Tansey ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...


course = new Course();
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Well that certainly looks better.

Having a problem with the course.setGrade line though, its saying

The method setGrade(char) in the type Course is not applicable for the arguments (String)



I tried adding in:

But it didn't take that either, and there is no scanner.nextChar() method either?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is legal Java syntax, but it's uncommon and I would consider it bad style to do an assignment and use the return value of the assignment as the argument of a method call in one line.

Try this.
 
Kevin Tansey
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm, ok.

this is how it stands right now:



and it compiles correctly, but I get more warnings now:

"local variable title, numCredits is never read"

and when I run the program this is the output:
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, no offense but I think it's time to move this to Java in General - Beginner. So here we go...
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic