Trouble getting past the loop.

M

mmoski

I'm writing a program that is supposed to read in any number of hex
values (3c 4a 5b 45 ect), convert them to binary (in this case
111100100101010110111000101). The program then counts the number of
ones in the sequence. Also, it is to figure out the longest sequence
of 0's and of 1's seperately. I have managed to make it as far as to
count all the ones, but the System.out.println(totalOnes); only works
if it's in one of the while loops! I am only supposed to output this
number once. But even test strings (System.out.println("Works");)
don't run outside of those loops. What am I doing wrong?
 
M

mmoski

Oh, and here's my code:

import java.util.Scanner;
public class Test {



public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
String input = sc.next();

Boolean isItOne = true;
int longestZero = 0;
int longestOne = 0;
int totalOnes = 0;

while(input != null){

int inputHex = Integer.parseInt (input, 16);
String usBinary = Integer.toBinaryString(inputHex);
int stopCase = usBinary.length();


while(stopCase > 0){

String currentChar = usBinary.substring(0, stopCase);
String test = currentChar.substring((stopCase - 1), stopCase);
int testAsInt = Integer.parseInt(test);

if(testAsInt == 1){
totalOnes ++;
}

stopCase--;
System.out.println(test);

}

input = sc.next();

}

System.out.println(totalOnes);
}

}
 
M

Mike Schilling

mmoski said:
I'm writing a program that is supposed to read in any number of hex
values (3c 4a 5b 45 ect), convert them to binary (in this case
111100100101010110111000101). The program then counts the number of
ones in the sequence. Also, it is to figure out the longest sequence
of 0's and of 1's seperately. I have managed to make it as far as to
count all the ones, but the System.out.println(totalOnes); only works
if it's in one of the while loops! I am only supposed to output this
number once. But even test strings (System.out.println("Works");)
don't run outside of those loops. What am I doing wrong?

If you don't do your own homework, you'll never learn anything.
 
M

mmoski

If you don't do your own homework, you'll never learn anything.

Dude, that's messed up. This isn't a java course. I'm not asking you
for methods or how to count 1's or 0's or anything. This is a systems
course, I've chosen to use java to complete this assignment. Thanks
for your help by the way. I'm sure in the time it took you to type
that crap you could have just told me where/how to run that
statement. Thanks though. Should I still kiss your ring on my way
out?
 
M

Mike Schilling

mmoski said:
Dude, that's messed up. This isn't a java course. I'm not asking you
for methods or how to count 1's or 0's or anything. This is a systems
course, I've chosen to use java to complete this assignment. Thanks
for your help by the way. I'm sure in the time it took you to type
that crap you could have just told me where/how to run that
statement.

In other words, you don't care whether you learn anything.
 
L

Luc The Perverse

mmoski said:
I'm writing a program that is supposed to read in any number of hex
values (3c 4a 5b 45 ect), convert them to binary (in this case
111100100101010110111000101). The program then counts the number of
ones in the sequence. Also, it is to figure out the longest sequence
of 0's and of 1's seperately. I have managed to make it as far as to
count all the ones, but the System.out.println(totalOnes); only works
if it's in one of the while loops! I am only supposed to output this
number once. But even test strings (System.out.println("Works");)
don't run outside of those loops. What am I doing wrong?

Hint #1: If something doesn't execute unless it's in a loop then it might
be reasonable to assume that the loop never exits ;)

Hint #2: You might want to investigate how System.in works.

Suggestion #1: Think about how you want your input to end and develop a
strategy therefrom.

Suggestion #2: I could not follow your code because the indentation was out
of control. This may be an error in cut and paste - but definitely try to
apply some organization if this is not the case.

later on "mmoski said:
Dude, that's messed up. This isn't a java course. I'm not asking you
for methods or how to count 1's or 0's or anything. This is a systems
course, I've chosen to use java to complete this assignment. Thanks
for your help by the way. I'm sure in the time it took you to type
that crap you could have just told me where/how to run that
statement.

Suggestion #3: Calm down. It's going to be ok. I think your question was
reasonable - but we are inundated with people who want us to do their
assignments for them - it puts everyone on the edge. And it was obvious to
everyone that it was an assignment - regardless of any unique situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top