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

Thread: Simple problem help!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple problem help!

    Hello!

    So I'm writing a massive code for a school project at the moment, and while I have all the complicated stuff, I am getting stuck on this simple bit. I'm trying to scan in a text file, a single line of a bunch of characters, then make that file a string, then traverse the string to see if there are any upper case A-G's. My code as it is compiles, and runs, but none of my int counters are going up. Someone this not reading correctly.

    Can someone help? This is the last thing I have to do for this

    Below is the section of relevant code:

    import java.util.*;
    import java.io.*;
    import javax.swing.JOptionPane;
     
    public class test5cop3530{
     
    public static void main(String[] args) {
     
    BaseObArr array;
    array = new BaseObArr(7);
     
    //declare counters to record the number of letters A-G
    	int a = 0;
    	int b = 0;
    	int c = 0;
    	int d = 0;
    	int e = 0;
    	int f = 0;
    	int g = 0;
     
    File input = new File(args[0]); //create a file named input, assign it to the file run with the program
    Scanner read;  		//Create a scanner
    try
    {
    read = new Scanner(input);	//wrap a scanner around my input file, call it read
    String data = read.next(); //create a string from my file
     
    //Traverse the String and count how many times I see and A-G
    int i = 0;
    while(i < data.length());
    {
    if(data.charAt(i)=='A')
    {
    a++;
    i++;
    }
    else if(data.charAt(i)=='B')
    {
    b++;
    i++;
    }
    else if(data.charAt(i)=='C')
    {
    c++;
    i++;
    }
    else if(data.charAt(i)=='D')
    {
    d++;
    i++;
    }
    else if(data.charAt(i)=='E')
    {
    e++;
    i++;
    }
    else if(data.charAt(i)=='F')
    {
    f++;
    i++;
    }
    else if(data.charAt(i)=='G')
    {
    g++;
    i++;
    }
    else
    i++;
    }
     
    }
    	catch(Exception ex) {
      ex.printStackTrace();
    }
    //Check to make sure the values are correct
    System.out.print("a is " +a+ "\nb is "+b+"\nc is "+c+"\nd is "+d+"\ne is "+e+"\nf is "+f+"\ng is "+g);
     
    ...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Simple problem help!

    Add a println() statement after where the String is read that prints out the String so you can see what the computer sees when it executes.

    The code needs to be formatted by indenting the nested statements. Not all statements should start in the first column. Properly formatted code is easier to read.

    Also there are a lot of hidden }s at the end of statements.
    } should be easy to see on a line by themselves.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple problem help!

    I did. The string is exactly what I have entered. The problem is, now it's getting stuck in an endless loop. The string class has a .length method, so that shouldn't be the problem.

    --- Update ---

    Yeah, I'm looking over that while loop...I printed string.length and for my data it's giving me 87. The .charAt() is also reading in correctly. I have increment statements in every portion of my while loop, in all my if statements. I declared the variables outside of the try/catch block so they should retain their values why I reach the end. Where is it getting stuck?

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

    Default Re: Simple problem help!

    Look at the while loop and see if it is coded correctly. Add a println first thing inside the while loop that prints out the value of i to check that it is incrementing correctly. The poorly formatted code with the hidden }s makes the code hard to read.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple problem help!

    It looks like I'm not even entering the while loop. Why would that happen? I printed the values of k and data.length() just before the while loop and they are correct. That boolean should be true.

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

    Default Re: Simple problem help!

    I'm not even entering the while loop
    Is there a statement terminating ; for the while?
    The code following the while that is inside the {}s is not part of the while statement.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple problem help!

    ...and this is why I shouldn't code for 3 days straight...I'm an idiot.

    I got it. Thanks so much for your help!

Similar Threads

  1. Simple loop problem
    By tyneframe in forum Loops & Control Statements
    Replies: 7
    Last Post: October 15th, 2012, 12:00 AM
  2. Simple TextListener Problem =/ Please help!
    By austin.rose94 in forum AWT / Java Swing
    Replies: 2
    Last Post: December 8th, 2011, 01:55 PM
  3. please help, simple problem
    By grandmst in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2011, 06:26 PM
  4. Very simple problem...PLEASE HELP!
    By dungeondragon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 1st, 2011, 07:19 AM
  5. Simple problem...
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 6th, 2011, 12:02 AM