Jump to content

Struggling with java homework...any help?

Go to solution Solved by Ghost,

Here is some code that does what you want. I left the comments out so you actually have to work out how it works. :)

import java.util.Scanner;public class Binary {	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		String input = sc.nextLine();		int oneCounter = 0;		boolean isBinary = true;		for(char c: input.toCharArray()){			switch(c){			case '1':				oneCounter++;			case '0':				break;			default:                                isBinary = false;				break;			}			if(!isBinary)break;		}		if(isBinary){			System.out.format("The number is binary and has %d 1s\n", oneCounter);		} else{			System.out.println("The number is not binary");		}		sc.close();	}}

Hi everybody,

 

I'm struggling with this homework problem.  I have to create a java program that lets the user input a binary number, have the program verify that it is truly a binary number, and then have the program count how many 1's are in the number.

 

So far I am thinking I need to declare each number in the binary as a separate variable?  Then have the program say hey, this number is good, this number is good, this isn't good, etc, returning it to the beginning if it doesn't pass to re-enter a new binary number?

 

I am pretty much a complete beginner, and am having trouble with what the syntax would look like as well.

 

Any help would be really appreciated!

 

PS: I understand that this involves loops, which is part of the trouble I am having getting down.  

 

Here's what I have so far...

import java.util.Scanner;import java.text.DecimalFormat;public class BinaryWords {public static void main( String [] args ){int binaryNumber1;int binaryNumber2;int binaryNumber3;int binaryNumber4;int binaryNumber5;int binaryNumber6;int binaryNumber7;int binaryNumber8;int binaryNumber9;int binaryNumber10;int binaryNumberWhole = binaryNumber1 + binaryNumber2 + binaryNumber3+ binaryNumber4 + binaryNumber5 + binaryNumber6 + binaryNumber7+ binaryNumber8 + binaryNumber9 + binaryNumber10;if 1<binaryNumber1<0then final int SENTINEL = binaryNumber1;System.out.println( "This program verifies a binary number"+ "representing a word and then" + "counts how many 1's are in that number");System.out.println("Please enter a binary number representing a word");

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
https://linustechtips.com/topic/315555-struggling-with-java-homeworkany-help/
Share on other sites

Link to post
Share on other sites

Java homework?

 

Do schools really teach Java?

But oh man, this is so easy. Just think about it.

 

First of all, the user inputs 1 integer. You need to get each digit of that integer and, if it's different than 1 or 0, like 2, type the error message. In the same time, you need a counter to count the amount of "1" digits there are.

 

PS : 15 % 10 = 5. ( the rest of 15/10 is 5)

That's if both values are integers.

 

Also, you need a scanner to read user input.

 

//first of all, make sure you include java.util.Scannerint a, digit;int OK=1; /* This flags whether the number is a real binary number or not */int j=0; /* our counter for the 1-s */Scanner in = new Scanner(System.in);a = in.nextLine();//This stores the user input in an integer//Now we go ahead and find each digit of the number and also count 0-s and see if it's not binarywhile(a!=0){digit = a % 10;a/=10;if(digit != 0 && digit != 1) { break; OK = 0;if(digit == 1) j++;}if(OK) System.out.println(j);else System.out.println(<error message>); 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Java homework?

 

Do schools really teach Java?

But oh man, this is so easy. Just think about it.

 

First of all, the user inputs 1 integer. You need to get each digit of that integer and, if it's different than 1 or 0, like 2, type the error message. In the same time, you need a counter to count the amount of "1" digits there are.

 

PS : 15 % 10 = 5. ( the rest of 15/10 is 5)

That's if both values are integers.

 

 

But if the number is something like 100011010111, then I would just have the user input one of those numbers at a time?

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to post
Share on other sites

Here is some code that does what you want. I left the comments out so you actually have to work out how it works. :)

import java.util.Scanner;public class Binary {	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		String input = sc.nextLine();		int oneCounter = 0;		boolean isBinary = true;		for(char c: input.toCharArray()){			switch(c){			case '1':				oneCounter++;			case '0':				break;			default:                                isBinary = false;				break;			}			if(!isBinary)break;		}		if(isBinary){			System.out.format("The number is binary and has %d 1s\n", oneCounter);		} else{			System.out.println("The number is not binary");		}		sc.close();	}}

Feel free to PM for any water-cooling questions. Check out my profile for more ways to contact me.

 

Add me to your circles on Google+ here or you can follow me on twitter @deadfire19.

Link to post
Share on other sites

Are you saying the user has to individually enter each digit of the number?

I guess you could if you wanted to, but the assignment doesn't say I that I need to. 

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to post
Share on other sites

I guess you could if you wanted to, but the assignment doesn't say I that I need to. 

It says the user inputs one number.

 

It is a single number, not multiple numbers.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

But if the number is something like 100011010111, then I would just have the user input one of those numbers at a time?

No.

The user inputs 100011010111 , not 1 0 0 0 1 1 0 1 0 1 1 1 .

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Well if that isn't required than don't bother doing it. Ghost's example is what it should look like.

 

 

 

Here is some code that does what you want. I left the comments out so you actually have to work out how it works. :)

