Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-01-2007, 03:29 AM
Member
 
Join Date: Jun 2007
Posts: 92
Marcus is on a distinguished road
Algorithm problem
This code is supposed to allow the user to enter a 5 student IDs and their results and calculate the total and show the grades all the information should be shown in a table. For some reason the first studentID is indented - does anyone know why?

Code:
import java.util.*; public class Assignment { public static void main(String[] args) { //Declares variables and arrays int i = 0; int j = 0; String [] studentIDArray = new String [5]; int [][] studentArray = new int [5][9]; Scanner kybd = new Scanner(System.in); //Welcome message to user System.out.println("Welcome"); System.out.println("~~~~~~~"); System.out.println("Please note any unanswered questions should marked as -1"); System.out.print ("\n"); //User is prompted to enter student ID and the results for (i = 0; i < 5; ++i) { System.out.print("Please Enter the Student ID: "); studentIDArray[i] = kybd.next(); for (j = 0; j < 8; ++j) { System.out.println("Please Enter the Result for Question " + (j+1)); studentArray[i][j] = kybd.nextInt(); } } //Calculates the students total while (j < 8) { j = 0; while (i < 5) { studentArray[i][9] += studentArray[i][j]; j+=1; } i+=1; } int k = 0; String grade = ""; String [] gradeArray = new String [5]; //Declares new array to store grades //Calculates the grades for each student while (i < 5) { studentArray[i][8] += 3; if (studentArray[i][8] > 70) { gradeArray[k] = "Distinction"; } else if (studentArray[i][8] > 60) { gradeArray[k] = "Merit"; } else if (studentArray[i][8] > 40) { gradeArray[k] = "Pass"; } else if (studentArray[i][8] >= 40) { gradeArray[k] = "Fail"; } k+=1; i+=1; } System.out.print ("\n"); System.out.println ("StudentID Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Total Grade"); System.out.print (" "); i=0; j=0; k=0; //Output students results while (i < 5) { j = 0; System.out.printf(studentIDArray[i]); while (j < 9) { System.out.printf ("%7d", studentArray[i][j]); j+=1; } System.out.print(" " + gradeArray[k]); System.out.print ("\n"); i+=1; k+=1; } } }
Marcus
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-01-2007, 03:33 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
Well, one thing at a time:

Code:
System.out.println ("StudentID Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Total Grade"); System.out.print (" ");
Why are you printing two spaces - without a newline after the header? It will simply add these two spaces to the next line - like it does.

As far as the other issues - I'd strongly encourage you to add some System.out.prinln() statements here and there to understand what is going on.

Felissa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2007, 03:37 AM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
Can I make a suggestion? - print out the value of "i" and "j" before you enter the while loops. It looks like you're incrementing but never resetting them.

Eric
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with equation in my algorithm romina New To Java 2 07-20-2007 09:53 AM
Help with algorithm susan New To Java 1 07-14-2007 12:26 AM
Problem with algorithm Albert AWT / Swing 1 07-13-2007 05:31 PM
Help me with this algorithm Marcus Advanced Java 3 07-02-2007 03:30 PM
Help with Algorithm Daniel Advanced Java 2 07-02-2007 07:51 AM


All times are GMT +3. The time now is 04:38 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org