I am in the same situation and need help with the same problem. What you have for code is everything I have had since last week and it still prints "25" Ive tried fixing it many times and nothing had changed.
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.
I am in the same situation and need help with the same problem. What you have for code is everything I have had since last week and it still prints "25" Ive tried fixing it many times and nothing had changed.
It is printing 25 because in your for loop the if statement doesnt have brackets around the lines after it. I recommend always putting brackets around your blocks. You will avoid many of these errors in your future. Your largestindex variable is getting "a" which will always end in 25 because it is being set every time. IF STATEMENTS WITHOUT BRACKETS ONLY CONTROL ONE LINE DIRECTLY AFTER THEM. Use brackets...for the love of god use brackets. The sooner you do this the happier you'll be.
For those still looking to figure this out, i have the program working in my possession. I will help you work through your issues as you present questions, but i'm not just going to give you the answer. Work for it! If you have a due date, set up a time you can be on here and i'll make sure i'll be on at that time and help you through it till we're done.
public class LetterProfile
{
int scoreboard[] = new int [26];
private int index;
private int next;
private int largest =0;
private int largestindex =0;
public void countChars (String s)
{
// changes the input text to lower case letters
s = s.toLowerCase();
//
char a = 'a';
for (int i =0;i < s.length();i++)
{
int next = (int)s.charAt(i)-(int) a;
if ( next<26 && next >= 0)
scoreboard[next]++;
}
}
public void scoreLine (String line)
{
int index = line.length();
for (int j = 0; j<index; j++)
{
scoreboard[j]++;
System.out.println(j + scoreboard[j]);
}
}
public int largestLength()
{
int largest = 0;
int largestindex = 0;
for(int a = 0; a<26; a++)
{
if(scoreboard[a]>largest)
largest = scoreboard[a];
largestindex = a;
}
return (char)largestindex;
}
public int printResults()
{
largestLength();
return largestLength();
}
}
I have this thus far and when I run it the java input bars come up and then when I type things into it nothing is returned
Please start your own thread for your problem.
Where is the class with the main() method. That is where the program's execution starts.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
If you don't understand my answer, don't ignore it, ask a question.
That's where my post went...i'll go grab my other post...
--- Update ---
Your printResults function does not print anything. You need to print what your longestLength() function is returning. And dont call it twice for that matter. Call it once, save it to a variable if necessary, then display it.
I don't understand how to change this at all. I understand that I shouldn't have printResults function because it is simply not doing anything but I don't get how to change it. Sorry so new to programming
I suggested this on the other thread. Learn the techniques by writing a small simple program.
Then add to it to learn how to use methods.
I suggest that you put this code aside for a while and work on a very simple program to see how to call a method.
Write a new class with a main() method and one other method.
Have the main() method create an object and call the other method.
Have the other method print out a message.
A Very simple program. When you get that working, then we'll work on adding some features one small step at a time.
If you don't understand my answer, don't ignore it, ask a question.
I'm really confused too. Here is a simple program that has a "max" method and tells you the larger integer that you input. Also, why does this program work in 1 file, but the letter frequency needs 2 files?
import java.util.Scanner; public class MaxNumber { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int i = scan.nextInt(); int j = scan.nextInt(); int k = max(i, j); System.out.println("The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int big; if (num1 > num2) big = num1; else big = num2; return big; } }
Someone somewhere thinks that the main() method testing MUST be in a special class, not in the class that is being tested.
Personally I think the testing code (the main() method) should be in the class that is being tested.
If you don't understand my answer, don't ignore it, ask a question.
I have this for my driver class... But I don't know what to put so that it will print out the number count...I have made is so that there is a 2 line input
import java.util.Scanner;
public class LetterDriver{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String tScan= " ";
while(tScan.length() > 0){
tScan = scan.nextLine();
}
}
}
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
What is the purpose of the Scanner object and the while loop?
Are you ignoring what I suggested in post#7?
If you don't understand my answer, don't ignore it, ask a question.
That is what my professor said it is a scanner object... and the while loops scans the lines of text?
Why does the program want to scan lines of text? What is the purpose of doing that?while loops scans the lines of text?
The posted code is not formatted properly. Nested statements within {}s should be indented 3-4 spaces. All statements should NOT start in the first column.
You should start by doing the sample programs in the tutorial. This assignment is past your current knowledge.
See: "Hello World!" for Microsoft Windows (The Java™ Tutorials > Getting Started > The "Hello World!" Application)
If you don't understand my answer, don't ignore it, ask a question.
sorry when I coped it lines got shifted. The reason for the scan lines is that it will scan the text that is inputed then will return letters and count of how many times they appear
For testing I suggest that you comment out the scanner and the loop and define a String to scan in the program.
When the code can scan and report on that String, then you can restore the loop etc. For example:String toScan = "The string to scan would go here";
If you don't understand my answer, don't ignore it, ask a question.