11 Replies - 273 Views - Last Post: 30 November 2012 - 11:34 AM Rate Topic: -----

#1 marvelaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-October 12

validation input

Posted 28 November 2012 - 08:03 AM

import java.util.Scanner; 

/** 

 * This provides a simple to complex when complete example of how to read input from the keyboard until a
 * certain piece of text has been input. 
 *  
 * @author n.hussain 
 * 
 */ 

public class InputExample { 


private static Scanner input;

public final static void main(String [] args ) { 


	input = new Scanner(System.in);
	
	String next;	// stores the next line input 

	System.out.println("Let's draw something on the screen!");
	
	
	
	GraphicsScreen graphics = new GraphicsScreen(); 

	//graphics.moveTo(100, 100);// move to 100 pixels right, and 100 down. 
	//graphics.lineTo(250, 200);// draw a line to the centre of the screen 
	//graphics.circle(50);// draw a circle at the end of the line 
	//graphics.lineTo(400, 100);// draw a second line 
	//graphics.lineTo(100, 100);// draw back to the starting point 


 
	do {
		System.out.print("Enter a command (\"stop\") to finish : "); 
		next = input.nextLine(); 

		String [] splitupText = next.split(" ");
	
		String command = splitupText[0];
		
	
			if(command.equals("circle")){ 

			String val1 = splitupText[1];
			
			int circle = Integer.parseInt(val1);
		
			graphics.circle(circle); 

		} 
			

		if(command.equals("moveTo")){
			
			String val2 = splitupText[1];
			
			int v2 = Integer.parseInt(val2);
			
			String val3 = splitupText[2];
			
			int v3 = Integer.parseInt(val3);
			
			graphics.moveTo(v2, v3);
			
		} 	



		if(command.equals("lineTo")){ 

			String val2 = splitupText[1];
			
			int x = Integer.parseInt(val2);
			
			String val3 = splitupText[2];
			
			int y = Integer.parseInt(val3);
			
			graphics.lineTo(x,y);  
			
		
		} 	

		if (command.equals("lineTo")){ 
		
			String val2 = splitupText[1];
			
			int v2 = Integer.parseInt(val2);
			
			String val3 = splitupText[2];
			
			int v3 = Integer.parseInt(val3);
			
			graphics.lineTo(v2,v3); 


		} 		
		
		
		}	 
		while ( next.equalsIgnoreCase("stop") == false ); 

		System.out.println("You have decided to stop entering commands. Program terminated!"); 
		
		
		
		

	}

 } 



I am fairly new to programming and need help to include some validation in my code, i have done research but i need a starting point.

Is This A Good Question/Topic? 0
  • +

Replies To: validation input

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,597
  • Joined: 27-December 08

Re: validation input

Posted 28 November 2012 - 08:04 AM

What specific type of validation are you talking about? Can you be more specific as to what you are looking for?
Was This Post Helpful? 1
  • +
  • -

#3 marvelaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-October 12

Re: validation input

Posted 28 November 2012 - 08:49 AM

I need the programme to display error messages for example if a user types in 'circle100' instead of the 'circle50' i need to display an error message.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,597
  • Joined: 27-December 08

Re: validation input

Posted 28 November 2012 - 08:56 AM

So what are the conditions for a valid piece of input? Why is circle100 invalid and circle50 valid? Help me understand in more general terms, as that's how we will be writing code- to be more general and reusable.
Was This Post Helpful? 1
  • +
  • -

#5 jkervin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 30-August 12

Re: validation input

Posted 28 November 2012 - 09:56 AM

use if then else statement ^^
Was This Post Helpful? 1
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: validation input

Posted 29 November 2012 - 07:36 AM

Well, first of all, the user must be aware of what are the commands he should type in. You could use another print statement listing the commands that the user can use so that the user will know the commands that can be typed.

Once you do that, according to the options, you can give 'if' conditions and for an 'else' condition, you can ask the user to try again.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#7 marvelaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-October 12

Re: validation input

Posted 29 November 2012 - 07:42 AM

All i need to include it error checking, so that if the user types an invalid command the program reports this and does not attempt to draw anything.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,597
  • Joined: 27-December 08

