Restrictions about hexa and etc.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1549 Views - Last Post: 20 June 2010 - 03:27 PM Rate Topic: -----

#1 NoobProgramer  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 191
  • Joined: 15-June 09

Restrictions about hexa and etc.

Posted 18 June 2010 - 03:36 AM

here's the code
  System.out.println("NOTE: if your gonna input hexadecimal numbers be sure its only 1-9 and A-F/a-f");
    System.out.println();
   System.out.println("Enter number to convert: ");
    String number = kbd.readLine();


    int originalBase = -2;
    while(originalBase < 2 || originalBase > 16){
    System.out.println("Enter the Orginal Base: ");
    originalBase = Integer.parseInt(kbd.readLine());
    if(originalBase <2 ){
        System.out.println("Too low");
    }else if(originalBase >16){
        System.out.println("Too high");
    }
    }
    BigInteger n = new BigInteger(number,originalBase);
    int base =-2;
    while(base < 2 ||base > 16){
       System.out.println("Enter base FROM 2 - 16 ONLY: ");
     base = Integer.parseInt(kbd.readLine());
     if(base <2){
         System.out.println("Too low");
     }else if(base >16){
         System.out.println("Too high");


  }
  }System.out.println("Result is:"+n.toString(base));
  String yes = "Y";
    String no = "N";

    System.out.println("Do you want to try it again? [Y/N]: ");
     String answer = kbd.readLine();
    if(answer.equals(yes)){
    }else if(answer.equals(no)){
        System.exit(0);
    }
}

}



Questions:
1. How can i put a restriction if a user input G and beyond the program will ask him again since its an invalid hexa?
2. about the yes and no last batch of codes
im almost done w/ it but how can i rerun the program if the user choose Y = yes? that means he want to repeat it again to convert more numbers
w/o pressing shift + f6 "netbeans"
i left the if yes statement blank since i dont know what to put inside of it.

3. if i want a long method algorithm to get the results of any to any conversions? what codes do i needed or can somebody help me out for that long method thing for getting the result and so on.

This post has been edited by NoobProgramer: 18 June 2010 - 04:49 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Restrictions about hexa and etc.

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:10 AM

#1 do a try/catch statement; a NumberFormatExceptionerror means it was invalid, not A-F, 0-9

#2 Put your program in a while loop; after asking yes or no, if no, then break from infinite loop

#3 Don't quite understand your question, but your code should do conversions from any base to any base 2-16 if you followed my code in an earlier post
Was This Post Helpful? 0
  • +
  • -

#3 NoobProgramer  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 191
  • Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 07:25 AM

-edited-

This post has been edited by NoobProgramer: 18 June 2010 - 08:38 AM

Was This Post Helpful? -3
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 07:50 AM

We know that for bases 2-16, we will have digits from 0-F. So if we have a String or an array with the ordering 0123456789ABCDEF, we can check to see if a digit is invalid based on the base. So the largest digit will be at index base-1. So if we're working in base 8, then the largest number will reside at index 7.

As for your second question, you'll want to use an outer loop along the lines of:
do{
    //evaluate number bases part
    
    //then prompt for user input regarding play 
    //again
}while(playAgain);



Duplicate topics merged. Please avoid duplicate posting. :)
Was This Post Helpful? 0
  • +
  • -

#5 NoobProgramer  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 191
  • Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 08:05 AM

View Postbcranger, on 18 June 2010 - 05:10 AM, said:

#1 do a try/catch statement; a NumberFormatExceptionerror means it was invalid, not A-F, 0-9

#2 Put your program in a while loop; after asking yes or no, if no, then break from infinite loop

#3 Don't quite understand your question, but your code should do conversions from any base to any base 2-16 if you followed my code in an earlier post


1. i never used or experience using a try catch code so its kinda hard for me to use it but im reading some info's about it at uncle google.

2. which part of my program? and i already put a if statement below my codes
so if the user chose no then it will exit but about YES i haven't figured it out but anyways switch part?

3. about the number 3
i was wandering if incase i want to change the algorithm of the codes that u gave me to a Long Method algorithm
like the times , minus , divide thingy to convert any numbers to any numbers conversion

since the math.biginterger is kinda shortcut.

View Postmacosxnerd101, on 18 June 2010 - 06:50 AM, said:

We know that for bases 2-16, we will have digits from 0-F. So if we have a String or an array with the ordering 0123456789ABCDEF, we can check to see if a digit is invalid based on the base. So the largest digit will be at index base-1. So if we're working in base 8, then the largest number will reside at index 7.

As for your second question, you'll want to use an outer loop along the lines of:
do{
    //evaluate number bases part
    
    //then prompt for user input regarding play 
    //again
}while(playAgain);



Duplicate topics merged. Please avoid duplicate posting. :)


sorry for double posting Coz' my internet is really slow.


so ill change the if statement to do while statement?
Was This Post Helpful? 1
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 08:05 AM

Please do not double post.

You can use the regex class for pattern matching, heres a quick example I mocked up.

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class matching {

	public static void main (String [] args){
		//instantiate scanner
		Scanner s = new Scanner(System.in);
		//print
		System.out.println("Please enter hex");
		//assign input to String
		String a=s.next();
		//instantiate a Pattern invoking compile with pattern as parameters
		Pattern p = Pattern.compile("[0-9a-fA-F]");
		//we then invoke the matcher() method on the String a to see if it matches 0-9 a-f A-F
		Matcher m = p.matcher(a);
		//if it can find pattern
       if(m.find()){
    	   
    	   //carry on
       }
       else
    	   
		do{
			//we then ask for the user input if it is not valid
			System.out.println("Not valid enter again:");
			//assign input to string a
			a = s.next();
			//check if it matches pattern
			m = p.matcher(a);

		}
		//while it cannot find pattern 
		while(!m.find());
	}
}




Edit - Way too late

This post has been edited by m-e-g-a-z: 18 June 2010 - 08:06 AM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 08:57 AM

Quote

so ill change the if statement to do while statement?

Not exactly. You'll want the loop to contain basically the rest of your program.

Quote

1. i never used or experience using a try catch code so its kinda hard for me to use it but im reading some info's about it at uncle google.

If you don't want to use try-catch, you can implement the approach I described for this.

Quote

2. which part of my program? and i already put a if statement below my codes
so if the user chose no then it will exit but about YES i haven't figured it out but anyways switch part?

More or less, this is the idea.
while(the user input is not yes or no)
   //prompt for play again


Was This Post Helpful? 0
  • +
  • -

#8 NoobProgramer  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 191
  • Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 09:11 AM

View Postm-e-g-a-z, on 18 June 2010 - 07:05 AM, said:

Please do not double post.

You can use the regex class for pattern matching, heres a quick example I mocked up.

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class matching {

	public static void main (String [] args){
		//instantiate scanner
		Scanner s = new Scanner(System.in);
		//print
		System.out.println("Please enter hex");
		//assign input to String
		String a=s.next();
		//instantiate a Pattern invoking compile with pattern as parameters
		Pattern p = Pattern.compile("[0-9a-fA-F]");
		//we then invoke the matcher() method on the String a to see if it matches 0-9 a-f A-F
		Matcher m = p.matcher(a);
		//if it can find pattern
       if(m.find()){
    	   
    	   //carry on
       }
       else
    	   
		do{
			//we then ask for the user input if it is not valid
			System.out.println("Not valid enter again:");
			//assign input to string a
			a = s.next();
			//check if it matches pattern
			m = p.matcher(a);

		}
		//while it cannot find pattern 
		while(!m.find());
	}
}




Edit - Way too late


i tried and run your program
and i tried to input 13G-Z
and your program accepted it.

Quote

2. which part of my program? and i already put a if statement below my codes
so if the user chose no then it will exit but about YES i haven't figured it out but anyways switch part?

More or less, this is the idea.
while(the user input is not yes or no)
   //prompt for play again


[/quote]

sorry if incase that i irritate you w/ my questions
so ill add a while statement before my if statement? or?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 11:58 AM

Quote

sorry if incase that i irritate you w/ my questions
so ill add a while statement before my if statement? or?

Think about exactly what you want to repeat. Do you want to only repeat getting input, then evaluate the validated input after the loop? Do you want to process the input and respond appropriately (ie., if the user entered no, then exit) inside the loop?

Programming and algorithm development isn't about adding this here and that there to patch together a quick fix. It's about thinking about the problem and developing a working solution that is easy for others maintain.
Was This Post Helpful? 0
  • +
  • -

#10 NoobProgramer  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 191
  • Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 05:10 PM

View Postmacosxnerd101, on 18 June 2010 - 10:58 AM, said:

Think about exactly what you want to repeat. Do you want to only repeat getting input, then evaluate the validated input after the loop? Do you want to process the input and respond appropriately (ie., if the user entered no, then exit) inside the loop?

i want to repeat the whole program if the user chooses YES
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 05:50 PM

Great. So try writing the code for this, or at least some pseudo-code. Those questions were intended to get you thinking about a solution. :)
Was This Post Helpful? 0
  • +
  • -

#12 NoobProgramer  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 191
  • Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:11 PM

View Postmacosxnerd101, on 18 June 2010 - 04:50 PM, said:

Great. So try writing the code for this, or at least some pseudo-code. Those questions were intended to get you thinking about a solution. :)

i did some do while loop
but my problem is how can i repeat the program from the top if the users choose a YEs *geez no is more easier :D*

String repeatChoice="";
  do{
    System.out.println("Do you want to try it again? [yes/no]: ");
    String answer = kbd.next();
  }while(repeatChoice.equalsIgnoreCase("yes"));


Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:14 PM

So wouldn't it make sense to have the logic of your program inside the loop? :)
Was This Post Helpful? 0
  • +
  • -

#14 NoobProgramer  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 191
  • Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:23 PM

View Postmacosxnerd101, on 18 June 2010 - 05:14 PM, said:

So wouldn't it make sense to have the logic of your program inside the loop? :)

i tried this one but its kinda long
String repeatChoice="";
  System.out.println("Do you want to try it again? [yes/no]: ");
  String answer = kbd.next();
  do{
       System.out.println("Enter number to convert: ");
     number = kbd.next();
       Pattern c = Pattern.compile("[0-9a-fA-F]");
        Matcher d = c.matcher(number);
       if(m.find()){
       } else
                do{
                        System.out.println("Enter number to convert Again: ");
                       number = kbd.next();
                        m = c.matcher(number);
                }while(!m.find());


     originalBase = -2;
    while(originalBase < 2 || originalBase > 16){
    System.out.println("Enter the Orginal Base: ");
    originalBase = Integer.parseInt(kbd.next());
    if(originalBase <2 ){
        System.out.println("Too low");
    }else if(originalBase >16){
        System.out.println("Too high");
    }
    }
    BigInteger y = new BigInteger(number,originalBase);
     base =-2;
    while(base < 2 ||base > 16){
       System.out.println("Enter base FROM 2 - 16 ONLY: ");
     base = Integer.parseInt(kbd.next());
     if(base <2){
         System.out.println("Too low");
     }else if(base >16){
         System.out.println("Too high");

     }

  }System.out.println("Result is:"+y.toString(base));
    
  }while(repeatChoice.equalsIgnoreCase("yes"));



the problem is it only repeat once.
and haven't put a no.
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:29 PM

Can you work on your formatting and indentation conventions so that I can better follow your logic? :)

Also, do-loops execute first, then evaluate the while(condition). So regardless of what the user enters first, the loop will execute. Better to prompt at the end of the loop.
String repeatChoice=""; 
  System.out.println("Do you want to try it again? [yes/no]: "); 
  String answer = kbd.next(); 



So along the lines of:
do{
   //your number base conversion

   while(input is not YES or NO){
      //prompt for input
   }
}while(the user wants to play again);

//the user has input no, so we are out of
//the loop and quitting the program


Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3