getting user input and checking if it's valid
This commit is contained in:
		
							parent
							
								
									8a80ac9f26
								
							
						
					
					
						commit
						c9aed83465
					
				| @ -4,6 +4,7 @@ import java.util.ArrayList; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Scanner; | ||||
| 
 | ||||
| import fr.packageviewer.pack.SearchedPackage; | ||||
| 
 | ||||
| @ -69,6 +70,7 @@ public class Main { | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		// list all packages in reverse order | ||||
| 		for (int i = packets.size(); i-- > 0; ) { | ||||
| 			SearchedPackage searchedPacket = packets.get(i); | ||||
| 			System.out.printf("%s - %s/%s/%s %s%n\t%s%n", | ||||
| @ -79,5 +81,51 @@ public class Main { | ||||
| 							  searchedPacket.getVersion()==null?"":searchedPacket.getVersion(), | ||||
| 							  searchedPacket.getDescription()); | ||||
| 		} | ||||
| 
 | ||||
| 		System.out.printf("Pick a package to see in details (0-%s) : %n", packets.size()-1); | ||||
| 		Scanner input = new Scanner(System.in); | ||||
| 
 | ||||
| 		// we create vars for the loop | ||||
| 		String packetNumberString; | ||||
| 		int packetNumber; | ||||
| 		boolean notValid; | ||||
| 		// we ask input and check if the user input is correct | ||||
| 		do { | ||||
| 			packetNumberString = input.nextLine(); | ||||
| 			// reset notValid to false, we set it in true only if something is wrong | ||||
| 			notValid = false; | ||||
| 			if(isNumeric(packetNumberString)) { | ||||
| 				packetNumber = Integer.parseInt(packetNumberString); | ||||
| 				if(packetNumber < 0 || packetNumber >= packets.size()) { | ||||
| 					// this number is too big or too small | ||||
| 					System.out.println("Enter a valid number"); | ||||
| 					notValid = true; | ||||
| 				} | ||||
| 			} else { | ||||
| 				// this is not a number | ||||
| 				System.out.println("Enter a valid number"); | ||||
| 				notValid = true; | ||||
| 			} | ||||
| 		} while(notValid); | ||||
| 
 | ||||
| 		input.close(); | ||||
| 
 | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Check if the String given is a number | ||||
| 	 * @param i the String given | ||||
| 	 * @return true if the String is a number | ||||
| 	 * @author Capelier-Marla | ||||
| 	 */ | ||||
| 	 private static boolean isNumeric(String i) { | ||||
| 		try { | ||||
| 			Integer.parseInt(i); | ||||
| 			return true; | ||||
| 		} catch (Exception e) { | ||||
| 			return false; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user