Re: validation input

Posted 29 November 2012 - 08:06 AM

Not to be rude, but it's getting a little old trying to guess what a valid command is. If you can't give us either a list of valid commands or a formula for the valid commands, we can't help you. :)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: validation input

Posted 29 November 2012 - 12:46 PM

    String[] valid = {"circle", "lineto", "moveto"};

    int command = 0; 
    do { 
        String line = scanner.nextLine();
        String[] token = line.split(" ");
        if(token.length != 2) {
           System.out.println("Invalid command 2 parameter are expected");
           continue;
        }
        for(command = 0; command < valid.length; ++command) {
            if(token[0].equals(valid[command]))
               break
        }
        if(command == valid.length) {
           System.out.println(token[0] + " is an Invalid command");
    } while(command == valid.length); 


Was This Post Helpful? 1
  • +
  • -

#10 marvelaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-October 12

Re: validation input

Posted 30 November 2012 - 08:42 AM

Thank you pbl for your help but i have just seen this now, i have written up some of my own code and i think i got it right :bananaman:

import java.util.Scanner; 

/** 

 * This provides a simple to complex when complete example of how to read input from the keyboard until a
 * certain piece of text has been input. 
 *  
 * @author n.hussain 
 * 
 */ 

public class InputExample { 


private static Scanner input;

public final static void main(String [] args ) { 


	input = new Scanner(System.in);
	
	String next;	// stores the next line input 

	System.out.println("Let's draw something on the screen!");
	
	
	
	GraphicsScreen graphics = new GraphicsScreen(); 

	//graphics.moveTo(100, 100);// move to 100 pixels right, and 100 down. 
	//graphics.lineTo(250, 200);// draw a line to the centre of the screen 
	//graphics.circle(50);// draw a circle at the end of the line 
	//graphics.lineTo(400, 100);// draw a second line 
	//graphics.lineTo(100, 100);// draw back to the starting point 

	
	boolean stop = false;

 
	do {
		System.out.print("Enter a command (\"stop\") to finish : "); 
		next = input.nextLine(); 

		String [] splitupText = next.split(" ");
	
		String command = splitupText[0];
		
	
			if(command.equals("circle")){ 

			String val1 = splitupText[1];
			
			int circle = Integer.parseInt(val1);
		
			graphics.circle(circle); 

		} 
			

		if(command.equals("moveTo")){
			
			String val2 = splitupText[1];
			
			int v2 = Integer.parseInt(val2);
			
			String val3 = splitupText[2];
			
			int v3 = Integer.parseInt(val3);
			
			graphics.moveTo(v2, v3);
			
		} 	


		if(command.equals("lineTo")){ 

			String val2 = splitupText[1];
			
			int x = Integer.parseInt(val2);
			
			String val3 = splitupText[2];
			
			int y = Integer.parseInt(val3);
			
			graphics.lineTo(x,y);  
			
		
		} 	

		if (command.equals("lineTo")){ 
		
			String val2 = splitupText[1];
			
			int v2 = Integer.parseInt(val2);
			
			String val3 = splitupText[2];
			
			int v3 = Integer.parseInt(val3);
			
			graphics.lineTo(v2,v3); 

			

		} else if (command.equalsIgnoreCase("stop")) {
			 stop = true;
		} else { 
				System.out.println(command + "is not a valid command. Try again");
			
		}
		
	}
	while (!stop);

	
		while ( next.equalsIgnoreCase("stop") == false ); 

		System.out.println("You have decided to stop entering commands. Program terminated!"); 
		
		

	}

 } 



Was This Post Helpful? 0
  • +
  • -

#11 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: validation input

Posted 30 November 2012 - 08:49 AM

Good job...but the user doesn't know what are the commands he can give.

So how about this print statement after line 41.
System.out.println("Please use either of these commands :circle,lineto and moveto"); 


In that way, the user will be aware of the commands he can use and won't unnecessarily give wrong commands.


regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: validation input

Posted 30 November 2012 - 11:34 AM

a lot better to use an array and then switch() on the array index :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1