Results 1 to 13 of 13
  1. #1
    fkmk is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Problem with string [...]

    Hello, I am new to Java and I've got a problem already.
    The program should copy person with the highest percent from the file 'plik1', and then put it to the file 'plik2'.
    The problem occurs while compiling.

    Thank you for understanding and your help,

    fkmk

    Compilator:

    Java Code:
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    Code:

    Java Code:
    package zadanie2;
    import java.util.*;
    import java.io.*;
    
    public class zadanie2 {
    	public static void main(String[] args) throws Exception {
    		Scanner plik1 = new Scanner(new File("plik.txt"));
    		plik1.useLocale(Locale.US);
    		PrintWriter plik2 = new PrintWriter("plik_max.txt");
    		
    		Person person[] = new Person[5];
    		
    		System.out.println("List of people:");
    		
    		while(plik1.hasNext()) {
    			for(int counter=0; counter<5; counter++) {
    			person[counter].name = plik1.next();
    			person[counter].surname = plik1.next();
    			person[counter].age = plik1.nextInt();
    			person[counter].percent = plik1.nextFloat();
    			System.out.println("%s %s %d %f", person[counter].name, person[counter].surname, person[counter].age, person[counter].percent);
    			counter++;
    			}
    			
    			int counter = 0;
    	        float maximum = person[0].percent;
    
    	        for(counter=1; counter<5; counter++) {
    	        if(person[counter].percent > maximum)
    	        maximum = person[counter].percent;
    	        }
    	        
    	        char badge = '*';
    	        
    	        for(counter=0; counter<5; counter++) {
    	            if((person[counter].name.length() > 3) && (person[counter].surname.lastIndexOf("ski")) && (person[counter].percent == maximum)) {
    	            plik2.print(person[counter].name); plik2.print(person[counter].surname); plik2.println(person[counter].age);
    	            } }
    		}
    		plik1.close();
    		plik2.close();
    	}
    }
    
    class Person {
        public :
             string name;
             string surname;
             int age;
             float percent;
    }

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Problem with string [...]

    String has to be uppercase. So, 'String' not 'string'

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: Problem with string [...]

    A couple of things. public : is not valid syntax. And string should be String (case matters).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Problem with string [...]

    Also posted at: Problem with string [...]
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with string [...]

    Looks like someone's copied some C++ code...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,877
    Rep Power
    5

    Default Re: Problem with string [...]

    clearly; not only the STL 'string' class, but also a clear usage of a struct is in there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Problem with string [...]

    And just another point:
    Java wants to have each class inside a file with the same name as the class and a suffix of ".java".

    So if you have 2 classes zadanie2 and Person, then you should have 2 files zadanie2.java and Person.java.

    With kind regards,

    Konrad

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with string [...]

    That's only a requirement if both classes are public.
    Of course it is, usually, good practice anyway, even if the compiler is happy with it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,877
    Rep Power
    5

    Default Re: Problem with string [...]

    In the many years I've been programming Java, I have yet to come across a single use-case that would make me want to put a second class in the same source file (that is not an inner class). But its nice for students.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    fkmk is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Problem with string [...]

    Thank you guys for all your replies. :) I appreciate it.
    Btw. Could you please recommend any books about Java?

    Tolls, why C++ code works with Java? :) I admit, I put some code of C++. :)

    Regards.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with string [...]

    Quote Originally Posted by fkmk View Post
    Tolls, why C++ code works with Java? :) I admit, I put some code of C++. :)
    Um...it doesn't. At least not in the way you've done it above.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,877
    Rep Power
    5

    Default Re: Problem with string [...]

    How can you even say "it works" when you get so many errors? I'm just completely flabbergasted.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    fkmk is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Problem with string [...]

    I have followed your pieces of advice, and everything works perfectly. :) Seriously.

Similar Threads

  1. A problem with String
    By tzzt123 in forum New To Java
    Replies: 5
    Last Post: 05-13-2013, 03:09 AM
  2. Problem with <s:if> and string
    By SomeoneHere in forum Struts
    Replies: 1
    Last Post: 08-24-2012, 09:44 PM
  3. Having a problem with to String
    By thorobred in forum New To Java
    Replies: 20
    Last Post: 06-02-2012, 08:24 AM
  4. Replies: 6
    Last Post: 12-06-2010, 09:26 AM
  5. String Problem
    By gnomewise in forum New To Java
    Replies: 1
    Last Post: 10-19-2008, 12:37 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
  •