Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: class assignment

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default class assignment

    hi i want to take in a integer from the user and use a boolean true or false to find out if each number is unique?? i know how to take in the number and convert it to a string but having trouble setting conditional to compare each character. I thought you could compare each element of the string to the other element but am not sure how to do thisThis what i have so far I am only doing java 3 weeks .
    import java.util.Scanner;

    public class Numberunique
    {
    public static void main ( String args[] )
    {

    int userInput = 0;
    boolean unique = false;
    String myString = "";



    Scanner input = new Scanner( System.in );
    System.out.println (" enter your number");
    userInput = input.nextInt();

    myString = Integer.toString(userInput);

    for ( int i = 0; i <= myString.length (); i ++)
    {
    if ( myString. charAt(0) == myString.charAt(1))

    unique = false;

    }
    System.out.println( "the number entered " + unique );
    }




    }
    Last edited by shanem; October 1st, 2012 at 05:45 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: class assignment

    Hi shanem and welcome.

    There is a sticky posted at the top of this section you should read, here is a link:
    This Forum is for Saying Hi, not for Asking Technical Questions (READ BEFORE POSTING)


    Also please see the Announcements (aka forum rules, but rules makes it sound so restrictive).


    What is the first step taught in your class? -What are you stuck on?

    What have you done so far? -Always post related code and error messages with your question.

    You said nothing about what kind of numbers to accept or reject from the user input.

    If you want to find unique numbers of a set you would have to have some way to remember what numbers you have already seen as you go. How could you remember the numbers the user has input as you go?

    When comparing your strings, what is the trouble? Is there an error? Does it compile and run but give unexpected/undesired results? Are you using == to compare? Questions like this could be answered if you included your code.

    Good luck with your assignment. If you have any more questions please try to as a well formed question about the problem you have and don't forget to include your code/error messages.




    Thread moved from member introductions.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default class assignment

    hi i want to take in a integer from the user and use a boolean true or false to find out if each number is unique?? i know how to take in the number and convert it to a string but having trouble setting conditional to compare each character. I thought you could compare each element of the string to the other element but am not sure how to do thisThis what i have so far I am only doing java 3 weeks .
    import java.util.Scanner;

    public class Numberunique
    {
    public static void main ( String args[] )
    {

    int userInput = 0;
    boolean unique = false;
    String myString = "";



    Scanner input = new Scanner( System.in );
    System.out.println (" enter your number");
    userInput = input.nextInt();

    myString = Integer.toString(userInput);

    for ( int i = 0; i <= myString.length (); i ++)
    {
    if ( myString. charAt(0) == myString.charAt(1))

    unique = false;

    }
    System.out.println( "the number entered " + unique );
    }




    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: class assignment

    take in a integer from the user and use a boolean true or false to find out if each number is unique
    Let me rephrase that to see if I understand: You want to write a method that takes a String of numeric digits and checks that the digits are unique. It will return true if they are and false if not.

    If you were going to do this manually only looking at two digits at a time how would you do it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: class assignment

    yes , i want to do this using a nested loop

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: class assignment

    Why decide what technique to use before you have determined the steps the code needs to take to solve the problem? Can you list the steps you would use to solve the problem if you were to do it manually with a row of coins that you wanted to determine if they were all unique?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: class assignment

    i figured it out earlier i just need a nested for loop but thanks for all the help NOT

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: class assignment

    i figured it out earlier
    Glad you've solved your problem.

    Please mark this thread as solved
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with class assignment !
    By Ryoshiro in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2012, 09:35 PM
  2. Instrument Class Assignment Help
    By kinney.j in forum Object Oriented Programming
    Replies: 1
    Last Post: November 2nd, 2011, 07:27 AM
  3. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  4. 1st Java class assignment, and having some difficulties. Little help please.
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 27th, 2011, 04:09 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM