Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Question Simple program that checks if a number has same digits as its triple?


    Hello, I'm trying to make a program that checks if a number has the same digits as its triple, so 16374, if its triple were 36471 would return true. The two numbers are put into a array and this is code to check it:
    Code:
    public static boolean check (int[]number, int[] third, int digits)
    	{
    		int matches=0;
    		int i=0;
    		int j=0;
    		while (i<digits-1)
    		{
    			while (number[i]!=third[j]&&j<digits-1)
    				j++;
    			i++;
    		if (j<digits)
    			matches++;
    		} 
    		if (matches==digits)
    			return true;
    		return false;
    	}
    I've looked over it many times and tried for loops as well, but it seems that matches never ++ and always returns true if I set (matches==0).
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Do you have code for testing the above: sets the arrays etc and calls the method and displays the results?

    Please edit the post and wrap the code in code tags:
    [code]
    >>>> THE CODE HERE
    [/code]
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Smile


    Originally Posted by NormR
    Do you have code for testing the above: sets the arrays etc and calls the method and displays the results?

    Please edit the post and wrap the code in code tags:
    [code]
    >>>> THE CODE HERE
    [/code]
    Yes I have, and they work
    Sorry, total noob, first post
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    they work
    Does that mean you have solved your problem?

    If the problem is not solved, please post the code for testing the method to show the problem.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Red face


    Originally Posted by NormR
    Does that mean you have solved your problem?

    If the problem is not solved, please post the code for testing the method to show the problem.
    I meant the other methods work here's the full code
    Code:
    public class Triples 
    {
    	public static void main(String[] args) 
    	{
    		Scanner keyboard = new Scanner(System.in);
    		int number=0;
    		System.out.print("Please enter digits: ");
    		int digits = keyboard.nextInt();		
    		for (int add=3;add<3*(int)Math.pow(10, digits);add*=10)
    		{
    			number+=add;
    		}
    		for (int i=number;i<Math.pow(10, digits);i+=3)
    		{
    			int third=number/3;
    			System.out.println (check(toArray(number,digits), toArray(third,digits), digits));
    		}
    		keyboard.close();
    	}
    	public static int[] toArray(int number, int digits) 
    	{
    		int[] array = new int[digits];
    		for (int i = 0; i < digits; i++)
    		{
    			array[i] = number % 10;
    			number /= 10;
    		}
    		return array;
    	}
    	public static boolean check (int[]number, int[] third, int digits)
    	{
    		int matches=0;
    		int i=0;
    		int j=0;
    		while (i<digits)
    		{
    			while (number[i]!=third[j]&&j<digits)
    				j++;
    			i++;
    		if (j<digits-1)
    			matches++;
    		} 
    		if (matches==digits)
    			return true;
    		return false;
    	}
    }
    The test is when if you change the last "digits" to 0, it returns true, meaning matches was never touched...which I don't understand.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    What input does it require to execute and show the problem?
    When I give it 12345 it goes into an infinite loop.

    Can you post the console for when you execute the program
    that shows the input and the program's output.
    Last edited by NormR; September 21st, 2013 at 06:04 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    Oh, a digit from 1 to 9
    So 4 would generate 3333 to 3999
    Then it checks if that number and it divided by 3 includes the same digits
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    When I give it a 3 it gives me an exception.
    What errors do you get when you execute it?

    Can you post the console for when you execute the program
    that shows the input and the program's output.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Angry


    That's so weird, no matter what you enter you get an error and 24 and 40
    Wasn't like this before...hold on, I'll look into it
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Smile


    So that's what I changed...from a while loop to a for, here it is
    Code:
    import java.util.Scanner;
    /**
     * The Triples class Determines how many X digit numbers have the same digits
     * when tripled
     * 
     * @author Jason He
     * @version September 15, 2013
     */
    public class Triples 
    {
    	public static void main(String[] args) 
    	{
    		Scanner keyboard = new Scanner(System.in);
    		int number=0;
    		System.out.print("Please enter digits: ");
    		int digits = keyboard.nextInt();		
    		for (int add=3;add<3*(int)Math.pow(10, digits);add*=10)
    		{
    			number+=add;
    		}
    		for (int i=number;i<Math.pow(10, digits);i+=3)
    		{
    			int third=number/3;
    			System.out.println (check(toArray(number,digits), toArray(third,digits), digits));
    		}
    		keyboard.close();
    	}
    	public static int[] toArray(int number, int digits) 
    	{
    		int[] array = new int[digits];
    		for (int i = 0; i < digits; i++)
    		{
    			array[i] = number % 10;
    			number /= 10;
    		}
    		return array;
    	}
    	public static boolean check (int[]number, int[] third, int digits)
    	{
    		int matches=0;
    		for (int i=0;i<number.length;i++)
    			for (int j=0;j<number.length;j++)
    				if (number[i]==third[j])
    				{
    					matches++;
    					third[j]=-1;
    				}
    		if (matches==digits)
    			return true;
    		return false;
    	}
    }
    If I run it like this, it keeps returning false
    Changing the last "digits" to 0 returns all true, so as I said, matches is never touched.
    Bonus objective: Find out why while loop doesn't work...
    Thanks for your efforts, man!
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    it keeps returning false
    Can you post a sample set of arrays that the check() method should return true for?
    And another set that should return cause check() to return false.
    And explain why.


    Also try debugging the code by adding some calls to the println method to print out the data being passed to the methods.
    For arrays:
    Code:
    System.out.println("an ID "+  java.util.Arrays.toString(theArrayName));
    Last edited by NormR; September 21st, 2013 at 08:57 PM.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Unhappy


    Thanks to that toString command I didn't know of, I found my mistake, I used number when I should've use i in the second for loop...but that didn't fix my problem...I have no idea now
    Code:
    public class Triples 
    {
    	public static void main(String[] args) 
    	{
    		Scanner keyboard = new Scanner(System.in);
    		int low=0;
    		System.out.print("Please enter digits: ");
    		int digits = keyboard.nextInt();		
    		for (int add=3;add<3*(int)Math.pow(10, digits);add*=10)
    		{
    			low+=add;
    		}
    		for (int number=low;number<Math.pow(10, digits);number+=3)
    		{
    			int third=number/3;
    			System.out.print (check(toArray(number, digits),toArray(third, digits), digits));
    			System.out.print (number);
    			System.out.println (third);
    		}
    		keyboard.close();
    	}
    	public static int[] toArray(int number, int digits) 
    	{
    		int[] array = new int[digits];
    		for (int i = 0; i < digits; i++)
    		{
    			array[i] = number % 10;
    			number /= 10;
    		}
    		return array;
    		
    	}
    	public static boolean check (int[]number, int[] third, int digits)
    	{
    		int matches=0;
    		for (int i=0;i<number.length;i++)
    			for (int j=0;j<third.length;j++)
    				if (number[i]==third[j])
    				{
    					matches++;
    					third[j]=-1;
    				}
    		if (matches==digits)
    			return true;
    		return false;
    	}
    }
    I can't provide example I can't find them online...
    but a true would if two numbers have the same digits, no repeats
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    I can't provide example
    That makes it hard to test the code if you don't have known examples. How do you know that the case is ever true?

    For testing Does the check() method require that the numbers in one array be generated from the numbers in the other array?
    For testing hard code two arrays of the same length and pass them to the check() method. For example: 1,2,3 and 3,2,1
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    Ugh my head hurts so I'm gonna move on to the next one...I'll check the check later
    This code's supposed to read a text that shows items, the price of them and quantity, and displays them in a chart
    Code:
    public class Inventory
    {
    	public static void main(String[] args) throws FileNotFoundException
    	{
    		Scanner inFile=new Scanner (new File ("inventory1.txt")); // Get info file
    		String name;
    		double price;
    		int quantity;
    		double total;
    		double grand=0;
    		// Print header
    		System.out.println("Product Name          Price ($)     Quantity     Total Value ($)");
    		while (inFile.hasNextLine())
    		{
    			// Read name, price, quantity and display them (plus total) for each item
    			name=inFile.nextLine();
    			inFile.nextLine();
    			price=inFile.nextDouble();
    			quantity=inFile.nextInt();
    			inFile.nextLine();
    			// Calculate total and grand total
    			total=price*quantity;
    			grand+=total;
    			System.out.printf("%10s %5d %5i %10d", name, price, quantity, total);
    		}		
    		inFile.close(); // Close file		
    		System.out.print("Overall total"); // Print grand total
    		System.out.printf("%50d", grand);
    	}
    }
    For some reason it doesn't enter the while loop, and only shows the headers
    This is the file it reads from
    Code:
    Product #1960
    40.88
    17
    Product #1627
    14.56
    34
    Product #1108
    76.30
    11
    Product #1223
    43.70
    33
    Product #1514
    1120.52
    1
    Product #1259
    46.36
    35
    Product #1802
    28.23
    38
    Product #1904
    49.78
    28
    Product #1667
    45.58
    2
    Product #1644
    1.07
    178
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    What is printed out by the program?

    What is the value of inFile.hasNextLine() just before the while() loop starts executing?

    One problem I see is that there are many calls to methods that will read data from the file inside of the while() loop without the hasNext test to see if there is data.

    Another problem is that the printf() statement does not have a newline ("\n") character at the end so all its output will be on one line.
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo