Results 1 to 7 of 7
  1. #1
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default whats wrong with my code?

    so this is a very rough code so far. i want it to scan a file and at least print something from that class (dependent, head, etc)
    so far its only printing out:

    Please enter the name of the file:
    input.txt
    end of head class: 0.0
    This is where Head is printed: 0.0
    This is where Dependents is printed: 0.0
    This is where Dependents is printed: 0.0
    End of first block
    end of head class: 0.0
    This is where Head is printed: 0.0
    This is where Dependents is printed: 0.0
    This is where Dependents is printed: 0.0
    This is where Dependents is printed: 0.0
    End of first block
    end of head class: 0.0
    This is where Head is printed: 0.0
    End of first block
    end of head class: 0.0
    This is where Head is printed: 0.0
    This is where Dependents is printed: 0.0
    End of first block
    End of first block

    i want those 0.0 to be something from head and something from dependent depending on where its printed (This is where Dependents is printed:)

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Scanner;
    
    
    public class adjust {
    static double completeTotal = 0;
    	public static void main(String[] args) throws IOException, MalformedURLException {
    
    		Scanner input = new Scanner(System.in);
    		System.out.println("Please enter the name of the file: ");
    		String file = input.next();
    		File portfolio = new File(file);
    		Scanner scan = new Scanner(portfolio);
    
    
    		while (scan.hasNextLine()) {
    			String line = scan.nextLine();
    			if (line.startsWith("Head")){
    				System.out.println("This is where Head is printed: " + head(line));
    			}
    			else if (line.startsWith("Dependent")){
    				System.out.println("This is where Dependents is printed: " + Dependent(line));
    			}
    			else{
    				System.out.println("End of first block");
    			}
    		}
    		System.out.println();		
    	}
    		public static double head (String head) {	
    			Scanner scan = new Scanner(head);
    			String name, gender, eyeColor, hairColor = " ";
    			double height = 0;
    
    			if (scan.hasNext()){			
    				name = scan.next();
    			}
    			if (scan.hasNext()){			
    				gender = scan.next();
    			}
    			if (scan.hasNext()){
    				eyeColor = scan.next();
    			}
    			if (scan.hasNextInt()){
    				hairColor = scan.next();
    			}
    			if (scan.hasNextDouble()){
    				height = scan.nextDouble();
    			}
    			System.out.println("end of head class: "  + hairColor + " " + height);
    			return (height);
    		}
    		public static double Dependent (String Dependent) {	
    			Scanner scan = new Scanner(Dependent);
    			String dependents, dependentsName, dependentsGender, dependentsStatus = "";
    			double dependentsHeight = 0;
    
    			if (scan.hasNext()){			
    				dependentsName = scan.next();
    			}
    			if (scan.hasNext()){			
    				dependentsGender = scan.next();
    			}
    			if (scan.hasNext()){			
    				dependentsStatus = scan.next();
    			}
    			if (scan.hasNextDouble()){
    				dependentsHeight = scan.nextDouble();
    			}
    			return dependentsHeight;
    		}
    	}
    here is the file to scan:
    Java Code:
    Head steven M brown brown 6.15
    Dependent michelle F adult 5.9
    Dependent mike M minor 5.7
    End
    Head sarah F blue brown 5.5
    Dependent joe M adult 6.2
    Dependent john M minor 6.1
    Dependent kaylee F adult 5.3
    End
    Head michael M brown brown 6
    End
    Head gina F green red 5.7
    Dependent mark M minor 4.0
    End
    SUPER-END
    any thoughts?

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    10

    Default

    Im not sure if its a problem, but you are printing out a statement which needs to be evaluated. During evaluation you are also printing something else out. This may be causing conflicts (im not sure though)
    Try this:
    Java Code:
    if (line.startsWith("Head")){
        double temp = head(line);
        System.out.println("This is where Head is printed: " + temp);
    }

  3. #3
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    thanks for the suggestion, unfortunately it is still giving me the same print out:

    Please enter the name of the file:
    input.txt
    end of head class: 0.0
    This is where Head is printed: 0.0
    This is where Dependents is printed: 0.0
    This is where Dependents is printed: 0.0
    End of first block
    end of head class: 0.0
    This is where Head is printed: 0.0
    This is where Dependents is printed: 0.0
    This is where Dependents is printed: 0.0
    This is where Dependents is printed: 0.0
    End of first block
    end of head class: 0.0
    This is where Head is printed: 0.0
    End of first block
    end of head class: 0.0
    This is where Head is printed: 0.0
    This is where Dependents is printed: 0.0
    End of first block
    End of first block

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    10

    Default

    Hmm. ok.
    Are you sure that the scanner is correctly set up?
    You may want to double check the if statements.

    for instance
    Java Code:
    if (scan.hasNext()){			
    	dependentsName = scan.next();
        System.out.println ("dependent: name: " + dependentsName); // <-- add this :P
    }
    I've got a nasty feeling itll be printing out "dependent" instead of the name (as its the first token)
    Im not too good with scanners though.

    Personally id do String[] fields = String.split etc.

  5. #5
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    can you give me an example of how i would use a String[] field with this project?

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    10

    Default

    You have four different string inputs
    eg.
    Java Code:
    Head gina F green red 5.7
    Dependent mark M minor 4.0
    End
    SUPER-END
    You need to parse the string into tokens, where each token represents a field

    Java Code:
    public void parse (String line) {
        String [] tokens = line.split (" "); // split takes a regular expression saying where is 
                                                    // should split the line
    }
    You know that the first token (i.e. tokens[0]) is going to be "Head", "Dependent", "End", or "SUPER-END", so do an if statement to differentiate between them.

    For a "head", tokens[1] will be name, tokens[2] will be gender etc.
    For a dependent, tokens[1] will be name etc etc.

    obviously you will need to convert the string to a double as appropriate.

    Does this make sense?

  7. #7
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    yeah that makes sense. for this assignment i need to make separate classes and use arrays. i believe what you are suggesting is to make the arrays,correct?

Similar Threads

  1. whats wrong
    By atenv in forum New To Java
    Replies: 6
    Last Post: 06-15-2010, 02:55 PM
  2. whats is wrong with this app??
    By mrajan in forum New To Java
    Replies: 4
    Last Post: 06-09-2010, 11:56 PM
  3. Whats wrong with this code???
    By javanew in forum New To Java
    Replies: 4
    Last Post: 03-28-2010, 06:46 PM
  4. Whats wrong with this code?
    By bbtgirl in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 04:51 AM
  5. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 01:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •