How to Read Lines From a Scanner
11 Replies - 13944 Views - Last Mail: eleven June 2011 - 09:50 PM
#1
Using scanner to browse specific lines.
Posted 10 June 2011 - 12:fifteen PM
Alright what i am trying to do is browse a file which would accept 3 lines.My scanner should browse the 3 lines and keep them in an assortment. And i accept a choicebox applet when i choose choice 1 should read line 1 and when i choose choice ii should choose line ii. etc etc. This is what i have and then far because i am not sure how to go along them in an array. Tin anyone help?
import java.applet.Applet; import java.awt.*; import coffee.io.*; import coffee.util.*; public grade ChoiceBox extends Applet { private Selection choice; Scanner Scan = cypher; public void init() { setFont(new Font("Helvetica", Font.Bold, 36)); choice = new Choice(); choice.addItem("Automobile 1"); choice.addItem("Motorcar 2"); choice.addItem("Car three"); add together(selection); try{ Scan = new Scanner (new File("C:\\Users\\Luis\\Desktop\\chinese.txt")); } take hold of(Exception e){ Arrangement.out.println("could non detect file"); System.exit(1); } } public boolean action(Event effect, Object object) { if (event.target == choice) { String choice = selection.getSelectedItem(); if (selection.equals("Motorcar 1")) doChoice1Action(); else if (selection.equals("Car 2")) doChoice2Action(); else if (selection.equals("Car 3")) doChoice3Action(); return(truthful); } else return(faux); } public void doChoice1Action() { String Text = ""; Text += Scan.nextLine() ; System.out.println(Text); } public void doChoice2Action() { String Text = ""; Text += Scan.nextLine() ; Organization.out.println(Text); } public void doChoice3Action() { String Text = " "; Text += Scan.nextLine() ; System.out.println(Text); } } Every bit yous can see all this program does is read a line and expect until its called once more and stays on the same spot. Please assist =/
This mail has been edited by macosxnerd101: eleven June 2011 - 01:l PM
Reason for edit:: Please use code tags
Replies To: Using scanner to browse specific lines.
#ii
Re: Using scanner to browse specific lines.
Posted ten June 2011 - 12:49 PM
bump
#three
Re: Using scanner to scan specific lines.
Posted x June 2011 - 12:50 PM
Hi, put your code in tags so it is readable.
Click on the [CODE ] icon.
#4
Re: Using scanner to browse specific lines.
Posted 10 June 2011 - 12:50 PM
Delight edit your post so the code is in lawmaking blocks [ code ] code here [/ lawmaking ] without the spaces.
Next, please dont bump your post, otherwise no ane will look at them.
Lastly, I havnt done much in applets, but along with the init method, I think y'all also crave a start and finish. Will expect at the lawmaking further once y'all edit your post and I can run into it properly.
#5
Re: Using scanner to browse specific lines.
Posted ten June 2011 - 12:58 PM
import java.applet.Applet; import java.awt.*; import java.io.*; import coffee.util.*; public grade ChoiceBox extends Applet { private Choice pick; Scanner Scan = null; public void init() { setFont(new Font("Helvetica", Font.BOLD, 36)); choice = new Option(); choice.addItem("Auto 1"); selection.addItem("Auto 2"); choice.addItem("Car 3"); add(selection); try{ Scan = new Scanner (new File("C:\\Users\\Luis\\Desktop\\chinese.txt")); } take hold of(Exception e){ System.out.println("could not find file"); System.exit(1); } } public boolean activeness(Event event, Object object) { if (issue.target == option) { String selection = choice.getSelectedItem(); if (selection.equals("Car one")) doChoice1Action(); else if (pick.equals("Machine 2")) doChoice2Action(); else if (choice.equals("Machine 3")) doChoice3Action(); return(truthful); } else return(false); } public void doChoice1Action() { String Text = ""; Text += Scan.nextLine() ; Organization.out.println(Text); } public void doChoice2Action() { String Text = ""; Text += Browse.nextLine() ; System.out.println(Text); } public void doChoice3Action() { String Text = " "; Text += Scan.nextLine() ; System.out.println(Text); } } Lamentable about bump and ok thanks
#half-dozen
Re: Using scanner to scan specific lines.
Posted 10 June 2011 - 01:04 PM
Yous need a showtime and stop method, as when you extend an applet, yous have to override these. Go through this so y'all get a better understanding of what is involved in an applet
HERE
#seven
Re: Using scanner to scan specific lines.
Posted x June 2011 - 01:17 PM
i've actually seen this page about the init, outset, stop methods for an applet i've been just messing around with applets i exercise know i have to modify a few things on my plan. but i still am not certain on how to shop the text into an array. My scanner should scan the 3 lines and proceed them in an array. And i have a choicebox applet when i choose choice one should read line 1 and when i cull selection ii should choose line 2. etc etc. This is what i take and so far because i am non certain how to proceed them in an array.
#8
Re: Using scanner to scan specific lines.
Posted 10 June 2011 - 05:44 PM
An Applet cannot access the local file system... so your question pose a fake problem. You lot but can't admission C:\\Users\\Luis\\Desktop\\chinese.txt from an Applet
#9
Re: Using scanner to browse specific lines.
Posted 11 June 2011 - 08:20 AM
Quote
Y'all just tin't access C:\\Users\\Luis\\Desktop\\chinese.txt from an Applet
That'southward not really the example. An applet tin can access its codebase and if the above happens to be its codebase, so that'due south fine.* But don't create a File object: read the codebase thus:
Scanner southward = new Scanner(getClass().getResourceAsStreamAsStream("/chinese.txt")); * Footnote:
Of grade, deploying an applet in any context other than that of a spider web server is really near useless
This mail service has been edited by g00se: 11 June 2011 - 08:25 AM
#x
Re: Using scanner to scan specific lines.
Posted 11 June 2011 - 10:47 AM
i see, really i might have been doing things wrong, since im a bit new to java. Anyways i've been watching some java tutorials at the following website "thenewboston.com". And i just been trying to come up with a few things myself. i believe i fixed information technology using a count variable and a val variable to shop the input and its working so far.. this is the following code. if anyone can assist modify for it can piece of work better it would exist neat. basically i am but reading the file from my desktop. all it has is 3 lines with a few numbers.
import java.applet.Applet; import java.awt.*; import coffee.io.*; import coffee.util.*; import javax.swing.JLabel; public class ChoiceBox extends Applet { private Option choice; Scanner Scan = cipher; int count1 =0; int count2 =0; int count3 =0; String Text = ""; String Text2 = ""; Cord Text3 =""; String val1 = ""; String val2 = ""; Cord val3 = ""; //JLabel characterization = new JLabel( "asd" + JLabel.S); public void init() { setFont(new Font("Helvetica", Font.Assuming, 36)); choice = new Choice(); selection.addItem("Car 1"); choice.addItem("Car ii"); option.addItem("Car three"); add(choice); try{ Scan = new Scanner (new File("C:\\Users\\Luis\\Desktop\\chinese.txt")); } take hold of(Exception due east){ System.out.println("could non notice file"); System.get out(i); } } public boolean activeness(Event event, Object object) { if (effect.target == option) { String option = selection.getSelectedItem(); if (choice.equals("Motorcar i")) doChoice1Action(); else if (choice.equals("Machine two")) doChoice2Action(); else if (option.equals("Car 3")) doChoice3Action(); return(true); } else return(imitation); } public void doChoice1Action() { if (count1 ==0){ Text += Scan.nextLine() ; count1++; Arrangement.out.println(Text); //System.out.println(count1); } else val1 = Text; Organisation.out.println(" Toyota =" +val1); //System.out.println(count1); } public void doChoice2Action() { if (count2 ==0){ Text2 = ""; Text2 += Scan.nextLine() ; count2++; System.out.println(Text2); //Organization.out.println(count2); } else val2 = Text2; System.out.println("Honda Civic" +val2); //Arrangement.out.println(count2); } public void doChoice3Action() { if (count3 ==0){ Text3 += Scan.nextLine() ; count3++; System.out.println(Text3); //Organization.out.println(count3); } else val3 = Text3; System.out.println("Subaru =" +val3); //label.setText(val3); } } #xi
Re: Using scanner to scan specific lines.
Posted 11 June 2011 - 01:55 PM
What do you think you are doing incorrect? What is/isn't your program doing? How does it deviate from requirements?
I know earlier y'all were request most arrays. TriggaMike'due south arrays tutorial is a adept place to start. If you're reading from a File with an unknown number of lines, consider using an ArrayList<String> instead, as ArrayLists are resizable.
You are on the write track with using a count variable with an assortment. The algorithm to use is:
count <-- 0 text <-- array[someLength] while at that place are more lines to read in and count < someLength text[count] <-- read line count <-- count + one end while
#12
Re: Using scanner to scan specific lines.
Posted xi June 2011 - 09:50 PM
Would be a lot easier to have a unmarried doAction() method that receive the count of lines to skip has parameter:
String car; if(selection.equals("Auto-1")) car = doAction(0); if(selection.equals("Machine-two")) car = doAction(1); if(selection.equals("Car-iii")) auto = doAction(2); ... Cord doAction(int nbToSkip) { for(int i = 0; i < nbToSkip; ++i) scan.nextLine(); render scan.nextLine(); } - ← Previous Topic
- Java
- Side by side Topic →
Source: https://www.dreamincode.net/forums/topic/235245-using-scanner-to-scan-specific-lines/
0 Response to "How to Read Lines From a Scanner"
Post a Comment