Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Trouble getting past the loop.

Thread view: 
mmoski - 03 Feb 2007 00:41 GMT
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 - 03 Feb 2007 00:50 GMT
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);
    }

}
Mike Schilling - 03 Feb 2007 02:44 GMT
> 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
[quoted text clipped - 5 lines]
> 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.
mmoski - 03 Feb 2007 02:49 GMT
On Feb 2, 8:44 pm, "Mike Schilling" <mscottschill...@hotmail.com>
wrote:

> > 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
[quoted text clipped - 7 lines]
>
> 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?
Mike Schilling - 03 Feb 2007 03:40 GMT
> On Feb 2, 8:44 pm, "Mike Schilling" <mscottschill...@hotmail.com>
> wrote:
[quoted text clipped - 17 lines]
> 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.
Luc The Perverse - 03 Feb 2007 04:30 GMT
> 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
[quoted text clipped - 5 lines]
> 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" <mmoski@gmail.com> 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

:)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.