473,379 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Java Assignment

Hello I am new but I have been in a intro programming class with Java as the main entree. I have this program where I need to end input characters at 65 characters and break the line and then add a new line.

This is my code:

import static java.lang.System.*;
import java.util.Scanner;

class formatter3 {

public static void main (String[] args) {
Scanner stdin = new Scanner (System.in);
int maxlen = 65;
int outlen = 0;
int spaces = 0;
while (stdin.hasNext ()) {
String word = stdin.next();
System.out.printf(word); ++(spaces);
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
}
}

}


and i get these error messages when i try to complie:

bash-2.03$ javac formatter3.java
formatter3.java:14: cannot find symbol
symbol : variable length
location: class java.lang.String
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java:14: operator + cannot be applied to int,java.lang.String.length
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java:14: operator > cannot be applied to <nulltype>,int
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java:14: cannot find symbol
symbol : method printf(java.lang.String)
location: class formatter3
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
4 errors


Waaahhhh!!!! any suggestions would be helpful. please I've been searching aorund campus for a community that does java and could help out and let me be a part, but santa cruz is HIPPY! x_x
Jul 19 '07 #1
8 2421
Atli
5,058 Expert 4TB
Hi, and welcome to TSDN!

You have come to the right place my friend!
Let me just move this question over to the Java forums, where our Java experts are more likely to find it.

Moderator
Jul 20 '07 #2
1. you need to import java.lang.String class

code:
import java.lang.String;

2. there is nop printf() in Java, you need to use:

System.out.println("");

think that should fix it

Hello I am new but I have been in a intro programming class with Java as the main entree. I have this program where I need to end input characters at 65 characters and break the line and then add a new line.

This is my code:

import static java.lang.System.*;
import java.util.Scanner;

class formatter3 {

public static void main (String[] args) {
Scanner stdin = new Scanner (System.in);
int maxlen = 65;
int outlen = 0;
int spaces = 0;
while (stdin.hasNext ()) {
String word = stdin.next();
System.out.printf(word); ++(spaces);
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
}
}

}


and i get these error messages when i try to complie:

bash-2.03$ javac formatter3.java
formatter3.java:14: cannot find symbol
symbol : variable length
location: class java.lang.String
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java:14: operator + cannot be applied to int,java.lang.String.length
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java:14: operator > cannot be applied to <nulltype>,int
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java:14: cannot find symbol
symbol : method printf(java.lang.String)
location: class formatter3
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
4 errors


Waaahhhh!!!! any suggestions would be helpful. please I've been searching aorund campus for a community that does java and could help out and let me be a part, but santa cruz is HIPPY! x_x
Jul 20 '07 #3
prometheuzz
197 Expert 100+
1. you need to import java.lang.String class

code:
import java.lang.String;
No everything from the java.lang package is automatically imported.


2. there is nop printf() in Java, you need to use:

System.out.println("");

think that should fix it
Yes there is:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.util.Locale,%20java.l ang.String,%20java.lang.Object...)
and since the OP imported System.out with a static import, this should work:

Expand|Select|Wrap|Line Numbers
  1. out.printf("%n"); // the 'out' must be in front of it!
But the real problem is the fact that the OP is trying to access the length of the String as if it was a public attribute, like this:

Expand|Select|Wrap|Line Numbers
  1. int i = aString.length;
but the method length() should be called:

Expand|Select|Wrap|Line Numbers
  1. int i = aString.length();
Jul 20 '07 #4
nickyeng
254 100+
word.length

if want to get word's length...you should call

word.length()
Jul 20 '07 #5
prometheuzz
197 Expert 100+
word.length

if want to get word's length...you should call

word.length()
How is that different from my answer?
Jul 20 '07 #6
JosAH
11,448 Expert 8TB
How is that different from my answer?
Your answer was first and his answer wasn't?

kind regards

Jos ;-)
Jul 20 '07 #7
prometheuzz
197 Expert 100+
Your answer was first and his answer wasn't?

kind regards

Jos ;-)
No, his answer was last and mine wasn't!
; )
Jul 20 '07 #8
JosAH
11,448 Expert 8TB
No, his answer was last and mine wasn't!
; )
We'd call it an 'ex equo' then and split the prize!

kind regards,

Jos ;-)
Jul 20 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mondo Dynamo | last post by:
A friend of mine is learning java as part of a Business Studies Degree and is finding it very hard to get to grips with. She has an assignment coming up soon and needs some help, she is offering...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
51
by: erikcw | last post by:
DiveIntoPython.org was the first book I read on python, and I really got a lot out of it. I need to start learning Java (to maintain a project I've inherited), and was wondering if anyone knew of...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
4
by: Tigger2000 | last post by:
I keep getting an error when trying to compile this for Java prog class, below is the instructions for the assignment and my code. Loop Assignment Write, compile, and run a Java program that...
6
by: penny | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
2
by: toprahman | last post by:
Hi guys, I'm a student teacher at a high school in Washington D.C. and I've come across a slight problem. See, I'm a math major, but one of the computer science teachers just quit and they need me to...
29
by: s0suk3 | last post by:
Hello, I was hoping to get some opinions on a subject. I've been programming Python for almost two years now. Recently I learned Perl, but frankly I'm not very comfortable with it. Now I want to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.