I am trying to get this project done for my CS class but I don't know if I am anywhere in the ball park. THis is what I have so far. I need to have a toDecimal, toBinary, and isBinary, that return true for a legal binary number and false other wise. Could someone take a look at this and see if they could help?
PHP Code:
import java.io.*;
public class BinConverter
{
public static void main(String[] args)
{
System.out.println("Welcome to your third java program!!");
//next 2 lines are the setup for reading stuff in from the user
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(input);
String line = "s";
if (isBinary (line))
{
int num= toDecimal(s);
System.out.println(num);
String answer=toBinary (num);
System.out.println (answer);
}
else
{
System.out.println("Not Binary");
}
public static boolean isBinary (String s)
{
int l = s.length();
int check = 1;
for (int i = 0; i < l; i++)
{
if (s.charAt(i) == '1' || s.charAt(i) == '0')
{
else
} check = 0;
}
if (check == 0)
return false;
else
return true;
}
public static double toDecimal (String s)
{
int l = s.length();
double result = 0;
System.out.println("s=" + s);
System.out.println("l=" + l);
for (int i = 0; i < l; i++)
{
System.out.println("result before iteration=" + result);
System.out.println("s.charAt(i)=" + (double)s.charAt(i));
System.out.println("s.length() - i - 1=" + (s.length() - i - 1));
System.out.println("Math.pow(2, (s.length() - i - 1))=" + Math.pow(2, (s.length() - i - 1)));
System.out.println("adding " + (s.charAt(i) * Math.pow(2, (s.length() - i - 1))) + " to result");
result = result + s.charAt(i) * Math.pow(2, (s.length() - i - 1));
System.out.println("result after iteration=" + result);
}
System.out.println("final result=" + result);
return result;
}
public static String toBinary (int n)
{
if (n % 2==0)
return 0;
n--;
}
else
{
return 1;
n--;
n % n;
}
}
}
I have two errors: 25 : illegal start of expression static boolean isBinary (String s)
78 : ';' expected }
Personally i wouldn't give a variable a value of 0 if it was false. I'd just use a bool and say that once it has reached a character which isn't a 0 or a 1 then change the boolean value to false so it would return false. That should cut down the program quite a lot.
I'm not sure but i think you may have to add another modifier to your isBinary method, public static instead of just static. Not sure on that one though.
Code:
public static boolean isBinary(String s)
{
boolean check = true;
for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) != '1' && s.charAt(i) != '0')
{
check = false;
}
}
return check;
}
I haven't got a clue if that's right, i'm first year CS as well but that's just an idea as it cuts down the code.
EDIT: It's just a way to cut down your code, if the if statement fails each time then it won't change the value of the variable check to false meaning that the value IS binary. Flame me if you will, it was just an idea
EDIT EDIT: Also, you shouldn't have to ask a forum how to do your homework. I do all my java programming for my CS course all by myself, i haven't asked one question about it on these forums yet, i class it as cheating, if you turn up to lectures and go to tutorials then you should know everything you need to know to complete the coursework. Also, the java API is a seriously handy resource, just a little imagination is needed (and read your java books!)
EDIT EDIT: Also, you shouldn't have to ask a forum how to do your homework. I do all my java programming for my CS course all by myself, i haven't asked one question about it on these forums yet, i class it as cheating, if you turn up to lectures and go to tutorials then you should know everything you need to know to complete the coursework. Also, the java API is a seriously handy resource, just a little imagination is needed (and read your java books!)
Well, there's a difference: The unwritten rule is: We don't WRITE code for homework assignments. However, this is quite another case. He has evidently tried to make it work, he gets an error, and wants to find out what he's doing wrong. Telling him what he should think of and what mistakes he's making will help him become a better programmer. It's those that expect us to write the program for them that we don't help. Those that are really trying, those we do help.
I deleted the num varible at the top because I don't think I need it For the toBinary statement. The while loop at the bottom goes to the string toBinary statement as well. Do I need to change the variable 's' from a 2 of the statments. And I think I called the 'S' statment at the top, I am probably wrong though.
I'm thinking maybe your s variable in main should be line, and you just put s because that is what you set line to. You are trying to send s to the isBinary method, but there is no s to send.
I don't see where the while loop at the bottom does anything.
I updated the file. How do I get rid of that illegal start? ANd a tthe buttom of the page, it says that I am missing a semi-colon. How do I check for that?
I think your original 2 errors had to do with the fact that your main method was bracketed correctly, which is what the illegal start is. You can't call the isBinary method if it's not outside the main method. Your main method needs to end.
import java.io.*;
public class BinConverter
{
public static void main(String[] args)
{
System.out.println("Welcome to your third java program!!");
//next 2 lines are the setup for reading stuff in from the user
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(input);
String line = "s";
if (isBinary (line))
{
int num= toDecimal(s);
System.out.println(num);
String answer=toBinary (num);
System.out.println (answer);
}
else
{
System.out.println("Not Binary");
}
}
public static boolean isBinary (String s)
{
int l = s.length();
int check = 1;
for (int i = 0; i < l; i++)
{
if (s.charAt(i) == '1' || s.charAt(i) == '0')
{}
else
check = 0;
}
if (check == 0)
return false;
else
return true;
}
public static double toDecimal (String s)
{
int l = s.length();
double result = 0;
System.out.println("s=" + s);
System.out.println("l=" + l);
for (int i = 0; i < l; i++)
{
System.out.println("result before iteration=" + result);
System.out.println("s.charAt(i)=" + (double)s.charAt(i));
System.out.println("s.length() - i - 1=" + (s.length() - i - 1));
System.out.println("Math.pow(2, (s.length() - i - 1))=" + Math.pow(2, (s.length() - i - 1)));
System.out.println("adding " + (s.charAt(i) * Math.pow(2, (s.length() - i - 1))) + " to result");
result = result + s.charAt(i) * Math.pow(2, (s.length() - i - 1));
System.out.println("result after iteration=" + result);
}
System.out.println("final result=" + result);
return result;
}
public static String toBinary (int n)
{
if (s % 2==0)
return 0;
s--;
}
else
{
return 1;
s--;
s % 2;
}
}
With these two errors
C:\Program Files\Xinox Software\JCreator LE\MyProjects\dd\BinConverter.java:71: illegal start of type
else
^
C:\Program Files\Xinox Software\JCreator LE\MyProjects\dd\BinConverter.java:76: <identifier> expected
}