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 6 of 6

Thread: Java program to take input from a file as a string of 3 integers separated by a space

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java program to take input from a file as a string of 3 integers separated by a space

    I have a method in which I'd like to check that the input read from a file is a string of 3 integers sperated by a space.

    e.g

    123 456 789
    987 654 321

    etc

    My problem is when it retuns to main it seems to return false everytime time apart from if I input the integers with no whitespace

    e.g

    123456789
    987654321

    My theory is the method checks the whitespace and flags it as not integer and returns false. I cant find a way around this.

     
    	static boolean checkNumbersOnly(String input)
    	{
    		int i;
     
    		//if no input or null set to false
    		if(input == null || input.length() == 0)
    			return false;
     
    		//loop through input and check each character is an integer
    		//if a non integer is found return false
    		for(i = 0; i < input.length(); i++)
    		{
    			if(!Character.isDigit(input.charAt(i)))
    				return false;
    		}
     
    		return true;
     
    	}//End Method

    I'd like to check inputs such as

    abc 122 456

    things that are incorrect but will be picked up in the check and flagged as false.

    Thanks in advance
    Last edited by Deep_4; November 7th, 2012 at 11:12 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Check string is integers only

    Hey xeoric,

    Could you please post all your code for me to take a look at? This will enable me to have a play around with it..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check string is integers only

     
    import B102.*;
    import java.util.*;
     
    class Triangle
    {
    static boolean checkNumbersOnly(String input)
    {
    	int i;
     
    	//if no input or null set to false
    	if(input == null || input.length() == 0)
    		return false;
     
    	//loop through input and check each character is an integer
    	//if a non integer is found return false
    	for(i = 0; i < input.length(); i++)
    	{
    		if(!Character.isDigit(input.charAt(i)))
    			return false;
    	}
     
    	return true;
     
    }//End Method
     
    //process method here
    static String findTriangle(String input)
    {
    	String str1 = "an Equilateral", str2 = "an Isosceles", str3 = "a Scalene", str4 = "an Invalid";
    	String output, st1, st2, st3;
    	int n, temp = 0, sum = 0, n1, n2, n3, sti1, sti2, sti3;
     
    	//Break the input line into tokens
    	StringTokenizer st = new StringTokenizer(input);
     
    	//count tokens
    	n = st.countTokens();
     
    	if(n == 3)
    	{
    		//Set tokens as variables so it can sort them into ascending order
    		st1 = st.nextToken();
    		st2 = st.nextToken();
    		st3 = st.nextToken();
     
    		//Change tokens to integers
    		sti1 = Integer.parseInt(st1);
    		sti2 = Integer.parseInt(st2);
    		sti3 = Integer.parseInt(st3);
     
    		//sort integers into ascending order
    		if(sti1 > sti2)
    		{
    			temp = sti1;
    			sti1 = sti2;
    			sti2 = temp;
    		}
    		if(sti2 > sti3)
    		{
    			temp = sti2;
    			sti2 = sti3;
    			sti3 = temp;
    		}
    		if(sti1 > sti2)
    		{
    			temp = sti1;
    			sti1 = sti2;
    			sti2 = temp;
    		}			
     
    		//Check 1st two lengths are smaller than last length then check for triangle type
    		sum = sti1 + sti2;
     
    		if(sum > sti3)
    		{
    			if((sti1 == sti2) && (sti2 == sti3))
    			{
    				output = str1;
    			}
    		else
    			if((sti1 == sti2) && (sti1 != sti3))
    			{
    				output = str2;
    			}
    			else
    			{
    				output = str3;
    			}
    		}
    		else
    		{
    			output = str4;
    		}
    	}
    	else
    	{
    		output = str4;
    	}
     
    	return output;
     
    }//End Method		
     
    //Read file name from command line
    public static void main(String[] args)
    {
     
    textFile srcFile, dstFile;
    String line, type;
    boolean b = true;
    int count = 0;
     
    //if there are no file names on command line
    if(args.length == 0)
    {
    	System.out.println("No filename given");
    	System.exit(1);
    }
     
    //if there is only one file name on command line use
    if(args.length == 1)
    {
    	//open source file on command line for reading
    	srcFile = new textFile(args[0], 'r');
     
    	while(!srcFile.EOF())
    	{
    		//read new line from file
    		line = srcFile.readLine();
    		count++;
     
    		//check line contains only numbers
    		b = checkNumbersOnly(line);
     
    		if(b == true)
    		{
    			//get triangle type
    			type = findTriangle(line);
     
    			System.out.println("The lengths " + line + " make up " + type + " Triangle");
    			}
    			else
    			{
    				System.out.println("Error: line " + count + " not all inputs are integers");
    			}
    		}
     
    		srcFile.closeFile();
    	}
    	else
    	{
    		//if there are two file names on the command line one for reading one for writing
    		if(args.length == 2)
    		{
    			//open source file on command line for reading
    			srcFile = new textFile(args[0], 'r');
     
    			//open destination file for writing
    			dstFile = new textFile(args[1], 'w');
     
    			while(!srcFile.EOF())
    			{
    				//read new line from file
    				line = srcFile.readLine();
    				count++;
     
    				//check line contains only numbers
    				b = checkNumbersOnly(line);
     
    				if(b == true)
    				{
     
    					//get triangle type
    					type = findTriangle(line);
     
    					dstFile.writeLine(type);
    				}
    				else
    				{
    					System.out.println("Error: line " + count + " not all inputs are integers");
    				}
    		}
     
    		srcFile.closeFile();
    		dstFile.closeFile();
     
     
    		}
    		else
    		{
    			System.out.println("Error: There are more than two files input");
    			System.exit(1);
    		}
    	}
     
    	return;
    }
    }

    It looks neater in notepad

    Basically I have to read lines from a file which contain the input stated above and validate what type of triangle it is.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Check string is integers only

    What is B102.*; used for?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check string is integers only

    It was a file from the uni it contains some classes I would be using.

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Check string is integers only

    If the numbers are always in the format of number space number then you could do something like this
    import java.util.regex.Pattern;
    ...
    static boolean MatchNumbers(String input){
        return Pattern.matches("\d*\s\d*\s\d*", input);
    }

    EDIT: I Just read a previous post of yours and it turns out that Pattern matching is above your supposed level...what a lie anyways, Solution mark II from me.

    static boolean MatchNumbers(String input){
        for(num : input.split(" ")){
           try{
              Integer.parseInt(num);
           }catch(NumberFormatException nfe){ return false; }
         }
         return true;
    }

    Regards,
    Chris
    Last edited by Freaky Chris; May 21st, 2009 at 05:51 AM.

Similar Threads

  1. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  2. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM
  3. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM
  4. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM
  5. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 05:37 AM