Read From a Text File and Using Switch

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 16
  • Joined: 07-March 15

How to combine Switch Statement and Reading Information from Text file

Posted 07 March 2015 - 08:08 AM

How-do-you-do All,

I know how to use Switch Statement. I too know How to read information from text file and display onto IDE console.
But, I am not sure even where to begin where I have to combine this two together.
I am assigned a homework where I take fifteen lines of data and values are separated by comma. we can say 15 rows and 3 columns.

Posted Image

public static void principal (String args[]) throws FileNotFoundException 	{ 		Scanner file = new Scanner(new File("information.txt")); 	   	file.close(); 		 	   	 	       		Scanner input = new Scanner(System.in); 	    String strResponse=""; 	    do 	    { 	      System.out.println("\nChapter 7"); 	       	      System.out.println("L - Bail Length"); 	       	      System.out.println("N - Bond Numbers (1=single, 2=double, 3=triple)\n"); 	      Arrangement.out.println("Q - quit programme."); 	      strResponse = input.nextLine(); 	      strResponse = strResponse.toUpperCase(); 	       	    	    	       	    }  	    	        	    while (!strResponse.equals("Q")); 	    System.out.println("\n\nThank you lot and accept a nice day."); 	 	     	   input.close(); 	         }            


Is This A Adept Question/Topic? 0

  • +

#2 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 09:36 AM

Quote

But, I am not sure fifty-fifty where to brainstorm where I have to combine this two together.

The reason for that is uncomplicated: there is no place for switch in that assignment

#iii Coffee.Coder User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 16
  • Joined: 07-March 15

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - ten:27 AM

So what practise yous suggest? How can I read information if my input is either bond length or number of bond

#four g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: twenty-September 08

Re: How to combine Switch Argument and Reading Information from Text file

Posted 07 March 2015 - 10:35 AM

Yes, i suppose if you need to ask them which column they are using, and so you lot could utilise a switch

I would tend to brand a List of some kind of data-encapsulating object. You lot could and then iterate that List

#5 jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: 20,991
  • Joined: xix-March 11

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 10:40 AM

You lot should showtime by stating explicitly what you're trying to practise. This is useful for people like me, who are interested in helping y'all figure out what yous need to practice, merely it's much more than of import for you, because spelling out everything you're trying to exercise volition greatly help you in thinking about how to become in that location. Don't start with the tools you want to use, showtime with the effect you're trying to produce. Then country how you remember you want to become that issue.

#half-dozen Java.Coder User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: sixteen
  • Joined: 07-March xv

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - eleven:23 AM

@jon.kiparsky

Let say I have these data in (data.txt)

C|C,370,0.154
C||C,680,0.13
C|||C,890,0.12
C|H,435,0.11
C|N,305,0.fifteen
C|O,360,0.fourteen
C|F,450,0.14
C|C,340,0.18
O|H,500,0.10
O|O,220,0.15
O|Si,375,0.xvi
Due north|H,430,0.10
N|O,250,0.12
F|F,160,0.14
H|H,435,0.074

Covalent bail(|=Single, ||=double, and |||= triple bond), Bail Energy, Bail Length

So If I enter Northward (number of bonds) in console then next affair it asks me select from: single, double, or triple

If I select Double or Two and then my Output would be

C||C,680,0.13

Case two If I enter L(length of bonds) in console
then in nextline I enter 0.12
then out would be
C|||C,890,0.12
Northward|O,250,0.12

In simple words. If i input something my output would other ii associated chemical and physical properties
and if there are multiple row that friction match my input then print all of them.

Thanks for your help

I am not certain why it is not displaying Image I accept attached in First mail service. Information technology would take made problem elementary for you guys to understand.

#vii g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 12:37 PM

Quote

It would take made problem simple for you guys to understand.

I have seen it, understood it and fabricated my recommendation ;)

#8 jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: 20,991
  • Joined: 19-March 11

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 04:13 PM

Quote

I am not sure why it is not displaying Image I have attached in Start mail.

