ikhwan salihin ikhwan salihin - 3 months ago 8
Java Question

exception handling and string converter

Below is my coding, it is still unfinished,

import java.lang.* ;
import java.util.Scanner;

public class pbtwashing
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String satu,dua,tiga,empat,weightrange,message,minus,rinse,spin,d,comp;
int x,min;
double a;
minus="\t\t -----------------------------------------------------------------------------";
System.out.println("\n\n\n\t\t ----------------------------------------------------------------------------");
System.out.println("\t\t| Welcome To PBU Laundry ! |");
System.out.println("\t\t| There are 4 type of wash you can choose. |");
System.out.println("\t\t| -----------------------------------------------------------------------------|");
System.out.println("\t\t| a-Normal Wash | a. 2kg == 30 Mins | b. 3kg == 40 Mins | c. 5kg == 50 Mins |");
System.out.println("\t\t| | |");
System.out.println("\t\t| -----------------------------------------------------------------------------|");
System.out.println("\t\t| b-Hard Wash | a. 2kg == 40 Mins | b. 3kg == 50 Mins | c. 5kg == 60 Mins |");
System.out.println("\t\t| | |");
System.out.println("\t\t| -----------------------------------------------------------------------------|");
System.out.println("\t\t| c-Delicate Wash | a. 2kg == 30 Mins | b. 3kg == 40 Mins | c. 5kg == 50 Mins |");
System.out.println("\t\t| | (Rinse after wash step will take extra 3 seconds) |");
System.out.println("\t\t| -----------------------------------------------------------------------------|");
System.out.println("\t\t| d-Speed Wash | a. 2kg == 30 Mins | b. 3kg == 40 Mins |");
System.out.println("\t\t| | (Cloths weight cannot more than 3kg) |");
System.out.println("\t\t -----------------------------------------------------------------------------");
System.out.print("\t\t\n\n Please choose between (1-4) : ");

min=0;
d="\n\n\t\t\tdone\n\n";
message="\n\n\t\t\tStep 1 : Washing......\n\n";
rinse="\n\n\t\t\tStep 2 : Rinsing......\n\n";
spin="\n\n\t\t\tStep 3 : Spining......\n\n";
comp="\n\n\t\t\tyour laundry is now complete\n\n\t\t\tminutes taken for laundry to complete is : " ;
satu="Normal Wash";
dua="Hard Wash";
tiga="Delicate Wash";
empat="Speed Wash";
weightrange =" Please choose your cloth weight range";

