2 Replies - 404 Views - Last Post: 22 November 2008 - 04:11 PM Rate Topic: -----

#1 j_ocel04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-September 08

Not sure what's going on here

Post icon  Posted 22 November 2008 - 03:53 PM

I'm trying to understand some code someone wrote for me. Whatever he did I haven't learned it so i ned to update it. here is a section of the code:
	public void input()
	{
		Scanner keyboard = new Scanner( System.in );
		
		JOptionPane.showInputDialog( "Enter name: " );				 
		name = keyboard.nextLine();
		
		for( int i=0; i<10; i++ )
		{
			JOptionPane.showInputDialog( "Enter grade " + assignment[i] + ": " );
			grades[i] = keyboard.nextInt();
		}				
	}



My question is what is the scanner and keyboard part? The overall program is supposed to store names and grades.

Is This A Good Question/Topic? 0
  • +

Replies To: Not sure what's going on here

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Not sure what's going on here

Posted 22 November 2008 - 03:59 PM

View Postj_ocel04, on 22 Nov, 2008 - 02:53 PM, said:

I'm trying to understand some code someone wrote for me. Whatever he did I haven't learned it so i ned to update it. here is a section of the code:
	public void input()
	{
		Scanner keyboard = new Scanner( System.in );
		
		JOptionPane.showInputDialog( "Enter name: " );				 
		name = keyboard.nextLine();
		
		for( int i=0; i<10; i++ )
		{
			JOptionPane.showInputDialog( "Enter grade " + assignment[i] + ": " );
			grades[i] = keyboard.nextInt();
		}				
	}



My question is what is the scanner and keyboard part? The overall program is supposed to store names and grades.


Kind of a mix up here
Scanner is for console input
JOptionPane for Graphical prompt in a Window
Use one or the other not both

	public void input()
	{
		Scanner keyboard = new Scanner( System.in );
		
		System.out.print( "Enter name: " );				 
		name = keyboard.nextLine();
		
		for( int i=0; i<10; i++ )
		{
			System.out.print( "Enter grade " + assignment[i] + ": " );
			grades[i] = keyboard.nextInt();
		}				
	}




or

	public void input()
	{
		name = JOptionPane.showInputDialog( "Enter name: " );				 
		name = keyboard.nextLine();
		
		for( int i=0; i<10; i++ )
		{
			 String str = JOptionPane.showInputDialog( "Enter grade " + assignment[i] + ": " );
			grades[i] = Integer.parseInt(str);
		}				
	}


Was This Post Helpful? 0
  • +
  • -

#3 j_ocel04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-September 08

Re: Not sure what's going on here

Posted 22 November 2008 - 04:11 PM

got it. thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1