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 2006

Tip: Looking for answers? Try searching our database.

Java Sanner

Thread view: 
Cogito - 15 Feb 2006 13:53 GMT
Hi guys,

I'm nearing the end of my java homework this week but the last section
has got me.
I realise to most that this will seem simple but I've haven't must
experience, so please help.

I have to read in a string of 1's and 0's  using the java scanner and
convert the string into a boolean array. The array is 6 long.

Any help please, been on this for a few hours with no luck so I'm
starting from scratch again.
Rhino - 15 Feb 2006 14:05 GMT
> Hi guys,
>
[quoted text clipped - 8 lines]
> Any help please, been on this for a few hours with no luck so I'm
> starting from scratch again.

You'd have a lot better chance of getting help if you posted your best
attempt at getting the code to work and asked a specific question. Very few
people here are likely to do your assignment for you with only a verbal
description to go on.

Also, have you considered a Google Groups search on the comp.lang.java.*
newsgroups searching on "Scanner byte array"? It's very possible that you
will find several examples showing something very close to what you want to
do without having to wait for replies to your question.

--
Rhino
tom fredriksen - 15 Feb 2006 14:14 GMT
> Hi guys,
>
[quoted text clipped - 5 lines]
> I have to read in a string of 1's and 0's  using the java scanner and
> convert the string into a boolean array. The array is 6 long.

Could you be a bot more detailed, please, the description you are giving
is not very helpful.

/tom
Roedy Green - 15 Feb 2006 14:27 GMT
>I have to read in a string of 1's and 0's  using the java scanner and
>convert the string into a boolean array. The array is 6 long.

Partition the problem.

1. see if you can figure out how to get the scanner to read anything.

2. presuming you had a nice string magically already  in memory
"010101011" how would you convert it to binary.

There are two approaches. from String -> char -> boolean -> boolean
array or from String ->long -> boolean array.

See http://mindprod.com/jgloss/conversion.html
http://mindprod.com/jgloss/binary.html

The first will probably is probably what you prof had in mind.

The key is always to do the easiest part of the problem first.

See http://mindprod.com/jgloss/tackling.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Cogito - 15 Feb 2006 18:45 GMT
I'm reading in details from a file using java scanner ;

Here is my code so far;