import java.util.Scanner;public class Binary {	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		String input = sc.nextLine();		int oneCounter = 0;		boolean isBinary = true;		for(char c: input.toCharArray()){			switch©{			case '1':				oneCounter++;			case '0':				break;			default:                                isBinary = false;				break;			}			if(!isBinary)break;		}		if(isBinary){			System.out.format("The number is binary and has %d 1s\n", oneCounter);		} else{			System.out.println("The number is not binary");		}		sc.close();	}}

Thank you to you both - I am going to look this over and try to learn/figure out what I can. 

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to post
Share on other sites

 

Hi everybody,

 

I'm struggling with this homework problem.  I have to create a java program that lets the user input a binary number, have the program verify that it is truly a binary number, and then have the program count how many 1's are in the number.

 

So far I am thinking I need to declare each number in the binary as a separate variable?  Then have the program say hey, this number is good, this number is good, this isn't good, etc, returning it to the beginning if it doesn't pass to re-enter a new binary number?

 

I am pretty much a complete beginner, and am having trouble with what the syntax would look like as well.

 

Any help would be really appreciated!

 

PS: I understand that this involves loops, which is part of the trouble I am having getting down.  

 

Here's what I have so far...

import java.util.Scanner;import java.text.DecimalFormat;public class BinaryWords {public static void main( String [] args ){int binaryNumber1;int binaryNumber2;int binaryNumber3;int binaryNumber4;int binaryNumber5;int binaryNumber6;int binaryNumber7;int binaryNumber8;int binaryNumber9;int binaryNumber10;int binaryNumberWhole = binaryNumber1 + binaryNumber2 + binaryNumber3+ binaryNumber4 + binaryNumber5 + binaryNumber6 + binaryNumber7+ binaryNumber8 + binaryNumber9 + binaryNumber10;if 1<binaryNumber1<0then final int SENTINEL = binaryNumber1;System.out.println( "This program verifies a binary number"+ "representing a word and then" + "counts how many 1's are in that number");System.out.println("Please enter a binary number representing a word");

Without doing the rest of the program as you need to learn this to xD your problem would be here as you are adding numbers 1+1 = 2 not 11...

 

int binaryNumberWhole = binaryNumber1 + binaryNumber2 + binaryNumber3

+ binaryNumber4 + binaryNumber5 + binaryNumber6 + binaryNumber7

+ binaryNumber8 + binaryNumber9 + binaryNumber10;

Needs Update

Link to post
Share on other sites

Without doing the rest of the program as you need to learn this to xD your problem would be here as you are adding numbers 1+1 = 2 not 11...

 

int binaryNumberWhole = binaryNumber1 + binaryNumber2 + binaryNumber3

+ binaryNumber4 + binaryNumber5 + binaryNumber6 + binaryNumber7

+ binaryNumber8 + binaryNumber9 + binaryNumber10;

 

Yeah, I was making quite a mess :P

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to post
Share on other sites

Yeah, I was making quite a mess :P

well you can use the method to count the amount of ones .. as if you look at 54 again witch is 110110 there are 4 one's .... so if you do count them all 1 + 1 + 0 + 1 + 1 + 0 = 4 so you can count the amount of once like that.( i miss read and though you needed to convert the binary number to a int number xD that is a bit more tricky then just checking if it is a binary number)  

Needs Update

Link to post
Share on other sites

by the looks of your program you have not done much in the your course (so you might not have done arrays yet) 

 

>First you need to get the input of the binary number.

 

>This is the part that i don't know how you would do it in a early stage because if you input a number like 110110 it would be seen as a single number and would be hard to split up into 6 number without using arrays of char.

 

>if you have not done that you might need to input them each on there own.(would be easy with arrays but it is something that you might not know of )

 

>if you have each binary number on it own just do a While loop 

 

int i = 0;

while(i < 9)

{

//code

 

i++;

}

 

with a if statement inside it to check if the number is a 0 or a 1 and if that is true then it is a binary number if not then is is not.

 

if(binary number == 1 || binary number == 0)

{

         True

}

else

{

        false

}

 

hope this helps.. as someone who was learning this myself it is better to try and try again then to copy someone's that might even have stuff in that you have not done as this is a basic stuff that you would later need to use to work and if you don't get this then the work to come will be why harder.

Needs Update

Link to post
Share on other sites

 

import java.util.Scanner;

 

public class test {

 

   public static void main(String args[]) {

 

      Scanner scan = new Scanner(System.in);

      String num;

      System.out.println("Enter a binary number:"); 

      num = scan.nextLine();

 

      if (isBinary(num) == false) {

         System.out.println("Not a binary number.");

      }

      else {

         System.out.println("it is a binary number");

         System.out.println("The binary number is " + Integer.parseInt(num, 2));

      }


 

public static boolean isBinary(String number) {

   int copy= Integer.parseInt(number);

   while (copy!= 0) {

      if (copy% 10 > 1) {

         return false;

      }

      copy= copyOfInput / 10;

   }

  return true;

  }

}

 

This might work, I hope I understand what you meant.

I hardly tested it, with a few digits. It seems to work, but I have no error handling or any catching of any errors.

Also not sure how much that parseInt can parse

 

Edit: Sorry, I didn't count how many 1's there are.

but that shouldn't be too hard. Just use the method to divide by 2 constantly

Link to post
Share on other sites

Phew, thanks everyone for the comments!  Just going over my head a little bit because basically I am a complete beginner with java, so I appreciate the help. 

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×