Question:
HELP! java programming: my program is not supposed to accept characters or String of characters from the user?
jinRisse
2009-03-11 08:52:28 UTC
hi! i'm a beginner in java programming.

my program is about Sorting. it asks inputs from the user but it's not supposed to accept characters or Strings of charaters from the user. but when it does, it should display a message that the user has keyed in a wrong input. the program should not be terminated, but rather, it would ask the user to give a new input. what should i do?

here's my code:

for my Main Class:

// start of code

package trylang2;


public class Main {

public static void main(String[] args) {

SortChoice pumili = new SortChoice();

pumili.choice();

}

} // end of code



for my SortChoice Class:

// start of code

package trylang2;

import java.util.*;

public class SortChoice extends Main {

int option; // refers to the choice of sorting to be used
int size; // refers to the size or number of content of the array
long[] array; // refers to the array
long input; // refers to the numbers to be sorted
long min;
long temp;

Scanner scan = new Scanner(System.in);
SelectionDaw select = new SelectionDaw();
InsertionDaw insert = new InsertionDaw();
ExchangeDaw exchange = new ExchangeDaw();

public void choice() { //displays the types of Sorting to choose from
do {
System.out.println("Types of Sorting available: ");
System.out.println("\n1: Selection Sorting");
System.out.println("2: Insetion Sorting");
System.out.println("3: Exchange Sorting");
System.out.println("4: Quick Sorting");
System.out.println("5: Heap Sorting");
System.out.println("Or press 6 to exit.");

//asks the user to choose which type of Sorting to use
System.out.println("\nEnter your choice: ");
option = scan.nextInt();

switch (option) {
case 1:
System.out.println("\nYou chose Selection Sorting");
getArraySize();
getUserInput();
System.out.println("\n");

select.selSort(array, size);

System.out.println("\n");
break;
case 2:
System.out.println("\nYou chose Insertion Sorting");
getArraySize();
getUserInput();

System.out.println("\n");

insert.insertionSort(array, size);

System.out.println("\n");

break;
case 3:
System.out.println("\nYou chose Exchange Sorting");
getArraySize();
getUserInput();

System.out.println("\n");

exchange.exchange_sort(array);

System.out.println("\n");
break;
case 4:
// syntax not yet finished - jinrisse
break;
case 5:
// syntax not yet finished - jinrisse
break;
}

if ((option < 1) || (option > 6)) {
System.out.println("Out of range. Please choose again.");
}

} while (option != 6); //program will be terminated if 6 is pressed.

System.out.println("\nThank you for using this program.");
}

public int getArraySize() {
//asks the user to input the integers
System.out.println("Enter number of inputs: ");
size = scan.nextInt();

array = new long[size];

return size;
}

public long getUserInput() {
for (int i = 0; i < array.length; i++) {

System.out.println("\nEnter the integer: ");
input = scan.nextLong();

array[i] = input;
}

System.out.println("\n\nOriginal Line - up: ");
for (int i = 0; i < array.length; i++) { //for each element
System.out.print(array[i] + " "); //displays them
}

return input;
}
} // end of code



for my SelectionDaw class:

// start of code
package trylang2;

public class SelectionDaw {

public void selSort(long[] array, int size) {
for (int x = 0; x < array.length - 1; x++) {
int index_of_min = x;
for (int y = x; y < array.length; y++) {
if (array[index_of_min] > array[y]) {
index_of_min = y;
}
}
long temp = array[x];
array[x] = array[index_of_min];
array[index_of_min] = temp;


for(int i =0; i < array.length; i++) {
System.out.print(array[i] + " ");
}

System.out.println("\n");

}
Three answers:
Charlie S
2009-03-11 09:13:16 UTC
UPDATE: Throwing and Catch errors are a great way as stated above. I would rather go with that, but if its a class project and you havent gone over throwing errors, look up on it. Its great.





I tried setting up the program in my IDE but there are some extra classes missing.



What I believe you want is to put the method where it asks for input into a loop, that if someone enters a valid input, the loop terminates and continues on with the program. If there is a boolean variable or int variable that does not satisfy the loop (hence keep the loop going), the loop will repeat the input process. Just remember to put an output message saying that valid numbers were not entered before the loops reruns.

This should take 1 loop and at least 1 if-statement. The if statement will check to make sure valid input was entered, and if it wasnt, sets the sentinel value the loop is looking for to either

1) a value that terminates the loop if input was successful

or

2)a value that does not satisfy the loop, and causes it to rerun.



It is not a big addition, it should only take a couple lines of code.

Error checking is a large part of just about every program. Have some fun with it :)
anonymous
2009-03-11 09:04:39 UTC
You need to throw an inputmismatch exception. For instance in the following code, the exception is thrown when you try to use the scanner to get the next int but the item being scanned is not an int.



public static void main(String[] args) {



String inputString;

int firstNum, secondNum;

Scanner in = new Scanner(System.in);

boolean continueLoop = true;



do

{

try

{

System.out.print("Enter first number: "); // Display the string.

firstNum = in.nextInt();

System.out.print("Enter second number: ");

secondNum = in.nextInt();



}

catch (InputMismatchException e)

{

System.err.printf("Caught InputMismatchException: ", e);

in.nextLine();

System.out.println("You must enter integers. Try again.");

}

} while (continueLoop);



}
?
2016-10-16 10:03:32 UTC
Please instruct us what you have already and describe what's puzzling to you. I heavily doubt however that your instructor needs the Yahoo answer community to do your homework for you.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...