Scanner in = new Scanner(new File("p.txt"));

                      while (in.hasNext())
                      {

                              forename = in.next();
                              surname  = in.next();
                              city     = in.next();
                              telephone= in.next();

                              gender   = in.next().charAt(0);
                              age      = in.nextInt();
                              //intrests = in.next().chatAt
                              //create boolean array
                              //loop convert string

                              p = new Person(forename, surname, city,
telephone, gender, age);
                              parray[i] = p;

                              i++;

this is all fine, the  // indicate where i am stuck.
i can create the boolean array but don't know how to convert the string
(001011) which im reading in from the file, in to the array.

Any suggestions?

Many thanks.
Oliver Wong - 15 Feb 2006 18:58 GMT
> i can create the boolean array but don't know how to convert the string
> (001011) which im reading in from the file, in to the array.

   Assuming the array is the same size as the string, you could use a
for-loop to iterate over the string, looking at the characters one by one,
and setting the appropriate value in the array.

   - Oliver
IchBin - 15 Feb 2006 20:34 GMT
> I'm reading in details from a file using java scanner ;
>
> Here is my code so far;

[snip code]

Try this: (need to put the correct path to "p.txt".

public class Test
{
    public static void main(String[] args)
    {
        String file = "p.txt";
        String forename;
        String surname;
        String city;
        String telephone;
        char   gender;
        int    age;
        int    intrests;
        int    i = 0;

        try {
            Scanner in = new Scanner(new File(file));
            while (in.hasNext())
            {
                forename = in.next();
                surname = in.next();
                city = in.next();
                telephone= in.next();

                gender = in.next().charAt(0);
                age = in.nextInt();
                intrests = Integer.parseInt(in.next());
                String holdit = in.next();

                boolean[] boolArray = new boolean[6];
                for (i=0; i < 6; i++) {
                    boolArray [i] = holdit.charAt(i) != '0';
                }
                Person person = new Person(forename, surname, city,
telephone, gender, age, intrests, boolArray);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
class Person {
    String forename;
    String surname;
    String city;
    String telephone;

    char gender;
    int age;
    int intrests;
    boolean [] boolArray;

    Person ( String forename,  String  surname,  String  city,  String
 telephone, char gender, int age, int intrests, boolean[] boolAraray )
   {
        this.forename = forename;
        this.surname = surname;
        this.city = city;
        this.telephone = telephone;
        this.gender = gender;
        this.age = age;
        this.intrests = intrests;
        this.boolArray = boolArray;
    }
}

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

IchBin - 15 Feb 2006 21:04 GMT
>> I'm reading in details from a file using java scanner ;
>>
[quoted text clipped - 3 lines]
>
> Try this: (need to put the correct path to "p.txt".

[snip code]
>                 intrests = Integer.parseInt(in.next());
>                 String holdit = in.next();
[quoted text clipped - 3 lines]
>                     boolArray [i] = holdit.charAt(i) != '0';
>                 }
[snip code]

if you are not sure of the size of the input field just do this:

                intrests = in.nextInt();
                String holdit = in.next();
                int tokenLength = holdit.length();

                boolean[] boolArray = new boolean[tokenLength ];
                for (i=0; i < tokenLength ; i++) {
                    boolArray [i] = holdit.charAt(i) != '0';
                }

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

thefatson@gmail.com - 17 Feb 2006 05:21 GMT
Very strange Im doing the same project right now I can do all the suff
you have mentioned so far but something weird is happenning.
I can read the string to a boolean array but the output was messed up
in the end.
I put in println s to find the problem and discovered that even though
the right boolean values were being read from the string when I tried
to print them from the object holding the array each one contained
"false false false true true true".

Here is the code :
public static void main (String [] args)
{
    Client c;
    Client[] clients = new Client[20];
    File people;
    Scanner in;
    int i = 0;
    Boolean[] bools = new Boolean[6];

    people = new File("client.txt");

    try
    {
        in = new Scanner(people);
        while (in.hasNext())
        {
            String name = in.next();
            String sname = in.next();
            String local = in.next();
            String telo = in.next();
            String sex = in.next();
            String age = in.next();
            String like = in.next();

            Character s = sex.charAt(0);

            for(int j = 0; j<6; j++)
            {
                if (like.charAt(j) == '1')
                {
                    bools[j] = new Boolean(true);
                    System.out.println("111");
                    System.out.println(Boolean.toString(bools[j]));
                }
                if(like.charAt(j) == '0')
                {
                    bools[j] = new Boolean(false);
                    System.out.println("222");
                    System.out.println(Boolean.toString(bools[j]));
                }
            }

            c = new Client(name, sname, local, telo, s, Integer.parseInt(age),
bools);
            clients[i] = c;
            i++;
        }
        in.close();

        for(int ii = 0;ii<clients.length;ii++)
        {
            System.out.println(clients[ii].getSurname() + " " +
Boolean.toString(clients[ii].getInterests()[0])+ " " +
Boolean.toString(clients[ii].getInterests()[1])+ " " +
Boolean.toString(clients[ii].getInterests()[2])+ " " +
Boolean.toString(clients[ii].getInterests()[3])+ " " +
Boolean.toString(clients[ii].getInterests()[4])+ " " +
Boolean.toString(clients[ii].getInterests()[5]));
        }

The printlns used when converting to the boolean array give the right
output but the long line of printlns at the end just give "false false
false true true true" for each client object.
The constructor seems fine, this is driving me insane its like 2+2
suddenly equals 5.

I realise the Boolean is slightly different from boolean but I have
been messing around with things like that to find a solution.
thefatson@gmail.com - 17 Feb 2006 10:20 GMT
I sorted it out, I just declared the boolean array within the while
loop and it worked. It must have been using the same array for each
object before I did that. I also changed all my Booleans to booleans
but that is inconsequential.
Oliver Wong - 15 Feb 2006 14:32 GMT
> Hi guys,
>
[quoted text clipped - 8 lines]
> Any help please, been on this for a few hours with no luck so I'm
> starting from scratch again.

   Did you mean the you have an array of boolean of size 6, or did you mean
you have an array of long of size 6? E.g. is it "boolean[6]" or "long[6]" or
something else?

   Did you read these documents?

http://home.earthlink.net/~patricia_shanahan/beginner.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

   Do you have a specific question?

   - Oliver


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



©2009 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.