x = in.nextInt();
if (x == 1)
{
System.out.println("\n\n\t\t\t\t\t" + satu + " has been Chosen." + "\n" +minus);
System.out.print("\t\t\t" + weightrange + " (2kg/3kg/5kg) : ");

min=in.nextInt();
switch (min)
{
case 2:
System.out.println(minus);
slowPrint(message, 100);
tenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
tenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
tenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=30;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 3:
System.out.println(minus);
slowPrint(message, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=40;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 5:
System.out.println(minus);
slowPrint(message, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=50;
System.out.print(a);
System.out.println("\n"+minus);
break;
default:
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");

}
}
else if (x == 2)
{
System.out.println("\n\n\t\t\t\t\t" + dua + " has been Chosen." + "\n" +minus);
System.out.print("\t\t\t" + weightrange + " (2kg/3kg/5kg) : ");
min = in.nextInt();

switch (min)
{
case 2:
System.out.println(minus);
slowPrint(message, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=30;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 3:
System.out.println(minus);
slowPrint(message, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=50;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 5:
System.out.println(minus);
slowPrint(message, 100);
twentyminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
twentyminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
twentyminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=60;
System.out.print(a);
System.out.println("\n"+minus);
break;
default:
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");
break;
}
}
else if (x == 3)
{
System.out.println("\n\n\t\t\t\t\t" + tiga + " has been Chosen." + "\n" +minus);
System.out.print("\t\t\t" + weightrange + " (2kg/3kg/5kg) : ");

min=in.nextInt();

switch (min)
{
case 2:
System.out.println(minus);
slowPrint(message, 100);
tenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
tenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
tenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=30;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 3:
System.out.println(minus);
slowPrint(message, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=40;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 5:
System.out.println(minus);
slowPrint(message, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=50;
System.out.print(a);
System.out.println("\n"+minus);
break;
default:
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");
}
}
else if (x == 4)
{
System.out.println("\n\n\t\t\t\t\t" + empat + " has been Chosen." + "\n" +minus);
System.out.print("\t\t\t" + weightrange + " (2kg/3kg/5kg) : ");
min=in.nextInt();
switch (min)
{
case 2:
System.out.println(minus);
slowPrint(message, 100);
tenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
tenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
tenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=30;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 3:
System.out.println(minus);
slowPrint(message, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=40;
System.out.print(a);
System.out.println("\n"+minus);
break;
default:
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");
}
}


else
{
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");
}
}

static void tenminutes()
{
for ( int i = 0; i <= 10; i++ )
{
System.out.println("\t\t\t"+ i + " minutes ");
}
}

static void thirteenminutes()
{
for ( int i = 0; i <= 13; i++ )
{
System.out.println("\t\t\t"+ i + " minutes ");
}
}

static void sixteenminutes()
{
for ( int i = 0; i <= 16; i++ )
{
System.out.println("\t\t\t"+ i + " minutes ");
}
}


static void twentyminutes()
{
for ( int i = 0; i <= 20; i++ )
{
System.out.println("\t\t\t"+ i + " minutes ");
}
}

/**
* Function to print each character in a string with a delay (a "typewriter" effect)
* @param message The string to print
* @param millisPerChar Milliseconds to take to print each character
*/
public static void slowPrint(String message, long millisPerChar)
{
for (int i = 0; i < message.length(); i++)
{
System.out.print(message.charAt(i));
try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
/**
* Function to print each character in a string with a delay (a "typewriter" effect)
* @param String The string to print
* @param millisPerChar Milliseconds to take to print each character
*/
public static void slowPrint1(String rinse, long millisPerChar)
{
for (int i = 0; i < rinse.length(); i++)
{
System.out.print(rinse.charAt(i));

try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

}
/**
* Function to print each character in a string with a delay (a "typewriter" effect)
* @param spin The string to print
* @param millisPerChar Milliseconds to take to print each character
*/
public static void slowPrint2(String spin, long millisPerChar)
{
for (int i = 0; i < spin.length(); i++)
{
System.out.print(spin.charAt(i));

try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public static void slowPrint3(String d, long millisPerChar)
{
for (int i = 0; i < d.length(); i++)
{
System.out.print(d.charAt(i));

try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

}
public static void slowPrint4(String comp, long millisPerChar)
{
for (int i = 0; i < comp.length(); i++)
{
System.out.print(comp.charAt(i));

try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}


can anyone help me apply
NumberFormatException
at the cloth weight range and it will loop if user doesn't meet the input requirement, also i need to convert a string to primitive data .
i have already tried many times but still cant done . thanks in advance for any help.
ps/ sorry for my bad english.

Answer

Note: Your code is too long, I didn't read it.

I kinda get what you want to do. You want to keep asking the user for input until the input is valid. I hope I understood you correctly.

One way to do this is, as you have said, using NumberFormatException:

int userInput;
Scanner scan = new Scanner(System.in);
while(true) {
    try {
        userInput = Integer.parseInt(scan.nextLine());
        break;
    } catch (NumberFormatException e) {
        System.out.println("Input Invalid! Try again!");
    }
}
// do stuff with userInput

However, it is not recommended to use exceptions as a way of controlling the execution because it takes a lot of time to throw an exception.

Another way of checking whether the input is valid, is using regular expressions:

final Pattern pattern = Pattern.compile(<<Regular Expression Here>>);
int userInput;
String userString;
Scanner scan = new Scanner(System.in);
while(true) {
    userString = scan.nextLine();
    if (pattern.matcher(userString).matches()) {
        break;
    } else {
        System.out.println("Input invalid. Try again!");
    }
}
userInput = Integer.parseInt(userString);

See that <<Regular Expression Here>> thingy? If you want the user to enter an unsigned integer, replace that with "\\d+". If you want the user to enter a signed integer, replace that with "[\\+-]?\\d+".