5 Replies - 688 Views - Last Post: 07 February 2013 - 08:38 PM Rate Topic: -----

#1 sm1th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-February 13

Sentinel controlled while-loop problem

Posted 07 February 2013 - 03:06 PM

Hi everybody, I'm new to this site, so please bear with me and give me suggestions as to how I can improve my question-asking. If you need more information, please tell me.

I'm writing a test driver class to test my Student, and Course, and Course subclasses).

My goal is to scan in a text file for input and then create a student, and then Courses for that student. Sample input would look like this:

Jane Doe
CS1083 4 A+ 1
HIST1305 3 D 4
CS1073 4 A- 1
end



The problem is trying to quit the while loop. My code so far looks like this:

        ...

        String end = "end";

        Scanner scan = new Scanner(System.in);
        
        first = scan.next();
        last = scan.next();
        
        Student stu = new Student(first, last);
        
        do
        {
            title = scan.next();
            credit = scan.nextInt();
            grade = scan.next();
            category = scan.nextInt();
            
            if (category == 1)
            {
                Course cat1 = new Category1(title, grade, credit);
                stu.addCourse(cat1);
            }
            
            else if (category == 2)
            {
                Course cat2 = new Category2(title, grade, credit);
                stu.addCourse(cat2);
            }
            
            else if (category == 3)
            {
                Course cat3 = new Category3(title, grade, credit);
                stu.addCourse(cat3);
            }
            
            else if (category == 4)
            {
                Course cat4 = new Category4(title, grade, credit);
                stu.addCourse(cat4);
            }
            
            else
            {
                System.out.println("Invalid category");
            }
            
        } while (!(scan.next()).equals(end));

        System.out.println(stu.toString());

...



I can enter a first name, last name, and 1 course fine:

Jane Doe
CS1083 4 A+ 1
end



But when I try to keep going (enter a new course), it gives me an InputMismatchException.

I'm pretty sure the problem is that, where I have

 
        } while (!(scan.next()).equals(end));



, it is scanning for another String, instead of going back up and scanning for another title. I just don't know quite how to fix it.

Any help and suggestions would be greatly appreciated. As you can see, this is a pretty simple problem, but I'm not a very experienced programmer :)/>.

Is This A Good Question/Topic? 0
  • +

Replies To: Sentinel controlled while-loop problem

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Sentinel controlled while-loop problem

Posted 07 February 2013 - 03:41 PM

You are reading the next token without saving it
The next token could be the course title, and you have no way of using it because you don't save it
while (!(scan.next()).equals(end));



You are reading the grade as an int, but A+ is not an int. You might want to change it into a String
grade = scan.next();



This is how I would have done it
		Scanner scan = new Scanner(new File("students.txt"));
		
		while (scan.hasNext()) {
			String firstname = scan.next();
			String lastname = scan.next();
			System.out.println(firstname + " " + lastname);
			
			String token;
			while (!(token = scan.next()).equals("end")) {
				String title = token;
				int credit = scan.nextInt();
				String grade = scan.next();
				int category = scan.nextInt();
				
				System.out.println(title + " " + credit + " " + grade + " " + category);
			}
		}


Was This Post Helpful? 0
  • +
  • -

#3 sm1th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-February 13

Re: Sentinel controlled while-loop problem

Posted 07 February 2013 - 07:29 PM

Okay, that doesn't really help much.

I just need it to quit the loop when the user types end. I don't want any of the functionality changed. Maybe I don't understand what you're trying to say :/.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Sentinel controlled while-loop problem

Posted 07 February 2013 - 07:36 PM

Quote

My goal is to scan in a text file for input and then create a student, and then Courses for that student


Doesn't my code read in the data? You will then only need to create the student/category objects =o
Was This Post Helpful? 0
  • +
  • -

#5 sm1th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-February 13

Re: Sentinel controlled while-loop problem

Posted 07 February 2013 - 07:40 PM

Sorry, never made that clear.

I am using:

java TestDriver < input.txt
Was This Post Helpful? 0
  • +
  • -

#6 sm1th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-February 13

Re: Sentinel controlled while-loop problem

Posted 07 February 2013 - 08:37 PM

I've got it! Just needed to change a few things, thanks for your insight CasiOo!

   
        String token;

        while (!(token = scan.next()).equals("end")) 
        {
            title = token;
            credit = scan.nextInt();
            grade = scan.next();
            category = scan.nextInt();



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1