Results 1 to 4 of 4
  1. #1
    Pillowsofurr is offline Member
    Join Date
    Feb 2015
    Posts
    2
    Rep Power
    0

    Default Basic Operations need help..

    Hi I am new to java programming and we just recently learned basic operations etc.
    there's just a problem with my homework when i am displaying the results of my program.
    could you tell me what's wrong???

    this is just a practicing program where we will input student's name, year and grade then we will find out the first two highest grade...

    Java Code:
    package mypackage1;
    
    import java.util.Scanner;
    
    public class Homework {
    	
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		
    		String n1="",n2="",n3="",n4="",n5="";
    		String temp1="", temp2="", temp3="", temp4="", temp5="";
    		
    		int y1,y2,y3,y4,y5;
    		
    		double g1,g2,g3,g4,g5;
    		
    		double top1=0,top2=0;
    		
    		System.out.println("[1]Enter Name:");
    		n1=input.nextLine();
    		input.nextLine();
    		
    		System.out.println("Enter Year Level:");
    		y1=input.nextInt();
    		
    		System.out.println("Enter Grade:");
    		g1=input.nextDouble();
    		
    		temp1=("Name:"+n1+" Year:"+y1+" Grade:"+g1);
    		
    		//second
    		
    		System.out.println("[2]Enter Name:");
    		n2=input.nextLine();
    		input.nextLine();
    		
    		System.out.println("Enter Year Level:");
    		y2=input.nextInt();
    		
    		System.out.println("Enter Grade:");
    		g2=input.nextDouble();
    		
    		temp2=("Name:"+n2+" Year:"+y2+" Grade:"+g2);
    		
    		//third
    		
    		System.out.println("[3]Enter Name:");
    		n3=input.nextLine();
    		input.nextLine();
    		
    		System.out.println("Enter Year Level:");
    		y3=input.nextInt();
    		
    		System.out.println("Enter Grade:");
    		g3=input.nextDouble();
    		
    		temp3=("Name:"+n3+" Year:"+y3+" Grade:"+g3);
    		//fourth
    		
    		System.out.println("[4]Enter Name:");
    		n4=input.nextLine();
    		input.nextLine();
    		
    		System.out.println("Enter Year Level:");
    		y4=input.nextInt();
    		
    		System.out.println("Enter Grade:");
    		g4=input.nextDouble();
    		
    		temp4=("Name:"+n4+" Year:"+y4+" Grade:"+g4);
    		
    		//fifth
    		
    		System.out.println("[5]Enter Name:");
    		n5=input.nextLine();
    		input.nextLine();
    		
    		System.out.println("Enter Year Level:");
    		y5=input.nextInt();
    		
    		System.out.println("Enter Grade:");
    		g5=input.nextDouble();
    		
    		temp5=("Name:"+n5+" Year:"+y5+" Grade:"+g5);
    		
    		//end of getting inputs
    		
    		//conditions
    		
    		System.out.println("Top 1 is ");
    		
    		if(top1<g1)
    		{
    			top1=g1;
    			System.out.print(temp1);
    		}
    		
    		else if(top1<g2)
    		{
    			top1=g2;
    			System.out.print(temp2);
    		}
    		
    		else if(top1<g3)
    		{
    			top1=g3;
    			System.out.print(temp3);
    		}
    		
    		else if(top1<g4)
    		{
    			top1=g4;
    			System.out.print(temp4);
    		}
    		
    		else if(top1<g5)
    		{
    			top1=g5;
    			System.out.print(temp5);
    		}
    		
    		System.out.println("\nTop 2 is");
    		
    		if(top2<g1 && top1>g1)
    		{
    			top2=g1;
    			System.out.println(temp1);
    		}
    		
    		else if (top2<g2 && top1>g2)
    		{
    			top2=g2;
    			System.out.println(temp2);
    		}
    		
    		else if (top2<g3 && top1>g3)
    		{
    			top2=g3;
    			System.out.println(temp3);
    		}
    		
    		else if(top2<g4 && top1>g4)
    		{
    			top2=g4;
    			System.out.println(temp4);
    		}
    		
    		else if(top2<g5 && top1>g5)
    		{
    			top2=g5;
    			System.out.println(temp5);
    		}
    			
    		}
    		
    	}
    when i am already printing the top1 and top2 the name of the top2 does not appear just blank. can you tell me what's wrong??
    Last edited by Pillowsofurr; 02-18-2015 at 01:07 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Basic Operations need help..

    Debug it by printing out the values before going into that if statement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Pillowsofurr is offline Member
    Join Date
    Feb 2015
    Posts
    2
    Rep Power
    0

    Default Re: Basic Operations need help..

    well thanks for the advice i already saw whats wrong.. phew!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Basic Operations need help..

    The Scanner class's methods have issues when the nextLine() method is called after calls to other next..() methods which leave a newline character in the Scanner's buffer. nextLine() returns that newline character as an empty String.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Stack operations
    By Toluwase in forum New To Java
    Replies: 1
    Last Post: 07-09-2012, 12:36 PM
  2. Basic operations with selfmade datatypes
    By PascalS in forum New To Java
    Replies: 5
    Last Post: 04-16-2012, 08:15 AM
  3. Array Operations
    By knoxb in forum New To Java
    Replies: 2
    Last Post: 02-10-2012, 07:06 AM
  4. Operations on Nodes
    By Eleeist in forum New To Java
    Replies: 7
    Last Post: 02-05-2012, 04:43 PM
  5. Date operations
    By SAR in forum New To Java
    Replies: 9
    Last Post: 07-21-2010, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •