Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-08-2008, 01:57 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
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.

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-08-2008, 06:39 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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 nextLine method will allow for blocking // execution down the line. The nextInt 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 int. // Giving it a double 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 fromBinToDec 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"); */ } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-09-2008, 07:28 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
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 08:38 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-09-2008, 07:23 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Why do you need to parseInt choice
Try using nextInt instead and see if it makes any difference.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 08:01 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org