Hey i have a program thats due sunday and my teacher doesn't really teach that well....so i'm kinda confused and need help.

problem: read a file of positive intergers and then print each lin and then print each number in the line (i've done this part) this is where i get lost part to is to design and implement a new method that ask a user for a number and then searches the file for multiples of the user enter numbered....


this is what i have so far (it only does the first part of this program)

code!

//********************************************
//
//
//********************************************

import java.util.Scanner;
import java.io.*;

public class MultipleSelection
{
	//Reads numbers from file and prints
	public static void main (String[] args) throws IOException
	{
		String number;
		Scanner fileScan, numberScan;
		int value, limit, mul, count=0;
	
		fileScan = new Scanner (new File("numbers.txt"));

      //Read and process each line of the file
		while(fileScan.hasNext())
		{
			number=fileScan.nextLine();
			
			System.out.println(number);
						
			numberScan = new Scanner (number);
			
			numberScan.useDelimiter(",");
			
			while(numberScan.hasNext())
				System.out.println(" " + numberScan.next());
				
			System.out.println();
		}				
	}
}

text file!

56, 23, 46,25,
14, 15, 17, 19,
4, 5, 6, 4,
56, 9, 0, 65,

could someone help me or direct me to a similar program so i can get an understanding of what i am suppose to be doing! thanks

Recommended Answers

All 16 Replies

Here is what you do:

get all your integers into a int array (like this: int[] intArray = new int[count]; Make sure to count your integers first (run trough the file and count them)). Ok, so next you want to use something that is called modulo. The operator you use is %. What modulo does is that it takes your first number and subtract x amounts of the second number until the number you get is smaller that the number you subtract (also works for negative numbers). Example: 61%5 = 1. You subtract 5 from 61 until the reminding number is smaller than 5. So if you take 65%5 you will end up with 0. So now you want to get the number the user input and take all the number and check them with the modulo and see if any of them end up as 0. this means that the number is a whole number times the number the user input.

so you want a loop that looks something like this:

for(int i = 0; i<intArray.length; i++)
{
if(intArray[i]%userInputInt == 0)
{
//do whatever you want to do. Probably print out something.
}
}

I hope this helps you!

cheers

Here is what you do:

get all your integers into a int array (like this: int[] intArray = new int[count]; Make sure to count your integers first (run trough the file and count them)). Ok, so next you want to use something that is called modulo. The operator you use is %. What modulo does is that it takes your first number and subtract x amounts of the second number until the number you get is smaller that the number you subtract (also works for negative numbers). Example: 61%5 = 1. You subtract 5 from 61 until the reminding number is smaller than 5. So if you take 65%5 you will end up with 0. So now you want to get the number the user input and take all the number and check them with the modulo and see if any of them end up as 0. this means that the number is a whole number times the number the user input.

so you want a loop that looks something like this:

for(int i = 0; i<intArray.length; i++)
{
if(intArray%userInputInt == 0)
{
//do whatever you want to do. Probably print out something.
}
}


I hope this helps you!

cheers

thanks i'm about to try that now!

have no idea whats wrong now just say ; expected....help please!

new code

//********************************************
//
//
//********************************************

import java.util.Scanner;
import java.io.*;

public class MultipleSelection
{
	//Reads numbers from file and prints
	public static void main (String[] args) throws IOException
	{
		String number;
		Scanner fileScan, numberScan;
		int value, limit, mul, count=0;
	
		fileScan = new Scanner (new File("numbers.txt"));

      //Read and process each line of the file
		while(fileScan.hasNext())
		{
			number=fileScan.nextLine();
			
			System.out.println(number);
						
			numberScan = new Scanner (number);
			
			numberScan.useDelimiter(",");
			
			while(numberScan.hasNext())
				System.out.println(" " + numberScan.next());
				
			System.out.println();
		}			
	}
	
}

private class checkMultiple
{
	public static void main (String[] args) extends MultipleSelection
	{
		while(fileScan.hasNext())
		{
			int[] intArray = new int[count];
		}
		
		for(int i = 0; i<intArray.length; i++)
		{
			if(intArray[i]%userInputInt == 0)
			{
			 System.out.println(intArray[i]);
			}
		}
	
	}
}

how do i call the method i noticed thats something i did not do

Well... what can I say. There are so much wrong(no offence). You need to get info from the user, you need to fill the int array. You need to declare all your variables and so on. (these are very basic things. If you do not know how to do this you need to read about it before doing anything more).

One thing I can help you with is that you need to have "checkMultiple" in a method, and not make it as a class. Another thing is that you need to find out what "count" should be before using it. You need to run a loop that counts how many numbers you got in the file, then use that number to create a new int array. But you dont accually need to put it in an array, you could just check it straight away.

Well... what can I say. There are so much wrong(no offence). You need to get info from the user, you need to fill the int array. You need to declare all your variables and so on. (these are very basic things. If you do not know how to do this you need to read about it before doing anything more).

One thing I can help you with is that you need to have "checkMultiple" in a method, and not make it as a class. Another thing is that you need to find out what "count" should be before using it. You need to run a loop that counts how many numbers you got in the file, then use that number to create a new int array. But you dont accually need to put it in an array, you could just check it straight away.

oh my i've been racking my head all day didnt realize id left out those basic things...kinda have a headache...but thanks i'm bout to get to doing that now!

new code still errors

//********************************************
//
//
//********************************************

import java.util.Scanner;
import java.io.*;

public class MultipleSelection
{
	//Reads numbers from file and prints
	public void main (String[] args) throws IOException
	{
		int number, num;
		Scanner fileScan, numberScan;
		int positivenum, length=0;
		
	
		fileScan = new Scanner (new File("numbers.txt"));

      //Read and process each line of the file
		while(fileScan.hasNext())
		{
			number=fileScan.nextLine();
			
			System.out.println(number);
						
			numberScan = new Scanner (number);
			
			numberScan.useDelimiter(",");
							
			while(numberScan.hasNext())
			{
				System.out.println(" " + numberScan.next());
				
				num=numberScan.next();
				
				int[] intArray = new int[length];
				
				intArray[length]=num;
				
				length++;
			}
	
			System.out.println();
		}			
		
		Scanner scan=new Scanner (System.in);
		
		System.out.println("Enter a positive number to search the file for its multiples");
		positivenum = scan.nextInt();
		
		checkMultiple(positivenum,intArray, length);
		
	}
	
	public boolean checkMultiple (int userInput, int[] array, int length)
	{
		final int PER_LINE =5;
		
		int count=0;
		
		for(int i=0; i < length; i++)
		{
			if(array[i]%userInput ==0)
			{
				System.out.print(array[i]+ "\t");
				
				count++;
				
				if(count%PER_LINE==0)
					System.out.println();
			}
		}
		
		return false;
	}
	
}

You got a problem with your method of checking the length:

while(numberScan.hasNext())
			{
				System.out.println(" " + numberScan.next());
 
				num=numberScan.next();
                      //HERE YOU ARE DECLARING A NEW INT ARRAY EVERY TIME THE CODE LOOPS. THIS RESULT IN YOU DELETING EVERYTHING IN THE ARRAY EACH TIME AND YOU ALLOCATE NEW SPACE IN THE MEMORY EACH TIME.
				int[] intArray = new int[length];
 
				intArray[length]=num;
 
				length++;
			}

What you should do is something like this: (There might be other ways to do this, too, but this is the only one i know of)

numberScan = new Scanner (number);
 
			numberScan.useDelimiter(",");
 
			while(numberScan.hasNext())
			{

                                num=numberScan.next(); //Given that this way of getting to the next number in your scanner works. 
				length++;
			}
numberScan = new Scanner (number);
	int[] intArray = new int[length];
num = 0;

for(int i = 0; i < intArray.length; i++)
			{ 
				intArray[i]=numberScan.nextInt();
 			}

Now, for the next bit: You need to fix a little bit on your checkMultiple method.

public void checkMultiple (int userInput, int[] array)
	{
		for(int i=0; i < array.length; i++)
		{
			if(array[i]%userInput ==0)
			{
				System.out.print("The number "+array[i]+ " is a multiple of "+userInput+".");
 
 			}
		}
	}

I hope this helps! Good luck.

You got a problem with your method of checking the length:

while(numberScan.hasNext())
			{
				System.out.println(" " + numberScan.next());
 
				num=numberScan.next();
                      //HERE YOU ARE DECLARING A NEW INT ARRAY EVERY TIME THE CODE LOOPS. THIS RESULT IN YOU DELETING EVERYTHING IN THE ARRAY EACH TIME AND YOU ALLOCATE NEW SPACE IN THE MEMORY EACH TIME.
				int[] intArray = new int[length];
 
				intArray[length]=num;
 
				length++;
			}

What you should do is something like this: (There might be other ways to do this, too, but this is the only one i know of)

numberScan = new Scanner (number);
 
			numberScan.useDelimiter(",");
 
			while(numberScan.hasNext())
			{

                                num=numberScan.next(); //Given that this way of getting to the next number in your scanner works. 
				length++;
			}
numberScan = new Scanner (number);
	int[] intArray = new int[length];
num = 0;

for(int i = 0; i < intArray.length; i++)
			{ 
				intArray[i]=numberScan.nextInt();
 			}

Now, for the next bit: You need to fix a little bit on your checkMultiple method.

public void checkMultiple (int userInput, int[] array)
	{
		for(int i=0; i < array.length; i++)
		{
			if(array[i]%userInput ==0)
			{
				System.out.print("The number "+array[i]+ " is a multiple of "+userInput+".");
 
 			}
		}
	}

I hope this helps! Good luck.

thanks! but my teacher wants the checkMultiple method to return a boolean value.

thanks i've made the corrections and i have one error now that says:

MultipleSelection.java:60: illegal start of expression
	public boolean checkMultiple (int userInput, int[] array)
	^
1 error

this is my new code

/********************************************
//
//
//********************************************

import java.util.Scanner;
import java.io.*;

public class MultipleSelection
{
	//Reads numbers from file and prints
	public void main (String args) throws IOException
	{
		String number, num;
		Scanner fileScan, numberScan;
		int positivenum, length=0;
		
	
		fileScan = new Scanner (new File("numbers.txt"));

      //Read and process each line of the file
		while(fileScan.hasNext())
		{
			number=fileScan.nextLine();
			
			System.out.println(number);
						
			numberScan = new Scanner(number);
			
			numberScan.useDelimiter(",");
							
			while(numberScan.hasNext())
			{
				System.out.println(" " + numberScan.next());
				
				num=numberScan.next();
			
				length++;
			}
			
			numberScan = new Scanner (number);
				int[] intArray = new int[length];
			
			num=0;
			
			for(int i=0; i <intArray.length; i++)
			{
				intArray[i]= numberScan.nextInt();
			}			
		
		Scanner scan=new Scanner (System.in);
		
		System.out.println("Enter a positive number to search the file for its multiples");
		positivenum = scan.nextInt();
		
		checkMultiple(positivenum,intArray);
		
	}
	
	public boolean checkMultiple (int userInput, int[] array)
	{
		final int PER_LINE =5;
		
		int count=0;
		
		System.out.print("The followin numbers in the file are a multiple of " +userInput+ ".");
		
		for(int i=0; i < arrary.length; i++)
		{
			if(array[i]%userInput ==0)
			{
				System.out.print(array[i]+ "\t");
				
				count++;
				
				if(count%PER_LINE==0)
					System.out.println();
			}
		}
		
		return false;
	}
	
}

Well since you have a /* at the beginning you need to end it with a */ or else your whole code will be one giant comment ;)

Try making it a "public static" method. And you want it to return true if it turns out that there is a multiple of that value in the file. So write instead:

for(int i=0; i < arrary.length; i++)
		{
			if(array[i]%userInput ==0)
			{
			return true;
			}
		}

And then use that boolean value for something.

Maybe my eyes are decieving me, but it looks like you just need to add another curly brace at the end of your main method. It seems to me that the while loop started on line 22 never gets closed.

Well since you have a /* at the beginning you need to end it with a */ or else your whole code will be one giant comment ;)

thanx that would mess up the entire program lol

thanks for the help even though my work past due i still want to get it to work so i'm about to try your ideas and solutions now

thanx that would mess up the entire program lol

Well, commected out code won't give compiler errors :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.