Images are not a useful way to communicate information in text-axial medium. (such as, for case, a forum). Some people have visual impairments and won't get annihilation from them, others (like me) block ads, which ends up blocking images - which is generally fine, since images posted on forums are for the most function content-free.
Furthermore, information technology's useful for you to state the trouble, and your proposed solution, in your own words. After all, what you ultimately want is to country the solution in words the machine understands, isn't it?

So how do you want to go nigh solving this? You say y'all want to use a switch, just you don't say what you want to use information technology for. Once again, I think your focus should exist on figuring out the contours of your solution, and only later on the tools that you lot'll use to implement that solution. In my mind, this trouble is not an exception to my general rule that the correct number of switches in a well-written Java programme is zero.

One question you lot might ask yourself: is in that location an object here? Is there a part of this problem which looks like a little piece of data that knows how to do a few tricks? Thinking big-motion picture, if your goal is to learn Java, that means learning to write Java-like solutions to problems. That means thinking of your solutions in terms of objects and their interactions.

#9 Java.Coder User is offline

  • New D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 16
  • Joined: 07-March xv

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 09:21 PM

Ok I have decided to put data into ArrayList come across I go to move lilliputian frontwards with this assignment

Now I take few questions

How exercise I read specific line from arraylist?

{ 	public static void main (Cord args[]) throws FileNotFoundException{ 	   System.out.println("\nChapter 7 P7.21\n"); 	{ 	String token1 = ""; 	{ 	    Scanner file = new Scanner(new File("data.txt")).useDelimiter(",\\due south*"); 	    List<Cord> temps = new ArrayList<String>(); 	    while (file.hasNextLine()){ 	    	token1 = file.next(); 	    	temps.add(token1); 	    } 	      	      file.shut(); 	     String[] tempsArray = temps.toArray(new String[0]); 	     for (String due south : tempsArray){ 	    	 [b]Arrangement.out.println(s)[/b]; 	     } 	     Scanner input = new Scanner(System.in); 	     String strResponse=""; { 	    	    do 	    { 	      System.out.println("L - Bail Length"); 	      System.out.println("N - Bond Numbers (1 = single, ii =double, 3=triple)\n"); 	       	      Arrangement.out.println("Q - quit program."); 	      strResponse = input.nextLine(); 	      strResponse = strResponse.toUpperCase(); 	    	    } 	      while (!strResponse.equals("Q")); 	    System.out.println("\n\nThank you and have a nice day."); 		   input.close();  		   file.close(); 	   } 	     	           } 	 	} 	 } }            

#ten jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: 20,991
  • Joined: 19-March 11

Re: How to combine Switch Argument and Reading Data from Text file

Posted 07 March 2015 - 09:41 PM

Quote

How do I read specific line from arraylist?

You lot can access elements of an ArrayList past index:

ArrayList<Cord> al = getAnArrayListFromSomewhere(); Cord someString = al.get(3); // get the fourth chemical element in the list            

Y'all can also access all of the elements with an enhanced for loop:

