Help Needed with base converter program!
Hi all,
I have to create a base converter as my university assignment. it was fairly simple to craete the code for, it compiles but there is a problem!
When i select to convert from base 16 to base 10, the program skips the input line and just executes whatever is further down without first obtaining an input from the keyboard. i have the scanner class set up properly and i have a unique variable name for that specific input. as i said, it compiles perfectly but does not read the input from the keyboard when converting from base 16 to base 10. Any help would b much appreciated.
Below is the source code for my base converter. please feel free to compile it and help me solve this problem.
import java.util.*;
public class BaseConverter
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please select one of the following options");
System.out.println(" 1. To convert from base 10 to base 16");
System.out.println(" 2. To convert from base 16 to base 10");
System.out.print("Enter your option (1 or 2): ");
int selection = keyboard.nextInt();
switch (selection)
{
case 1:
{
int base = 16;
int remainder;
String hex = "";
String letters = "ABCDEF";
String remfinal = "";
String hexnum="";
System.out.print("Enter the integer in base 10: ");
int number = keyboard.nextInt();
do
{
remainder = number%base;
number = number/base;
if (remainder < 10)
{
remfinal = ""+ remainder;
}
else
{
remfinal = ""+letters.charAt(remainder - 10);
}
hex = hex + remfinal;
}
while ( number > 0);
for (int i= hex.length()-1; i>=0; i--)
{
hexnum = hexnum+hex.charAt(i);
}
System.out.println("The integer in base 16 is: " + hexnum);
}
break;
case 2:
{
String reference = "0123456789ABCDEF";
double total =0;
String hex = "";
System.out.println("Enter your hex number: ");
String input = keyboard.nextLine().toUpperCase();
for(char ch = 'G'; ch <='Z'; ch++)
{
for(int m=0; m<input.length(); m++)
{
if ( input.charAt(m)==ch)
System.out.println("Your input is invalid!");
System.exit(0);
}
}
for (int j = input.length()-1, i=0; j>=0 ; j--, i++)
{
total =total+(Math.pow(16,i))*(reference.indexOf(input.charAt(j)));
}
System.out.println("The hex number: "+input+" is:"+total+" in decimal format.");
break;
}
default:
{
System.out.println("Invalid Selection!");
}
}
}
}
hehe silly me. i figured out the solution 2 mins after i posted, even tho i spent hours trying to find the solution prior to posting... dont you just love the human brain....
anyway the solution was simple. all i needed to do was enclose the following segment of code in curley brackets:
for(int m=0; m<input.length(); m++)
{
if ( input.charAt(m)==ch)
System.out.println("Your input is invalid!");
System.exit(0);
}
so that it became
for(int m=0; m><input.length(); m++)
{
if ( input.charAt(m)==ch)
{
System.out.println("Your input is invalid!");
System.exit(0);
}
}
So simple. Thanks to all who would have posted.>
JavaNoob01:
Please note that asking questions for help on this such forum is a breech of the LaTrobe University Statement of Authentication.
The Statement for Online Submission of your assignment is:
Electronic Submission of your assignment from your authenticated account implies that you are making the following statement:
"I certify that the attached material is my original work. No other person's work has been used without due acknowledgement. Except where I have clearly stated that I have used some of this material elsewhere, it has not been presented by me for examination in any other course of subject at this or any other institution. I understand that the work submitted may be reproduced and/or communicated for the purpose of detecting plagiarism.
Therefore, any help you recieve here for this assignment is plagarism. Instead next time why don't you attend the Assignment Help class tomorrow and the following ones, or ask questions in your lab time!
Also, it might be a wise idea to edit the code out of your post, if someone steals your code and uses it then you could be up for plagarism too! I was alerted to this post from someone that was having trouble with the question, therefore, if they can find it, i bet others can too!
> Please note that asking questions for help on this such forum is a breech of
> the LaTrobe University Statement of Authentication.
Not if what you posted next is the thing he is supposed to have breached (or perhaps 'breeching' is something other than I understand it to be). The statement may be intended to say "do the assignment yourself!", but as it is written I don't think it will ever hold up in court.
> Also, it might be a wise idea to edit the code out of your post,
> if someone steals your code and uses it then you could be up for plagarism
> too!
You can't plagerize yourself... if someone copies your work, they are plagerizing (not you). The fact that you posted the code on the forum will rather help you than work against you (as it gives you a clear date on which you produced the code). :)
> Not if what you posted next is the thing he is
> supposed to have breached (or perhaps 'breeching' is
> something other than I understand it to be). The
> statement may be intended to say "do the assignment
> yourself!", but as it is written I don't think it
> will ever hold up in court.
It doesn't need to hold up in court. It would need to hold up to the University Plagarism board!
> You can't plagerize yourself... if someone copies
> your work, they are plagerizing (not you). The fact
> that you posted the code on the forum will rather
> help you than work against you (as it gives you a
> clear date on which you produced the code). :)
Not sure if you goto La Trobe yourself, but there has been numerous instances where students have placed assignments (or parts of assignments) on the internet and they have been "stolen" (or in some cases given permision to use) and have been found out by the plagarism detection software. The people that plagarize and the person who handed out their assignment is penalized and put under investigation, penatly ranges from failing the unit to expulsion from the university. I'm just putting the intrest of this student first, to make sure they don't get in trouble.
> It doesn't need to hold up in court.
It does, because that's where the student will go to when he feels he is being done injustice (at least that's what happened at my university).
> and have been found out by the plagarism detection software.
The plagarism detection software would be pretty bad if it wouldn't detect it...
> The people that plagarize
Again, you cannot plagarize yourself. You might be saying that 'La Trobe' is unreasonable and thinks you can plagarize yourself. In that case we return to the 'in court business". :)
> I'm just putting the intrest of this student first, to make sure they don't
> get in trouble.
Be that as it may, you should have just said exactly that. The point is that the 'rules' do not prevent you from putting your code on the internet (at least not the 'rules' you posted). The 'rules' do not even prevent you from asking for corrections.
If you are just trying to say that you could prevent a lot of possible trouble by not posting your code on the net, fine... but that you shouldn't because it is against the rules is just wrong.
What a weird thread. I'm sure mkoryak usually has to do something between replies #1 and #2, but this one sorted itself out...
> What a weird thread. I'm sure mkoryak usually has to> do something between replies #1 and #2, but this one> sorted itself out...The stars must have been aligned or something. :)