Results 1 to 4 of 4
  1. #1
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default Prompting user input of a string.

    Right now I'm using switch so people can choose what they want to do. But in certain cases where there's an input of a String the program doesn't execute correctly. Instead of letting the user input any string it just runs the whole program. It also does this with if else.

    Java Code:
    /**
     * @(#)Conversions.java
     *
     *
     * @author 
     * @version 1.00 2008/2/26
     */
    
    import java.util.*;
    public class Conversions {
    
        public static void main(String args[]) 
        {
        	double choice;
        	Scanner input = new Scanner(System.in);
        	System.out.println("[1] Hex to binary. [2] Binary to decimal. [3] Binary to hex. [4] Decimal to any.");
        	choice = input.nextDouble();
        	
        	if(choice == 1)
        	{
        	String hexNums;
        		System.out.println("Enter Hex number: ");
        		hexNums = input.nextLine();	
        	}
        	else if(choice == 2)
        	{
        		System.out.println("Enter Binary number: ");
        		int binNums = input.nextInt();
        	}
        	else if(choice == 3)
        	{
        		System.out.println("Enter Binary number: ");
        		String binNumss = input.nextLine();
        	}
        	else if(choice == 4)
        	{
        		System.out.println("Enter Decimal number: ");
        		int decNums = input.nextInt();
        		System.out.println("Enter Base wanted: ");
        		int desiredBases = input.nextInt();
        	}
        	/*
        	 
        	switch(choice)
        	{
        		
        		case 1: 
        		String hexNums;
        		System.out.println("Enter Hex number: ");
        		hexNums = input.nextLine();
        			
        			break;
        		
        		case 2:
        		System.out.println("Enter Binary number: ");
        		int binNums = input.nextInt();
        			
        			 break;
        		
        		case 3: 
        			
        		System.out.println("Enter Binary number: ");
        		String binNumss = input.nextLine();
        			
        			break;
        		
        		case 4: 
        			
        		System.out.println("Enter Decimal number: ");
        		int decNums = input.nextInt();
        		System.out.println("Enter Base wanted: ");
        		int desiredBases = input.nextInt();
        			
        			break;
        	}*/
        	
        	//The next 2 lines are used in all versions.
        	System.out.println("ABCD1234 Base-16 converts to " + BaseConverter.fromHexToBin("ABCD1234") +" in Base-2.\n");
        	System.out.println("E12B47F5 Base-16 converts to " + BaseConverter.fromHexToBin("E12B47F5") +" in Base-2.\n");
        	
        	//The next 2 lines are used in the 90-point versions and above.
        	System.out.println("1011111011101111 Base-2 converts to " + BaseConverter.fromBinToDec("1011111011101111") +" in Base-10.\n");
        	System.out.println("11100100100110110 Base-2 converts to " + BaseConverter.fromBinToDec("11100100100110110") +" in Base-10.\n");
        	
        	//The next line is used in the 95-point versions and above.
        	System.out.println("1011111011101111 Base-2 converts to " + BaseConverter.fromBinToHex("1011111011101111") + " in Base-16.\n");
        	
        	//The next line is used in the 100-point versions and above.
        	System.out.println("11100100100110110 Base-2 converts to " + BaseConverter.fromBinToHex("11100100100110110") + " in Base-16.\n");
        	
        	//The next 3 lines are useed in the 105 and 110-point versions.
        	System.out.println("1000 Base-10 converts to " + BaseConverter.fromDecToAny(1000,5) + " in Base-5.\n");
        	System.out.println("1000 Base-10 converts to " + BaseConverter.fromDecToAny(1000,8) + " in Base-8.\n");
        	System.out.println("200 Base-10 converts to " + BaseConverter.fromDecToAny(200,2) + " in Base-2.\n");
        	
        	//This last line is only used in the 110 point version.
        	System.out.println("48879 Base-10 converts to " + BaseConverter.fromDecToAny(48879,16) + " in Base-16.\n");
        }
    }
    
    class BaseConverter
    {
    	
    	public static String fromHexToBin(String hexNum)
    	{
    		
    		
    		
    		String binNum = ""; 
    	
    		
    		for (int j = 0 ; j<hexNum.length(); j++)
    			switch (hexNum.charAt(j))
    			{
    				case '0' : binNum += "0000"; break;
    				case '1' : binNum += "0001"; break;
    				case '2' : binNum += "0010"; break;
    				case '3' : binNum += "0011"; break;
    				case '4' : binNum += "0100"; break;
    				case '5' : binNum += "0101"; break;
    				case '6' : binNum += "0110"; break;
    				case '7' : binNum += "0111"; break;
    				case '8' : binNum += "1000"; break;
    				case '9' : binNum += "1001"; break;
    				case 'A' : binNum += "1010"; break;
    				case 'B' : binNum += "1011"; break;
    				case 'C' : binNum += "1100"; break;
    				case 'D' : binNum += "1101"; break;
    				case 'E' : binNum += "1110"; break;
    				case 'F' : binNum += "1111"; break;
    			}
    		
    		return binNum;
    	}
    	
    	public static int fromBinToDec(String binNum)
    	{
    		int decNum = 0;
    		//decNum = Integer.parseInt(binNum,2);
    		String bin = binNum;
    		int indexplace;
    		int place;
    		char atplace;
    		long decnum = 0;
    		
    		for(int d = binNum.length(); d>0; d--)
    		{
    			indexplace = d-1;
    			atplace = bin.charAt(indexplace);
    			place = bin.length() - d;
    			switch(atplace)
    			{
    				case '0': decNum += 0*(Math.pow(2,place)); break;
    				case '1': decNum += 1*(Math.pow(2,place)); break;
    			} 
    		}
    		
    		return decNum;
    		
    	}
    	
    	public static String fromBinToHex(String binNum)
    	{
    		String hexNum = "";
    		String bin = binNum;
    		int indexPlace;
    		int place;
    		int decQuotient;
    		int decRemainder;
    		String hexTemp1 = ""; 
    		String hexTemp2 = "";
    		char atPlace;
    		int decNum = 0;
    		for(int x = bin.length(); x > 0; x--)
    		{
    			indexPlace = x - 1;
    			atPlace = bin.charAt(indexPlace);
    			place = bin.length() - x;
    			switch(atPlace)
    			{
    				case '0': decNum += 0*(Math.pow(2,place));
    					break;
    				case '1': decNum += 1*(Math.pow(2,place));
    					break;	
    			}	
    		}
    		decQuotient = decNum;
    		while(decQuotient >= 1)
    		{
    			hexTemp2 = hexNum;
    			decRemainder = decQuotient % 16;
    			if (decRemainder > 9)
    			{
    				switch(decRemainder)
    				{
    					case 10: hexTemp1 = "A";
    						break;
    					case 11: hexTemp1 = "B";
    						break;
    					case 12: hexTemp1 = "C";
    						break;
    					case 13: hexTemp1 = "D";
    						break;
    					case 14: hexTemp1 = "E";
    						break;
    					case 15: hexTemp1 = "F";
    						break;
    				}
    			}
    			else
    			{
    				hexTemp1 = String.valueOf(decRemainder);	
    			}
    			hexNum = hexTemp1 + hexTemp2; 
    			decQuotient = decQuotient / 16;
    		}
    		return hexNum;
    	}
    	
    	
    	public static String fromDecToAny(int decNum, int desiredBase)
    	{
    		String newNum = "";
    		int dec = decNum;
    		int base = desiredBase;
    		int decQuotient;
    		String temp1 = "";
    		String temp2 = "";
    		int decRemainder;
    		decQuotient = dec;
    		while(decQuotient >= 1)
    		{
    			temp2 = newNum;
    			decRemainder = decQuotient % base;
    			if (decRemainder > 9)
    			{
    				switch(decRemainder)
    				{
    					case 10: temp1 = "A";
    						break;
    					case 11: temp1 = "B";
    						break;
    					case 12: temp1 = "C";
    						break;
    					case 13: temp1 = "D";
    						break;
    					case 14: temp1 = "E";
    						break;
    					case 15: temp1 = "F";
    						break;
    				}
    			}
    			else
    			{
    				temp1 = String.valueOf(decRemainder);	
    			}
    			newNum = temp1 + temp2; 
    			decQuotient = decQuotient / base;
    		}
    		return newNum;
    	}
    }
    Any help is appreciated to what I need to do.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    
    public class ConversionsRx {
        public static void main(String[] args) 
        {
    //        double choice;
            Scanner input = new Scanner(System.in);
            System.out.println("[1] Hex to binary. [2] Binary to decimal. " +
                               "[3] Binary to hex. [4] Decimal to any.");
            // The [i]nextLine[/i] method will allow for blocking
            // execution down the line. The [i]nextInt[/i] method may not.
            int choice = Integer.parseInt(input.nextLine());
    /*
            if(choice == 1)
            {
                String hexNums;
                System.out.println("Enter Hex number: ");
                hexNums = input.nextLine();	
            }
            else if(choice == 2)
            {
                System.out.println("Enter Binary number: ");
                int binNums = input.nextInt();
            }
            else if(choice == 3)
            {
                System.out.println("Enter Binary number: ");
                String binNumss = input.nextLine();
            }
            else if(choice == 4)
            {
                System.out.println("Enter Decimal number: ");
                int decNums = input.nextInt();
                System.out.println("Enter Base wanted: ");
                int desiredBases = input.nextInt();
            }
    */
            // A switch argument must be or evaluate-to an [i]int[/i].
            // Giving it a [i]double[/i] won't work.
            switch(choice)
            {
                case 1:       // hex to binary
    //                String hexNums;
                    System.out.println("Enter Hex number: ");
                    String hexNums = input.nextLine();
                    System.out.println("Hex to binary of " + hexNums + " = " +
                                        BaseConverter.fromHexToBin(hexNums));
                    break;
                case 2:       // binary to decimal
                    System.out.println("Enter Binary number: ");
                    // The [i]fromBinToDec[/i] method takes a String argument.
    //                int binNums = input.nextInt();
                    String binStr = input.nextLine();
                    System.out.println("Binary to decimal of " + binStr + " = " +
                                        BaseConverter.fromBinToDec(binStr));
                    break;
                case 3:       // binary to hex
                    System.out.println("Enter Binary number: ");
                    String binNumss = input.nextLine();
                    System.out.println("Binary to hex of " + binNumss + " = " +
                                        BaseConverter.fromBinToHex(binNumss));
                    break;
                case 4:       // decimal to any
                    System.out.println("Enter Decimal number: ");
                    int decNums = input.nextInt();
                    System.out.println("Enter Base wanted: ");
                    int desiredBases = input.nextInt();
                    System.out.println("Decimal value of " + decNums +
                                       " to base " + desiredBases + " = " +
                                        BaseConverter.fromDecToAny(decNums,
                                                              desiredBases));
                    break;
            }
    
            /**
             * This block looks like a demo to show you
             * how to use the BaseConverter class.
             */
    /*
            //The next 2 lines are used in all versions.
            System.out.println("ABCD1234 Base-16 converts to " +
               BaseConverter.fromHexToBin("ABCD1234") +" in Base-2.\n");
            System.out.println("E12B47F5 Base-16 converts to " +
               BaseConverter.fromHexToBin("E12B47F5") +" in Base-2.\n");
        	
            //The next 2 lines are used in the 90-point versions and above.
            System.out.println("1011111011101111 Base-2 converts to " +
               BaseConverter.fromBinToDec("1011111011101111") +" in Base-10.\n");
            System.out.println("11100100100110110 Base-2 converts to " +
               BaseConverter.fromBinToDec("11100100100110110") +" in Base-10.\n");
        	
            //The next line is used in the 95-point versions and above.
            System.out.println("1011111011101111 Base-2 converts to " +
               BaseConverter.fromBinToHex("1011111011101111") + " in Base-16.\n");
        	
            //The next line is used in the 100-point versions and above.
            System.out.println("11100100100110110 Base-2 converts to " +
               BaseConverter.fromBinToHex("11100100100110110") + " in Base-16.\n");
        	
            //The next 3 lines are useed in the 105 and 110-point versions.
            System.out.println("1000 Base-10 converts to " +
                            BaseConverter.fromDecToAny(1000,5) + " in Base-5.\n");
            System.out.println("1000 Base-10 converts to " +
                            BaseConverter.fromDecToAny(1000,8) + " in Base-8.\n");
            System.out.println("200 Base-10 converts to " +
                            BaseConverter.fromDecToAny(200,2) + " in Base-2.\n");
    
            //This last line is only used in the 110 point version.
            System.out.println("48879 Base-10 converts to " +
                            BaseConverter.fromDecToAny(48879,16) + " in Base-16.\n");
    */
        }
    }

  3. #3
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    Edit 3: Got it. Instead of paying attention to anything before this edit. Why do you need to parseInt choice?
    Last edited by apfroggy0408; 03-09-2008 at 07:38 AM.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Why do you need to parseInt choice
    Try using nextInt instead and see if it makes any difference.

Similar Threads

  1. How to get the User Name
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-04-2008, 02:43 PM
  2. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 07:03 AM
  3. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM
  4. How to get the User Name
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:35 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •