Trouble getting past the loop.

Discussion in 'Java' started by mmoski, Feb 3, 2007.

  1. mmoski

    mmoski Guest

    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?
     
    mmoski, Feb 3, 2007
    #1
    1. Advertising

  2. mmoski

    mmoski Guest

    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);
    }

    }
     
    mmoski, Feb 3, 2007
    #2
    1. Advertising

  3. "mmoski" <> wrote in message
    news:...
    > 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.
     
    Mike Schilling, Feb 3, 2007
    #3
  4. mmoski

    mmoski Guest

    On Feb 2, 8:44 pm, "Mike Schilling" <>
    wrote:
    > "mmoski" <> wrote in message
    >
    > news:...
    >
    > > 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.


    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?
     
    mmoski, Feb 3, 2007
    #4
  5. "mmoski" <> wrote in message
    news:...
    > On Feb 2, 8:44 pm, "Mike Schilling" <>
    > wrote:
    >> "mmoski" <> wrote in message
    >>
    >> news:...
    >>
    >> > 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.

    >
    > 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.
     
    Mike Schilling, Feb 3, 2007
    #5
  6. "mmoski" <> wrote in message
    news:...
    > 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.

    --
    LTP

    :)
     
    Luc The Perverse, Feb 3, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Sheen
    Replies:
    13
    Views:
    465
    Hermit Dave
    Jan 25, 2004
  2. Jim Fisher
    Replies:
    0
    Views:
    401
    Jim Fisher
    Oct 22, 2004
  3. Byte
    Replies:
    4
    Views:
    421
  4. Dick Moores

    Trouble getting loop to break

    Dick Moores, Nov 20, 2007, in forum: Python
    Replies:
    11
    Views:
    506
    Fredrik Johansson
    Nov 25, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    405
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page