ArrayList<String> al = getAnArrayListFromSomewhere(); for (String s: al) {   if (matchesSomeCondition(s)) {     takeSomeAction(s);   }            

The latter is probably the right mode to find the elements from the file which match some condition.

You can too apply the indexOf method to detect the index of a detail object in the list, if you lot already have a reference to that object. This won't practise you much good hither.

#11 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: How to combine Switch Statement and Reading Information from Text file

Posted 08 March 2015 - 06:27 AM

Re-read my comment. What y'all're doing currently will get you a long list of unrelated strings, which won't assistance you lot

#12 Java.Coder User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: xvi
  • Joined: 07-March 15

Re: How to combine Switch Statement and Reading Information from Text file

Posted 08 March 2015 - 06:20 PM

ok guys I am trying hard to become this thing to work
when I offset Plan

It should Requite me Three Option

Quote

Chapter vii P7.21
L - Bond Length
N - Bail Numbers (1 = unmarried, 2 =double, 3=triple)
Q - quit plan.

Information technology did that earlier
At present Afterward adding Switch argument
information technology gives me

Quote

Chapter seven P7.21
L - Bail Length
Due north - Bond Numbers (1 = single, ii =double, three=triple)
Number of bonds

my problem is I desire number of bonds to appear only if user input"N"
Second Problem is Information technology does not show Q = Quit Program
Information technology shows this later on I press only input of Due north

Quote

Chapter seven P7.21

L - Bond Length
North - Bond Numbers (1 = unmarried, 2 =double, three=triple)

Number of bonds
1
C|C,370,0.154
Q - quit program.
Exception in thread "master" coffee.util.NoSuchElementException: No line found
at coffee.util.Scanner.nextLine(Unknown Source)
at Patel_Prafulkumar.chief(Patel_Prafulkumar.coffee:47)

public static void main (String args[]) throws FileNotFoundException 	   	{ 		Scanner file = new Scanner(new File("information.txt")); 		file.close(); 		 	    Scanner input = new Scanner(Organization.in); 	     Cord strResponse="";  	    do 	    {  	    System.out.println("\nChapter seven P7.21\due north"); 	      System.out.println("\nL - Bond Length"); 	       	      System.out.println("N - Bond Numbers (i = single, 2 =double, three=triple)\north"); 	       	      input.equals("n"); 	    	  { 	    	  Cord bondnumber = ""; 	    	  System.out.println("Number of bonds"); 	    	Scanner myInput = new Scanner(System.in); 	    	int Bond = myInput.nextInt(); 	    	myInput.shut(); 	    	switch  (Bail){ 	    	case ane : bondnumber = "C|C,370,0.154"  ; 	    			pause; 	    	instance 2 : bondnumber = "C||C,680,0.xiii"; 	    			break; 	    	case 3 : bondnumber = "C|||C,890,0.12"; 	    			break; 	    	default: System.out.println("Invalid Number entered."); 	    			interruption; 	    	} 	    	System.out.println(bondnumber); 	    	 	    		 	    	} 	    	 	    	 	       	      Arrangement.out.println("Q - quit program."); 	      strResponse = input.nextLine(); 	      strResponse = strResponse.toUpperCase(); 	    	    } 	      while (!strResponse.equals("Q")); 	    System.out.println("\n\nThank you and have a nice solar day."); 		   input.shut();  		    	   } 	     	           }            

#13 jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: 20,991
  • Joined: nineteen-March eleven

Re: How to combine Switch Argument and Reading Data from Text file

Posted 08 March 2015 - 06:23 PM

I think you want an if at line sixteen?

(that doesn't explain why you lot've got that stuff hardcoded in the switch - that's making no sense to me at all)

#14 Java.Coder User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 16
  • Joined: 07-March 15

Re: How to combine Switch Statement and Reading Data from Text file

Posted 08 March 2015 - 06:28 PM

How tin can I accept more one string in instance i
I want program to print

Quote

C|C,370,0.154
C|H,435,0.xi
C|N,305,0.fifteen
C|O,360,0.14
C|F,450,0.14
C|C,340,0.18
O|H,500,0.10
O|O,220,0.15
O|Si,375,0.xvi
Due north|H,430,0.10
North|O,250,0.12
F|F,160,0.14
H|H,435,0.074

View Postjon.kiparsky, on 08 March 2015 - 06:23 PM, said:

I think y'all desire an if at line 16?

(that doesn't explain why y'all've got that stuff hardcoded in the switch - that's making no sense to me at all)

Tin can you tell me exact code at line sixteen. please i dont have much fourth dimension ponder and wonder. information technology is due in next two hours.

I did what I know. It may not be correct something is better than zero. I dont know how to read specific line from file to satisfy switch statement.

#15 jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: twenty,991
  • Joined: 19-March 11

Re: How to combine Switch Statement and Reading Data from Text file

Posted 08 March 2015 - 06:32 PM

That's the trouble with switches. Y'all tin can't get dynamic data into them, because they're defined at compile time. So, to be precise, yous can't read in data from a file at run time and get it into your switch.

Have you noticed how we've been telling y'all that this isn't a good identify to utilise a switch? It's still not a good identify to use a switch. The problem hither is to read the data from the file and decide if information technology matches the criteria prepare past the user - it'south not about writing the information into your source code.

stoneclavory.blogspot.com

Source: https://www.dreamincode.net/forums/topic/371651-how-to-combine-switch-statement-and-reading-data-from-text-file/

Related Posts

0 Response to "Read From a Text File and Using Switch"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel