Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Have user enter in 2 inputs instead of using -1 to end. Help appreciated!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Have user enter in 2 inputs instead of using -1 to end. Help appreciated!

    Hey guys im writing a program that determines your grade here is the code so far.
     
    		Scanner s = new Scanner(System.in);
    	String [ ]  name = new String [3];
    	int [ ] score =  new int [10];
    	char [ ] grade = new char [10];
    	int i =0;
    	do {
    	System.out.print("Enter student's Last name: ");
    	name[i] = s.nextLine();
    	System.out.print("Enter student's score: ");
    	score[i] = Integer.parseInt(s.nextLine());
    		System.out.print("Enter student's score: ");
    	score[i] = Integer.parseInt(s.nextLine());
    		System.out.print("Enter student's score: ");
    	score[i] = Integer.parseInt(s.nextLine());
    		System.out.print("Enter Final Exam score: ");
    	score[i] = Integer.parseInt(s.nextLine());
     
    	if (score[i] != -1)
    		i++;
    	}  while (score[i] != -1 &&  i<10);
     
    	for (int j=0; j<i; j++) {
    	if (score[j] >= 90)
    		grade[j] = 'A';
    	else if (score[j] >= 80)
    		grade[j] = 'B';
    	else if (score[j] >= 70)
    		grade[j] = 'C';
    	else if (score[j] >= 60)
    		grade[j] = 'D';
    	else
    		grade[j] = 'F';
    	} // end for
     
    	System.out.println("\nName\t\tScore\t\tGrade");
    	for (int j=0; j<i; j++)
    		System.out.println(name[j]+"\t\t"+score[j]+"\t\t"+grade[j]);
     
     } // end main
    } // end class

    instead of having the user put in -1 to end the inputs of the grades I'm trying to figure out how to just have 2 students put in there grades and be done with it.

    Thanks for your help


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Have user enter in 2 inputs instead of using -1 to end. Help appreciated!

    Simply remove the condition -1 to terminate the loop.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Have user enter in 2 inputs instead of using -1 to end. Help appreciated!

    If I understand you correctly, originally you wanted it to keep adding new students and their score until a user input-1,
    but now you want it to just ask twice(two students) and finish running the program. (printing two students)

    Currently you have:
    do {
    	//stuff
     
    	//increment loop
    	if (score[i] != -1) // you check if you have to increment.(VERY BAD PRACTICE. Ask your teacher why)
    		i++;
     
    }  while (score[i] != -1 &&  i<10);
     
    for (int j=0; j<i; j++) {
     
    	//use data from 1 array to fill another(slightly altered)
     
    } // end for
     
    	// print chart titles
     
    	for (int j=0; j<i; j++)
    	// print scores
     
    } // end for

    You want to change the end condition from a -1 flag to asking just twice.
    your first do loop used -1 condition other then that, nothing changes.

    So, to be proper, change your do/while to a standard for loop that uses i to increment and ends after its asked twice.
    make sure to clear that if statement that you used to increment your do/while loop.

    Hope this helps,
    Jonathan
    Last edited by JonLane; February 21st, 2012 at 05:34 PM.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Have user enter in 2 inputs instead of using -1 to end. Help appreciated!

    @JonLane: Kindly wrap your code in code tags.

Similar Threads

  1. "Separate Numbers"-Problem:Assigning Variables to User Inputs.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 10th, 2012, 07:08 PM
  2. Concatenating Inputs, need help please!
    By xdrechsler in forum File I/O & Other I/O Streams
    Replies: 17
    Last Post: August 19th, 2011, 01:02 PM
  3. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  4. Inputs not being applied.
    By Norflok669 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 01:25 AM
  5. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 05:08 AM